Transceiver Unit:
// Original code borrowed from "Blink without delay"
// and "Multijoy" by Principia Labs
int LEDButton = 9; // Button LED on Pin 9 named LEDButton
const int LEDBlink = 11; // the number of the sync LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the following variables are "long" because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 10000; // Off time for Sync LED (milliseconds)
long interval2 = 500; // On time for Sync LED (milliseconds)
// 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
int pinState = LOW; //Initial pinstate set
void setup()
{
// LED on Pin 11 for digital on/off demo
pinMode(LEDBlink, OUTPUT);
// LED on Pin 9 for digital on/off demo
pinMode(LEDButton, OUTPUT);
// Open the serial connection, 9600 baud
Serial.begin(9600);
}
void loop() {
// 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 = LED to control?
servo = userInput[0];
// Second byte = On or off?
pos = userInput[1];
// Packet error checking and recovery
if (pos == 255) { servo = 255; }
// Assign new position to appropriate servo (LED)
switch (servo) {
// LED on Pin 9 for digital on/off demo
case 99:
if (pos == 180) {
if (pinState == LOW)
pinState = HIGH;
Serial.print(char(255)); // error protection
Serial.print(char(98)); // send command for "case 98"
Serial.print(char(180)); // send state command
}
else {
pinState = LOW;
Serial.print(char(255)); // error protection
Serial.print(char(98)); // send command for "case 98"
Serial.print(char(0)); // send state command
}
if (pos == 0) {
pinState = LOW;
Serial.print(char(255)); // error protection
Serial.print(char(98)); // send command for "case 98"
Serial.print(char(0)); // send state command
}
digitalWrite(LEDButton, pinState);
break;
}
}
}
unsigned long currentMillis = millis(); // "millis" is an internal timer that
// starts running on power up
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW){
ledState = HIGH;
Serial.print(char(255)); // error protection
Serial.print(char(97)); // send command for "case 97"
Serial.print(char(180)); // send state command
}
else {
ledState = LOW;
Serial.print(char(255)); // error protection
Serial.print(char(97)); // send command for "case 97"
Serial.print(char(0)); // send state command
}
// set the LED with the ledState of the variable:
digitalWrite(LEDBlink, ledState);
}
else if(currentMillis - previousMillis > interval2) {
// save the last time you blinked the LED
if (ledState == HIGH){
ledState = LOW;
Serial.print(char(255)); // error protection
Serial.print(char(97)); // send command for "case 97"
Serial.print(char(0)); // send state command
digitalWrite(LEDBlink, ledState);
previousMillis = currentMillis; }
}
}
This code receives the signal from the handheld and relays it to Transceiver 2. It also sends a signal to the handheld as confirmation.