Hey guys, I am just waiting for the xbee sheilds, should be here thursday. I have the code ready for all three arduinos, I was hoping someone could take a quick look. I'm not too sure about serial communication parts.
Can you take a look at this code and see if I'm on the right track for making these three units communicate with each other? Thanks.
Here is what I have for the handheld unit:
/*
Pin 2 is connected to switch terminal and one end of resistor
other end of resistor is connected to ground
other switch terminal is connected to 5v
Pin 12 is for transmit indicator LED
*/
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 12; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int tim = 0;
void setup() { //Arduino initial setup
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600); //set baud rate on Arduino to 9600
}
void loop() { //loop program
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:L
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
Serial.print("H"); //Send the character H
tim = 1;
}
else if (tim == 1) {
// turn LED off:
digitalWrite(ledPin, LOW);
Serial.print("L"); //Send the character H
tim = 0;
}
}
Here is what I have for the first receiving unit:
// set pin numbers:
const int LEDBlink = 13; // the number of the 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 follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 2000; // interval at which to blink (milliseconds)
long interval2 = 100; // interval at which to blink (milliseconds)
int LEDButton = 11; // Assign Pin 11 for Button LED
void setup() { //Arduino initial setup
Serial.begin(9600); //Set baud rate for Arduino to 9600
pinMode(LEDBlink, OUTPUT); // set the digital pin as output:
pinMode(LEDButton, OUTPUT); // Make Digital Pin 11 as Output Pin
}
void loop() { // loop program
if (Serial.available()) { //If there is data in the Serial Line
int dataByte = Serial.read(); //save data into integer variable dataByte
Serial.print(dataByte, BYTE); //Print to screen the variable received
if(dataByte == 'H'){ //if the variable is the character H
digitalWrite(LEDButton, HIGH); //then Button LED pin is ON
}
else if(dataByte == 'L') { // if the variable is the character L
digitalWrite(LEDButton, LOW); // then Button LED pin is off
}
}
unsigned long currentMillis = millis();
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("N"); //sends N to other arduino
}
else {
ledState = LOW;
Serial.print("F"); //sends F to other arduino
}
// 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("F"); //sends F to other arduino
digitalWrite(LEDBlink, ledState);
previousMillis = currentMillis; }
}
}
Here is what I have for the second receiving unit:
int LEDBlink = 11; // Assign Pin 11 for Sync LED
int LEDButton = 9; // Assign Pin 9 for Button LED
void setup() { //Arduino initial setup
Serial.begin(9600); //Set baud rate for Arduino to 9600
pinMode(LEDBlink, OUTPUT); // set the digital pin as output:
pinMode(LEDButton, OUTPUT); // set the digital pin as output:
}
void loop() { // loop program
if (Serial.available()) { //If there is data in the Serial Line
int dataByte = Serial.read(); //save data into integer variable dataByte
Serial.print(dataByte, BYTE); //Print to screen the variable received
if(dataByte == 'N'){ //if the variable is the character N
digitalWrite(LEDBlink, HIGH); //then Sync LED pin is ON
}
else if(dataByte == 'F') { // if the variable is the character F
digitalWrite(LEDBlink, LOW); // then Sync LED pin is off
}
if(dataByte == 'H'){ //if the variable is the character H
digitalWrite(LEDButton, HIGH); //then Button LED pin is ON
}
else if(dataByte == 'L') { // if the variable is the character L
digitalWrite(LEDButton, LOW); // then Button LED pin is off
}
}
}