This is a prototype for verifying the design to control a servo motor via a wireless interface that supports link loss indication.
Schematic
Master (Local)
Slave (Remote)
Source Code - Master (Local)
const int LED_Alive = 2; // Alive indicator LED
const int LED_Error = 3; // Error indicator LED
int state = 0; // state of output signal, 0 = "L", 1 = "H".
int incomingByte; // a variable to read incoming serial data into
void setup() {
Serial.begin(9600);
pinMode(LED_Alive, OUTPUT);
}
void loop() {
long interval = 300000; // 1 second?
int ok = 0; // 0 = reply not ok, 1 = reply ok.
// switch the output status
if (state == 0) {
state = 1;
} else {
state = 0;
}
if (state == 1) {
Serial.print("H"); // Send "H"
digitalWrite(LED_Alive, HIGH);
} else {
Serial.print("L"); // Send "L"
digitalWrite(LED_Alive, LOW);
}
// Listen to reply from remote site for 1 second
while (interval >= 0) {
if (Serial.available() > 0) {
incomingByte = Serial.read();
//Serial.print(incomingByte);
//break; // break from the while loop
}
interval = interval - 1;
}
switch (incomingByte) {
case 72: // "H"
if (state == 1) ok = 1; // Send "H", receive "H".
break;
case 76: // "L"
if (state == 0) ok = 1; // Send "L", receive "L".
break;
default:
ok = 0; // if nothing else matches, do the default
break;
}
if (ok == 0)
digitalWrite(LED_Error, HIGH); // Turn on the error indicator
else
digitalWrite(LED_Error, LOW); // Turn off the error indicator
}
Source Code - Slave (Remote)
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
myservo.attach(A0); // attaches the servo on pin 0 to the servo object
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
Serial.print("H"); // send reply back to remote site
digitalWrite(ledPin, HIGH);
myservo.write(180); // tell servo to go to position 180
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
Serial.print("L");
digitalWrite(ledPin, LOW);
myservo.write(0); // tell servo to go to position 0
}
}
}
Note,
There seems to be a problem with the serial interface of the Arduino Nano used for this project. If the Nano is powered off (unplug the USB from the computer) then powered on (plug the USB cable back to the computer) while the above program is running, the Nano won't be able to send feedback to the Master device (the Link Loss Indicator on the Master device will keep flashing). When this happens, launching the Serial Monitor on the computer attaching to the Nano or re-uploading the program to the Nano could solve this problem. However, this problem doesn't exist if the Nano is replaced with Uno.
No comments:
Post a Comment