Pages: [1]   Go Down
Author Topic: my pins give me 1 all the time !  (Read 555 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I got my first arduino uno last week. I had a problem and I thought it's because of bouncing but It's getting so annoying can anyone help me.

here is the problem :

I made the switch circuit with 2 wires and with a 10K resistor . The diagram is like this

http://www.codeproject.com/KB/system/Interfacing_Arduino_LCDs/PushButtonSchematic.png

except there is not 100ohm resistor.

when I connect the wires I should see 1 on the serial monitor and when I disconnect them 0 .
but it’s always 1 !
when I connect the wires i it goes to 0 and 1 for a couple of times  and then it becomes 1. when I disconnect it it stays at 1 for a a few seconds and it goes to 0 sometimes and sometimes doesn't. 

this the simple program that I wrote to light a led when the switch is pushed is like this:
//

#define enc 8
#define led 13
int count = 0 ;

void setup()

{
pinMode(enc,INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
}

void loop()
{
int val = digitalRead(enc);
if (val ==HIGH)
{
digitalWrite(led,HIGH);
Serial.println(val);
}
else{
digitalWrite(led,LOW);
}
}

I thought maybe it’s because of bouncing. because it was like this since the first day that I got the Arduino.
but today I had problem in counting pulses from encode and I was told it's because of my pins has problem.
 there any other way to check the pins ? it’s UNO btw.

can anyone help me ?
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could be that your only writing to the serial monitor when (val ==HIGH)
Logged


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

right, I should put the Serial.print out of the if statement .

but the problem is when the wires are not connected, I still get 1 which is weird. it should be 0 .
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

right, I should put the Serial.print out of the if statement .

but the problem is when the wires are not connected, I still get 1 which is weird. it should be 0 .
Or put a print statement in the "else" clause of your if-statement,  otherwise you only print when it is high.
Logged

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

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

None of your image links work.

If you click "additional options" you can attach pictures in the post editor.
Logged

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

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

I made the code simpler .
Code:
int button = 12;

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

}

void loop()
{
  int val = digitalRead(button);
  Serial.println(val);
}

when I upload the program, I get the bunch of zeros which is normal

when i connect the wire I get 1, which is normal.
but when disconnect the wire, I get 1 for almost 5 seconds and then the value starts to bounce between 0 and 1 for 3 4 more seconds and then it goes to zero.

one other thing which seems weird is when I place a wire which is connected to nowhere to the pin 12. I get the uploaded  picture number 1 !!!!!! and when I take it out, for couple of seconds I get picture number 2. and then it goes to all zero.
it's so weird. the wire is connected to nowhere !!!!



* 1.png (25.34 KB, 654x390 - viewed 16 times.)

* 2.png (22.22 KB, 645x378 - viewed 16 times.)
Logged

Leeds, UK
Online Online
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop()
{
  int val = digitalRead(button);
  Serial.println(val);
  delay(10); //I bet that cures the problem.
}
Logged

~Tom~

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27113
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As written, there is nothing to make the pin go High once the connection to Ground is released.

Do this instead:
Code:
void setup()
{
    pinMode(button,INPUT);
    digitalWrite (button, HIGH); // turns on internal pullup resistor
    Serial.begin(9600);

}

Now val will reliably = HIGH immeditately whenever the switch is not pressed, and LOW when it is pressed.

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: