Go Down

Topic: serial communication help! (Read 488 times) previous topic - next topic

teoditomozzo

i'm 14 and i started to programming with arduino from 2 days and i have this problem for the first time...
the led remain turned on and don't have a state change can you help me please? thank you :)
Code: [Select]
int A;
int B;
int C;
int D;
int inputA;
int inputB;
int inputC;
int inputD;
int input;
void setup(){
  Serial.begin(9600);
  pinMode (2, OUTPUT);
  pinMode (3, OUTPUT);
  pinMode (4, OUTPUT);
  pinMode (5, OUTPUT);
  }
  void loop(){
    Serial.begin(9600);
    input=Serial.read();
    if(input == A);{digitalWrite(2, HIGH);}
    inputA = 1;
    if(input == B);{digitalWrite(3, HIGH);}
    inputB = 1;
    if(input == C);{digitalWrite(4, HIGH);}
    inputC = 1;
    if(input == D);{digitalWrite(5, HIGH);}
    inputD = 1;
    if(input == A and inputA == 1);{
      digitalWrite(2, LOW);
      inputA = 0;
      }
        if(input == B and inputB == 1);{
      digitalWrite(3, LOW);
      inputA = 0;
      }
        if(input == C and inputC == 1);{
      digitalWrite(4, LOW);
      inputC = 0;
      }
        if(input == D and inputA == D);{
      digitalWrite(5, LOW);
      inputD = 0;
      }
      }

AWOL

Lose the semicolons at the ends of your if statements
"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.

James C4S

Global integer variables are initialized to zero.  So A, B, C, D, inputA, inputB, inputC, inputD, and input are all 0.

It doesn't look like you have gone through any of the tutorials.  Your code has numerous problems.

Code: [Select]

    Serial.begin(9600);

This should only be in void setup(), not in loop().

Code: [Select]

    input=Serial.read();

You should not be calling Serial.read() unless Serial.available() is greater than 0. 

Code: [Select]

    if(input == A);{digitalWrite(2, HIGH);}
    inputA = 1;

Your if-statement does nothing because of the semicolon.  The correct syntax might be:
Code: [Select]

if (input == A)
   {
      DigitalWrite(2, HIGH);
   }

Lines like "inputA = 1" and inputB = 1" execute every time.  Did you mean to put them inside of the if-clause?  Also, in your if-statement "input == A" doesn't make sense to me.  A, B, C, and D are all initialized to 0.  Why are you always comparing the incoming byte to 0?

You need to clean up at least those problems before you can go much farther.  Regardless of what you see the LED doing, I am very certain this code isn't doing anything you expect it to be doing.  This is a great example of where comments would be helpful.

e.g.

Code: [Select]
if(input == A);{digitalWrite(2, HIGH);}  // if the letter A was typed, turn on the LED on pin 2
Comments like those help to indicate to someone else that the code you wrote doesn't do that.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

teoditomozzo

Thank you, you have been very useful!
I am a newbie and I still have much to learn! :D

P.s Sorry for the incorrect sentences, I'm Italian and I wrote only what I know to write: P

Go Up