Go Down

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


Jan 30, 2013, 03:31 pm Last Edit: Jan 30, 2013, 03:41 pm by AWOL Reason: 1
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';
   if (strcmp(command, "11") == 0){

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.


Moderator edit: Italic tags swapped for CODE TAGS


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.
"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.


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?
The art of getting good answers lies in asking good questions.


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

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131