Understanding serial data

Hi

This is my first post and I'm a noob to programming so take it easy XD .

Running into issues as I don't fully understand programming (probably a to big project for me) but if someone puts me on the right track I am sure Ill be able to make it work.

Commands that I receive from my Iphone (using Mote app can send custom commands over TCP/ip) via the wifi shield (dfrobot shield so the input from the shield is serial) are a string of ascii characters if I use int/String I get 11410110011111013 and if I use byte or char I get "redon" I was wondering on how to compare this to a set value.

I can switch one led on and off but when I try to control multiple leds it seems that arduino sees two different commands as the same (eg redon switches both leds on and greenon switches both leds on same thing for redoff and greenoff ).

Here is my code:

#define LEDGREEN 12
#define LEDRED 11

byte inByte='0';
byte ReOn = 'redon';   
byte ReOff = 'redoff';
byte GrOn = 'greenon';
byte GrOff = 'greenoff';

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(LEDRED,OUTPUT);
  pinMode(LEDGREEN,OUTPUT);
      

}

void loop() {
 if (Serial1.available()>0) {
   inByte = Serial1.read();
   Serial.print(inByte);
 }
 if (inByte==ReOn){
   digitalWrite(LEDRED, HIGH); 
 }
 if (inByte==ReOff){
   digitalWrite(LEDRED,LOW);
 }
 if (inByte==GrOn){
   digitalWrite(LEDGREEN, HIGH); 
 }
 if (inByte==GrOff){
   digitalWrite(LEDGREEN,LOW);
 }
}

Here is another piece of code. With this code I get a an error:

IPHONE_WIFI_ARDUINO_experiment.cpp: In function 'void loop()': IPHONE_WIFI_ARDUINOexperiment:30: error: duplicate case value IPHONE_WIFI_ARDUINOexperiment:22: error: previously used here IPHONE_WIFI_ARDUINOexperiment:34: error: duplicate case value IPHONE_WIFI_ARDUINO_experiment:26: error: previously used here :

#define LEDGREEN 12
#define LEDRED 11




void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(LEDRED,OUTPUT);
  pinMode(LEDGREEN,OUTPUT);
      

}

void loop() {
 if (Serial1.available()) {
   char inByte = Serial1.read();
   
   Serial.print(inByte);
   
   switch(inByte){
     case 'redon':
     digitalWrite(LEDRED,HIGH);
     break;
     
     case 'redoff':
     digitalWrite(LEDRED,LOW);
     break;
     
     case 'greenon':
     digitalWrite(LEDGREEN,HIGH);
     break;
     
     case 'greenoff':
     digitalWrite(LEDGREEN,LOW);
     break;
   }
 }
}

Any help would be much appreciated. I'm also willing to learn anything I can so any comments will always be helpful :). Jon Czudek

P.S. Let me know if need to know anything else

byte ReOn = 'redon'; this is a array of bytes, not a byte try byte ReOn = 'r'; like in byte inByte='0';

and at the switch condition use the same

switch(inByte){ case 'r': digitalWrite(LEDRED,HIGH); break;

Hi gvi70000

Thanks for the quick reply I'll try what you said but is it possible to compare to an array of bytes if that makes any sense. :D

it is possible to compare two byte arrays, but you can't use this in switch statement

Serial1.read() will read one byte at time from serial buffer, you need to know the length of the serial package in order to use a byte array and get intelligible data from serial.

Try to read about serial communication on arduino and have a better understanding how it works

is it possible to compare to an array of bytes

Have a look at the strcmp() and memcmp() functions.

But you can't use them directly in a case statement.


Rob

Hi

Thank you all for the info.

giv70000: Every time I send a command from my iphone what I see in my serial monitor is the exact same command that I send with my iphone (eg "redon" appears redon in my serial monitor).Does that mean that length of the serial package is [6]. See the attached file for a screen sot of what I'm getting. I have read the info on the arduino site about serial although as I'm a complete noob I try to implement the things that I read but it doesn't always work, that's why I hope I'll be able to get somethings explained from a different perspective. XD

Graynomad: Thanks I'll try to search for some more info about strcmp() and memcmp(). Would I be right to say that these functions can be used in an if/else statement

Thanks again for taking the time to explain some of these things to me. Jon Czudek

Sorry here is the attachment:

That screen shot implies to me that the iphone is sending a CRLF after each string, ie "redonCRLF".

Try doing

Serial.println (inbyte, HEX)

to see exactly what is being sent (post the results)

You can use those funcs in an if() statement

if (strcmp (string1, string2) == 0) ... // the strings are equal

Whether or not that's the right way to handle your program is another matter. I'd be inclined to send a single character, say

r = red off R = red on g - green off G = green on

Then your (slightly modified) switch statement will work (all other characters such as CR and LF will be ignored).


Rob

Hi Rob

In the iPhone app I can choose whether or not I want to send terminators. I don't know how that sloped my mind using upper and lower case thanks for the idea as for the strcmp() I'll post my findings and for the Serial.println (inbyte, HEX).

Thanks for your help Jon Czudek

Hi Rob

The switch case works with using the upper and lower case as I wanted it to, but I wouldn’t mind getting the strcmp() function working, in the attachment you’ll see the error I’ve run into.

Thanks alot for your help (such a simple solution that slipped my mind).
Jon Czudek

strcmp() is for comparing two strings, meaning a NULL-terminated series of chars.

You are trying to compare a byte (ReOn) with a string ("redon") and the compiler doesn't like it.

How on earth this

byte ReOn = 'redon'; byte ReOff = 'redoff'; byte GrOn = 'greenon'; byte GrOff = 'greenoff';

even compiles I have no idea, but it's not valid code AFAIK.

If you want to use strings then declare them like this

char ReOn[] = "redon";


Rob

If you want to use strings then declare them like this

char ReOn[] = "redon";

You also need to understand that you are reading one character at a time from the serial port, not a whole string.

Search for jraskell's post on reading from the serial port, and read his blog. It has all the answers you are searching for.

Hi

Thank you all your input and answers to my problems, I definitely learnt alot today.

Thanks
Jon Czudek

P.S. Hopefully I won’t be a noob forever XD

Hi PaulS

Just wondering if you could post the link to jraskell's post on reading from the serial port as I'm having difficulty finding it

Thanks Jon Czudek

Just wondering if you could post the link to jraskell's post on reading from the serial port as I'm having difficulty finding it

http://arduino.cc/forum/index.php/topic,65346.msg477853.html#msg477853

Thanks much appreciated