Pages: [1]   Go Down
Author Topic: trouble about void loop in Arduino  (Read 380 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I have programmed an Arduino Leonardo with the following code:

Code:
#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
« Last Edit: January 30, 2013, 09:41:19 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26224
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3008
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: