Hello,
For my project I'm working to take 5 switches, hooked up to 5V, and to a Mega, each on their own pin, and send a message when they turn on. Followed by a new message sent when they go off. Eventually I want to be able to send a confirmation message back from a button push on the receiver, but that isn't my worry now, but some of the coding with show future support built in already.
The code for this looks like:
const int switchOne = 52;
const int switchTwo = 51;
const int switchThree = 50;
const int switchFour = 49;
const int switchFive = 48;
const int ledOne = 22;
const int ledTwo = 26;
const int ledThree = 30;
const int ledFour = 34;
const int ledFive = 38;
int switchStateOne = 0;
int lastSwitchOneState = 0;
int switchStateTwo = 0;
int lastSwitchTwoState = 0;
int switchStateThree = 0;
int lastSwitchThreeState = 0;
int switchStateFour = 0;
int lastSwitchFourState = 0;
int switchStateFive = 0;
int lastSwitchFiveState = 0;
void setup() {
// put your setup code here, to run once:
pinMode(switchOne, INPUT);
pinMode(switchTwo, INPUT);
pinMode(switchThree, INPUT);
pinMode(switchFour, INPUT);
pinMode(switchFive, INPUT);
pinMode(ledOne, OUTPUT);
pinMode(ledTwo, OUTPUT);
pinMode(ledThree, OUTPUT);
pinMode(ledFour, OUTPUT);
pinMode(ledFive, OUTPUT);
Serial.begin(9600);
Serial.println('0');
}
void loop() {
// put your main code here, to run repeatedly:
int switchStateOne = digitalRead(switchOne);
int switchStateTwo = digitalRead(switchTwo);
int switchStateThree = digitalRead(switchThree);
int switchStateFour = digitalRead(switchFour);
int switchStateFive = digitalRead(switchFive);
if (switchStateOne != lastSwitchOneState){
if(switchStateOne == LOW){
Serial.println("0");
Serial.println("0x0013");
Serial.println('A');
}
else
{
Serial.println("0");
Serial.println("0x0013");
Serial.println('B');
}
}
lastSwitchOneState = switchStateOne;
}
This message is received by 5 separate XBEE series 1, all in end device mode, coded to the same PAN. These should then send the serial to an Arduino Nano, and the Nano should compare the data, and if the ID, serial byte 2, matches, preform an action base on the 3rd byte, the Msg_In. This is my current code for the receiver:
/*Test code built for multiple wearable devices. This is build for
* Arduino Nanos, with Serial coming in on TX and RX from Xbee. Pin
* 13 allows for an RGB ws2812b chip (neopixel), When serial
* comes in to "go"('b') the LED goes off. This resets it for the next cue.
*
* Jake Jobes September 25th, 2016
*/
//Set up Pixel Library and data
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// Can be changed for board
#define PIN 13
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 1
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//Store Message from Master
int Msg_In = 0;
int SerID_In = 0;
unsigned int IDval == '13';
//Address for this individual Xbee and Arduino
//MUST MATCH THE XBEE PLUGGED IN TO THIS DEVICE
#include <SoftwareSerial.h> //Serial pins any pins
SoftwareSerial Serial(1, 2); //Arudino RX, TX
void setup() {
// put your setup code here, to run once:
pixels.begin();
Serial.begin(9600);
}
void loop() {
// if Serial recieved is at least 2 bytes, read it
if (Serial.available() > 2)
{
SerID_In = Serial.read();
IDval = (SerID_In << 8) + Serial.read();
Msg_In = Serial.read();
//If message is for me, read it
if(IDval == SerID_In)
{
// If message is 1, turn on my led
// If message is 2, turn off my led and vibe
switch (Msg_In)
{
case 'A':
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.
pixels.show();
}
case 'B':
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(0,0,0)); // Off
}
pixels.show();
}
}
}
}
The problem I'm having is the Arduino isn't addressing the received serial correctly. I know the XBEE is receiving the sent message in 3 bytes correctly, can I can see the Arudino blink its RX LED, getting the message, but the LED does not turn on.
I think It's getting caught up in the:
if (Serial.available() > 2)
{
SerID_In = Serial.read();
IDval = (SerID_In << 8) + Serial.read();
Msg_In = Serial.read();
Any help would be appreciated!