Pages: [1]   Go Down
Author Topic: if/else question  (Read 647 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
i have a web server on my arduino duemilanove, and with this code, i get printed both "feedback high" and "feedback error"
Code:
 
  feedbackin = digitalRead(feedback);
 
  if (feedbackin == HIGH){
    client.println ("<font color='green' size='5'>Feedback high</font>");
  }
 
  if (feedbackin == LOW) {
    client.println ("<font color='grey' size='5'>Feedback low</font>");
  }
 
  else {client.println ("<font color='red' size='5'>Feedback ERROR</font>");
  Serial.println ("FEEDBACK ERROR");
  Serial.println (feedbackin);
  Serial.println ("");
  }
gives output
"Feedback high Feedback ERROR"

anyone that have a clue on why this happen?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes it is working exactly as you wrote it.
If it is high then the first if gets true and the second is false so the else kicks in.
Therefore you get both high and error.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 72
Posts: 3763
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Feedbackin is evidently high, so your first if prints Feedback high

In your second if, Feedbackin isn't low, so it executes the else part, emitting Feedback Error

perhaps you wanted
Code:
else if (feedbackin == LOW) {

But since digitalRead will only ever give you high or low, I'm struggling to see what the else clause is really for.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

aha... i did put in the error part to see if the input was floating... because i was experimenting with inputs...

thanks for the help  smiley-cool
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

digitalRead will always return either HIGH or LOW (look at the source code). If the pin is left floating, it will return a random value.
Logged

Pages: [1]   Go Up
Jump to: