Go Down

Topic: trouble about void loop in Arduino (Read 414 times) previous topic - next topic

denyan

Jan 30, 2013, 03:31 pm Last Edit: Jan 30, 2013, 03:41 pm by AWOL Reason: 1
Hi,
I have programmed an Arduino Leonardo with the following code:

Code: [Select]
#include <stdio.h>
#include <Wire.h>

#define RLY08_ADDRESS1 0x38

int i;
char command[3];


void loop() {
 if (Serial1.available() > 0) {
   
   for (i=0; i<2; i++){
     command[i] = Serial1.read();
   }
   command[2] = '\0';
 }
 delay(100);
 
 
   if (strcmp(command, "11") == 0){
     Wire.beginTransmission(0x38);
     Wire.write(0x00);
     Wire.write(0x65);
     Wire.endTransmission();
     delay(500);
     Wire.beginTransmission(0x38);
     Wire.write(0x00);
     Wire.write(0x6F);
     Wire.endTransmission();
   }
   
     
 }


The question is:
I receive, through serial communication, 2 bytes and if the bytes are "11" in unsigned char format the if condition (strcmp(command, "11") == 0)is executed and it works fine but the execution of the instruction is one time only.
If I receive any bytes from serial communication, the  if instruction (strcmp(command, "11") == 0) is not executed more. Why? the char command[2] contain the "11" values always.


Regards,


Moderator edit: Italic tags swapped for CODE TAGS

AWOL

Code: [Select]
if (Serial1.available() > 0) {
   
    for (i=0; i<2; i++){
      command[i] = Serial1.read();
    }


I'm going to make up a keyboard macro for this one: See if there is at least one character available to read, then read both of them.
Wrong.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

You also need to tell us what is sending the data. If it is the Serial Monitor, what option do you have selected? Is it adding a carriage return, line feed, or both to the data?

michinyon

In addition to the problems already mentioned,   you never clear or empty the "command" array.

So if you get "11"  sent to you successfully one time,   it will always be there for every subsequent iteration
of loop().   Which may, or may not, be what you want.

Go Up