Serial data Transfer

I cant get the LED to Switch on or OFF
I am getting serial data but the If statements are giving me trouble
I know I'm missing something basic but...

<
char dataIn;
int P1LED = 0;
int P2LED = 0;
//volatile int P1Score = 0;
//volatile int P2Score = 0;

void setup() {
pinMode(8,OUTPUT); //Player 1 LED
pinMode(9,OUTPUT); //Player 2 LED
Serial.begin(9600);
}

void loop() {
if(Serial.available()){
dataIn = Serial.read();
Serial.print(dataIn);
}

if(dataIn == "77777"){
P1LED = (dataIn);
if (P1LED == "77777")
digitalWrite(8,HIGH);
}

if(dataIn == "66666"){
P1LED = (dataIn);
if (P1LED == "66666")
digitalWrite(8,LOW);
}
}

You've declared dataIn as a character. It will never be equal to a string. P1LED and P2LED are declared as integers. They will also never be equal to a string.

And please enclose your code in code tags as shown in How to get the best out of this forum.

Interesting code style.. some it may not be working the way you’re hoping it will

Unfortunately most languages are very strict in doing what you ask them to do.
Check up on the read() function, and data types.

Cool - Thanks I will have a study-up on "Strings"
Chears

No, not "Strings". "strings". As in character arrays. Strings are something completely different - and generally best avoided.

1 Like

Your absolutely correct - coding is very strict on formatting.

Will do - Thanks

I have done a fair bit of VBA coding but this "C+" is all very new to me
I try to work logically but as proven it doesn't always work out the way I expect

It is strict in syntax/semantics and NOT on formatting. For exampe:

String myData1;
in the above here myData1 is an object (variable) of type String which is a "user data type". You can apply method/function on myData1. For example:

  String myData1 = "Forum";
  char y = myData1.charAt(3);    //the method carAt() is applied on myData1
  Serial.println(y);   //shows: u

char myData2[] = "Forum";
In the above myData2 is an array whose elements are of type char. For example:

  char myData2[] = "Forum";
  char y2 = myData2[3];
  Serial.println(y2);   //shows: u

Hahaha - Oh boy I have so much to learn

Merge two threads about serial communications.

@Glen_Lewis, please keep related questions on one thread. The first provides good background for the second. Both questions are about "serial communications".

void loop() {
    if(Serial.available()){
        dataIn = Serial.read();
        Serial.print(dataIn);
    }
  • the read() returns a single char
    if(dataIn == "77777"){
        P1LED = (dataIn);
        if (P1LED == "77777")
        digitalWrite(8,HIGH);
    }
  • this code is invoked regardless if anything has been read. should it be inside the SerialAvailable() block
  • it compares the single char in dataIn to a multi-byte string "77777"
  • the code below does similar
    if(dataIn == "66666"){
        P1LED = (dataIn);
        if (P1LED == "66666")
        digitalWrite(8,LOW);
    }
}

what about

void loop() {
    if(Serial.available()){
        char c = Serial.read();
        Serial.print(c);

        if ('7' == c)
            digitalWrite(8,HIGH);
        else if ('6' == c)
            digitalWrite(8,LOW);
    }
}

Thanks for this
Is this code limited to 4 items? (A,B,234,567)
I have tried adding 2 more fields to both parts of the code (UNO1 and UNO2) (A,B,C,D,234,567)
I am getting funny readings on the 2nd serial monitor
Like A
B
C
-22
234 ...

You can have more comma seperated items.

What data items have you tried -- please, mention them?

Don't worry I worked it out :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.