Pages: [1]   Go Down
Author Topic: How to read a "button"?  (Read 394 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need close the circuit touching two wires, so, I take to print what was read, via Serial.println(), the problem is, it's reading wrong (prints 1 and 0 in different positions), I don't know what do, check the images. After I touch both wires, so it shows only 0. Below there is the code:

Code:
int pedal = 6;
int led = 13;
int pedalInput = 0;

void setup() {
  Serial.begin(9600);
  pinMode(pedal, INPUT);
  pinMode(led, OUTPUT);
}

void loop() { 
 
  pedalInput = digitalRead(pedal);

  if(pedalInput == HIGH) {
    Serial.println("1");
    digitalWrite(led, HIGH);
  } else {
    Serial.println("0");
    digitalWrite(led, LOW);
  }
}

What can I do to fix this problem?

Thanks so much!!!


* Bug.jpg (23.13 KB, 457x377 - viewed 31 times.)

* Bug_OnTouch.jpg (23.7 KB, 456x375 - viewed 28 times.)

* Bug_Scheme.jpg (140.17 KB, 596x656 - viewed 27 times.)
Logged

BCC AZ USA
Offline Offline
Edison Member
*
Karma: 36
Posts: 1242
It gets hot so it must be working
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With the two wires not touching you have a "floating input".  The input is very high impedance so will pick up noise.  When you touch the wires the input is pulled to ground.  Change  the line "pinMode(pedal, INPUT); to pinMode(pedal, INPUT_PULLUP);
Now when the wires are not touching the input is held high by the internal pullup resistor and will read 1,  Touch the wires for 0.


* dig sw.jpg (43.96 KB, 760x510 - viewed 28 times.)
« Last Edit: December 19, 2013, 07:29:26 pm by groundfungus » Logged

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

Know it's saying that  'INPUT_PULLUP' was not declared in this scope
Logged

BCC AZ USA
Offline Offline
Edison Member
*
Karma: 36
Posts: 1242
It gets hot so it must be working
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What version of the IDE are you using?  Go back to pinMode(pedal, INPUT); and add digitalWrite(pedal. HIGH): after.  That is how to enable internal pullups for an older version of the IDE.
Logged

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

Wow man, it's working, thanks!!! I am using an older version.
Logged

Pages: [1]   Go Up
Jump to: