Pages: [1]   Go Down
Author Topic: serial communication help!  (Read 379 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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 smiley
Code:
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;
      }
      }
Logged

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

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    Serial.begin(9600);
This should only be in void setup(), not in loop().

Code:
    input=Serial.read();
You should not be calling Serial.read() unless Serial.available() is greater than 0. 

Code:
    if(input == A);{digitalWrite(2, HIGH);}
    inputA = 1;
Your if-statement does nothing because of the semicolon.  The correct syntax might be:
Code:
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:
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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, you have been very useful!
I am a newbie and I still have much to learn! smiley-grin

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

Pages: [1]   Go Up
Jump to: