Receiving Unit:
// Original code borrowed from "Blink without delay"
// and "Multijoy" by Principia Labs
// Import the Arduino Servo library
#include <Servo.h>
// User input for servo and position
int userInput[3]; // raw input from serial buffer, 3 bytes
int startbyte; // start byte, begin reading input
int servo; // which servo to pulse?
int pos; // servo angle 0-180
int i; // iterator
// LED on Pin 51 assigned to button
int LEDButton = 9;
int LEDSync = 11;
int pinState = LOW;
void setup()
{
// LED on Pin 53 for digital on/off demo
pinMode(LEDButton, OUTPUT);
pinMode(LEDSync, OUTPUT);
// Open the serial connection, 9600 baud
Serial.begin(9600);
}
void loop() { // loop program
// Wait for serial input (min 3 bytes in buffer)
if (Serial.available() > 2) {
// Read the first byte
startbyte = Serial.read();
// If it's really the startbyte (255) ...
if (startbyte == 255) {
// ... then get the next two bytes
for (i=0;i<2;i++) {
userInput[i] = Serial.read();
}
// First byte = servo to move?
servo = userInput[0];
// Second byte = which position?
pos = userInput[1];
// Packet error checking and recovery
if (pos == 255) { servo = 255; }
// Assign new position to appropriate servo
switch (servo) {
// LED on Pin 9 for digital on/off demo
case 98:
if (pos == 180) {
if (pinState == LOW)
pinState = HIGH;
}
else pinState = LOW;
if (pos == 0) {
pinState = LOW;
}
digitalWrite(LEDButton, pinState);
break;
case 97:
if (pos == 180) {
if (pinState == LOW)
pinState = HIGH;
}
else pinState = LOW;
if (pos == 0) {
pinState = LOW;
}
digitalWrite(LEDSync, pinState);
break;
}
}
}
}
This code allows the second unit to receive the relayed signal from the first unit.