New Basic Arduino Serial Communication

This is an updated example of basic serial communication between Arduinos, the old example can be found here.

Sender code:

[Arduino]

// SENDER
int b1 = 7;
int b2 = 8;

void setup()
{
Serial.begin(9600);
pinMode(b1, INPUT);
pinMode(b2, INPUT);
digitalWrite(b1, HIGH);
digitalWrite(b2, HIGH);
}

void loop()
{
byte val1 = (byte)digitalRead(b1);
byte val2 = (byte)digitalRead(b2);
Serial.write(‘a’); //SYNC char
Serial.write(val1);
Serial.write(‘b’); //SYNC char
Serial.write(val2);
delay(50);
}

 

[/Arduino]

Receiver Code:

// RECIEVER
byte led1;
byte led2;

void setup() {
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
delay(1000);
}

void loop() {
if(Serial.available() > 0)
{
char inChar = Serial.read();
while(Serial.available()<=0);

if (inChar == ‘a’)
{
led1 = Serial.read();
}

if (inChar == ‘b’)
{
led2 = Serial.read();
}
}

digitalWrite(9, led1);
digitalWrite(8, led2);
delay(10);
}

13 thoughts on “New Basic Arduino Serial Communication”

  1. Hi what is the problem i misunderstanding pls help .and pls schematic diagram put your site orsend me rgds

    sketch_aug11a:18: error: stray ‘\’ in program
    sketch_aug11a:18: error: stray ‘\’ in program
    sketch_aug11a:20: error: stray ‘\’ in program
    sketch_aug11a:20: error: stray ‘\’ in program
    sketch_aug11a.ino: In function ‘void loop()’:
    sketch_aug11a:18: error: ‘u2018a’ was not declared in this scope
    sketch_aug11a:20: error: ‘u2018b’ was not declared in this scope

  2. Hi Gui! Nice job with your xbees! (I refer to the old post).
    It looks like something I’m trying to do with a couple of Bluetooth Bee-Standalone.

    They have an ATMEGA168 onboard and Seeedstudio’s product page says:
    “Save that extra Arduino you have lying around for another project, this two-for-one device has the Arduino built-in. Bluetooth Bee with Arduino is an enhanced version of the Bluetooth Bee. We added an onboard ATMEGA168 to it to make it a full-featured Arduino while maintaining the same footprint and instruction set as the Bluetooth Bee…”
    So, I guess I could use ’em like yours without mounting ’em on an Arduino…
    Did you have any exprience with this kind of modules?

    Anyway, how did you set up each module before loading up the sketch?

    Talking about your new sketch, Arduino’s IDE reports the following errors in function ‘void loop()’ for lines 18 and 23:
    warning: comparison with string literal results in unspecified behaviour
    error: ISO C++ forbids comparison between pointer and integer

    Any help would be really appreciated, thanks – Erik.

  3. Hi Erik!

    I never used the Bluetooth Bee-Standalone modules, they look quite good, I never had good reference regarding bluetooth modules and always try to avoid them.

    Regarding the sketch errors, you need to change the ´` to ”
    I am trying to solve the code snippet post into wordpress.

    Cheers and good luck with your project!

  4. Thank you very much for your attention.
    I’ll give you a feedback for any successful progress.
    Best regards ;) Erik

  5. Hi!
    I worked a bit with my BT Standalone modules.
    I even tried your sketches, but I had no results…
    My goal is to turn on and off some leds, driving them via switches and bluetooth connection.
    At the moment (after setting up basic parameters via “\r\n+” command in the sketch) I can drive my system with the following sketch for the master bee:

    // SENDER ******************************************************

    #include //Software Serial Port
    #define RxD 2 // these ‘2’ & ‘3’ are fixed by the hardware
    #define TxD 3

    SoftwareSerial blueToothSerial(RxD,TxD);
    const int buttonPin1 = 5;
    const int buttonPin2 = 6;

    int buttonState1 = 0;
    int buttonState2 = 0;

    char s1;
    char s2;

    // setup *******************************************************

    void setup()
    {
    Serial.begin(19200);
    pinMode(RxD, INPUT);
    pinMode(TxD, OUTPUT);

    pinMode(buttonPin1, INPUT);
    pinMode(buttonPin2, INPUT);

    setupBlueToothConnection();
    }

    // connection **************************************************

    void setupBlueToothConnection()
    {
    blueToothSerial.begin(38400);
    delay(1000);
    blueToothSerial.print(“\r\n+CONN=0,13,EF,0,8,F9\r\n”);
    }

    // logic *******************************************************

    void loop()
    {
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);

    if (buttonState1 == HIGH) {
    s1=(‘a’);
    }

    else {
    s1=(‘b’);
    }

    if (buttonState2 == HIGH) {
    s2=(‘c’);
    }

    else {
    s2=(‘d’);
    }

    // data ********************************************************

    blueToothSerial.write(s1);
    delay(10);
    blueToothSerial.write(s2);
    delay(10);
    }

    and the slave bee:

    // RECEIVER ****************************************************

    #include //Software Serial Port
    #define RxD 2 // these ‘2’ & ‘3’ are fixed by the hardware
    #define TxD 3

    SoftwareSerial blueToothSerial(RxD,TxD);
    int led1 = 5;
    int led2 = 6;

    // setup *******************************************************

    void setup()
    {
    Serial.begin(19200);
    pinMode(RxD, INPUT);
    pinMode(TxD, OUTPUT);
    pinMode(led1,OUTPUT);
    pinMode(led2,OUTPUT);
    setupBlueToothConnection();
    }

    // connection **************************************************

    void setupBlueToothConnection()
    {
    blueToothSerial.begin(38400);
    delay(1000);
    blueToothSerial.print(“\r\n+CONN=0,13,EF,0,A,B7\r\n”);
    }

    // logic *******************************************************

    void loop()
    {
    char blueToothData = (char)blueToothSerial.read();

    if (blueToothData == ‘a’) {
    digitalWrite(led1, HIGH);
    }

    if (blueToothData == ‘b’) {
    digitalWrite(led1, LOW);
    }

    if (blueToothData == ‘c’) {
    digitalWrite(led2, HIGH);
    }

    if (blueToothData == ‘d’) {
    digitalWrite(led2, LOW);
    }
    }

    I know, it’s quite rough, but it works…
    Indeed, everything works fine except the fact I have to program the master without these lines:

    // data ********************************************************

    blueToothSerial.write(s1);
    delay(10);
    blueToothSerial.write(s2);
    delay(10);

    After resetting, so the modules go pairing fine, I finally upload the complete sketch with the lines above.
    This procedure is needed every time I turn off any of the Xbee carriers.

    Any idea to solve this problem or any suggestion to improve the sketch or the entire system?
    Thanks in advance ;)

  6. Oi guilherme, estou com duas placas arduino, ambas com microcontrolador Atmega 328 , uma ethernet e a outra com 4 relés , estou precisando acionar da placa ethernet (um site) os relés da outra placa e a comunicação deverá ser por pelo xbee, só que sou iniciante ainda , você poderia me ajudar no desenvolvimento dos codigos das placas e na configuração dos Xbees??

    abraço

  7. olá Luiz, de momento não estou a ver como solucionar o teu problema, porque não tentas colocar a tua questão no forum do Arduino? tem lá uma secção em português!
    boa sorte ;)

Leave a Reply

Your email address will not be published. Required fields are marked *