Pages: [1]   Go Down
Author Topic: [Newb] DigitalRead - 1's & 0's in my "0's function" :S  (Read 362 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Greetings,
        New guy here, so first things first. Hello!  smiley-lol

        I picked up a Arduino Uno yesterday. So far I've been having a lot of fun messing around, trying to get feel for the basics. I didn't suspect it would be so addicting. smiley-grin

       I'm trying to make a small led light show for my first project but I'm having problems with digitalRead. The layout is very basic right now as I'm trying to plan. So I have two separate circuits. The button circuit from 5v to GND, which is the input when pressed. The other circuit is a simple led (pin 13, resistor, GND).

        The Serial Monitor is displaying 1's & 0's randomly when the button is off resulting in a low flicker of the led. Then all 1's when it is pressed, which is correct.

        In all honestly, I don't know if this is a programming or hardware issue. I've been trying different arithmetic to make it all 0's/off but to no avail (= 0 LOW, = 1 HIGH). If you have any suggestions they would be much appreciated.



Code:
//

int led = 13;
int in = 2;

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

void loop()
{
  int state = digitalRead(in);
  Serial.println(state);
  delay(1);
 
  if (state > 0)
  {
    digitalWrite(led, HIGH);
  }
 
  else (state < 0);
  {
    digitalWrite(led, LOW);
  }
}


* Untitled.png (82.65 KB, 898x729 - viewed 6 times.)
Logged

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

Greetings,

The Serial Monitor is displaying 1's & 0's randomly when the button is off resulting in a low flicker of the led.

The pin is "floating".  You need a "pull down" (aka "pulldown") or "pull up" (aka "pullup") resistor.  Tutorial explaining...

http://www.ladyada.net/learn/arduino/lesson5.html
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4302
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It sounds like the input pin is floating.
Try this
Code:
  pinMode(in, INPUT_PULLUP);
It will keep the input HIGH when the button is not pressed.  Arrange your circuit to connect the pin to GND when the button is pressed
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

Quote
The button circuit from 5v to GND,
If you actually did that you would short out the power supply.

My take on inputs:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19015
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My write-up on handling switches:

http://www.gammon.com.au/switches
Logged


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

WOW! quick to respond!

Quote
The button circuit from 5v to GND,
If you actually did that you would short out the power supply.

My take on inputs:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Yeh, typo. It was supposed to be 5v > Button > Input Pin.


My write-up on handling switches:

http://www.gammon.com.au/switches


That's amazing! I'm getting solid numbers between states now after using the resistors.

It was the internal resistor on digital side GND that was throwing me off. As when I used it in the tutorials, but on my project I used the power side GND's.

Thanks for the advice and resource share Nick!
Logged

Pages: [1]   Go Up
Jump to: