Hi all, First post on the forum but been using it for info for a while. Brilliant!
Anyway, i'm starting to develop a simple home automation system. so far i've started to look at how i'm gonna send messages to the slave arduino's from the master. So obviously i'm gonna need addressing, and a protocol of some sort. this is what i've got so far, using the messenger library.
// This example sets all the values of the digital pins with a list through a callback function
#include <Messenger.h>
// Instantiate Messenger object with the default separator (the space character)
Messenger message = Messenger();
int LEDpin = 13;
// Create the callback function
void messageReady() {
int i = 0;
int tempstr = 0;
int startBit = 9;
int userInput[2];
// Loop through all the available elements of the message
while ( message.available() ) {
for(i = 0; i<3; i++) {
userInput = message.readInt();
}
if (userInput[0] == startBit) {*
switch (userInput[1]) {*
case 1:*
Serial.print("One");*
Serial.print(userInput[2]);*
if(userInput[2] <2) {*
digitalWrite(LEDpin,userInput[2]);*
}*
break;*
case 2:*
Serial.print("two");*
Serial.print(userInput[2]);*
break;*
default:*
Serial.print("Not Understood");*
}*
}*
else {*
Serial.print("NACK");*
}*
}*
}void setup() {
- // Initiate Serial Communication*
- Serial.begin(115200);*
- // Attach the callback function to the Messenger*
- message.attach(messageReady);*
}
void loop() {- // The following line is the most effective way of using Serial and Messenger's callback*
- while ( Serial.available() ) message.process(Serial.read () );*
}
[/quote]
What do you think? basically i send a "9" as a start bit, then it looks at the next bit to decide where the message is destined, i.e arduino 1 or 2. Then it looks at the last valule to decide what to do. adding another bit on the end could allow for more flexibility. Anyway, is there a better way to do this that using switch/case?
Any info or help will be very gratefully recieved!!!
Harry