Go Down

Topic: [Newb] DigitalRead - 1's & 0's in my "0's function" :S (Read 383 times) previous topic - next topic

dritchie0042

Greetings,
        New guy here, so first things first. Hello!  XD

        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. :D

       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: [Select]

//

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);
  }
}

Coding Badly

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

UKHeliBob

It sounds like the input pin is floating.
Try this
Code: [Select]
  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
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Grumpy_Mike

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

Nick Gammon


dritchie0042

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!

Go Up