Pages: [1]   Go Down
Author Topic: Basic digitalRead question  (Read 358 times)
0 Members and 1 Guest are viewing this topic.
Baltimore
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all, I apologize for the dumb question but... I just got my arduino Uno today (hi everybody!) and am familiarizing myself with the language. I actually have quite a bit of experience programming in python, c and c++, but I'm not a programmer by trade. I just wanted to modify the example DigitalReadSerial code. It works just fine with the original code (see the Examples section), but I tried to modify it so that it wouldn't display all those 0s when the switch is not depressed, I only want it to display the 1s when I press the switch, so here is what I changed:

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
}

void loop() {
  int sensorValue;
  sensorValue = digitalRead(2);
  if ( sensorValue = HIGH) {
    Serial.println(sensorValue, DEC);
  }
}

Problem is: I was expecting the serial monitor to display nothing until I pressed the switch, and then I thought it'd start outputting 1s; instead, all it does is only display 1s, all the time, even when the switch is not depressed. I'm sure it's something very simple, I'm just not sure what I'm doing wrong....
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12289
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

  if ( sensorValue == HIGH) {
Logged

0
Offline Offline
Shannon Member
****
Karma: 162
Posts: 10523
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The be specific the '=' operator always assigns a value to a variable  and returns that same value as its result.  It has nothing to do with 'equals' comparison at all.  The '==' operator does comparison and returns true or false.  Its very easy to be caught out by this, mainly because you are allowed to use '=' in an expression.

The plus side is that you can write:
Code:
  a = b = c = 0 ;
Which is interpreted as if it were:
Code:
  a = (b = (c = 0)) ;

So your code that did if ( sensorValue = HIGH) was setting sensorValue to HIGH then returning HIGH (which is equivalent to true) as the if-test.
Logged

[ I won't respond to messages, use the forum please ]

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

Dang. Just like Python.  smiley-red I'll go sit in a corner and write "I will not ask stupid questions" 1,000 times. Thanks!

OK, so the digitalRead function returns either 0/1 or LOW/HIGH then? One could use either, and even if you declare it as an integer, you can still manipulate it as high or low as opposed to 1 or 0?
« Last Edit: April 04, 2011, 09:14:41 pm by OlivierG » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12289
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, so the digitalRead function returns either 0/1

Yes.

Quote
or LOW/HIGH then?

Yes.

Quote
One could use either

Yes.

Quote
and even if you declare it as an integer, you can still manipulate it as high or low as opposed to 1 or 0?

Yes.
Logged

Pages: [1]   Go Up
Jump to: