Pages: [1]   Go Down
Author Topic: inter-arduino communication  (Read 878 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Quote

 
// 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 () );
}


 

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Prototype Limited Edition owner! lol
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suggest using hex values for start bits and so on. Like 0xF0 (use the hex of a wierd ascii character u never use). Hex works faster in computer world. And easier to work with.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok thanks, how do i send a hex code? is it just a case of sending:

Serial.print(0xFF);

or do i need to do something else?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
I do my own experiments.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

both 9 and 0xFF are 1 byte of data so I don't agree that one is faster than the other. I do agree with the rest of what Intosia said.

int startBit=9;

Serial.print(startBit,BYTE);

Serial.print(0xFF,BYTE);
Logged

Pages: [1]   Go Up
Jump to: