I'm using an Arduino Uno. When I connect my Arduino to power (computer USB port) the LED automatically starts blinking. The pushbutton also does nothing. I thought it may be because there's no "debounce", but I don't really know how to implement that. All help would be appreciated.
int ledG = 5;
int btn = 4;
int btnState;
bool ledOn; // is green led on
void setup() {
pinMode(ledG, OUTPUT);
pinMode(btn, INPUT);
digitalWrite(ledG, LOW);
ledOn = false;
}
void loop() {
if (digitalRead(btn) == LOW) { //if button pressed
btnState = LOW; //change btnstate to low
} else {
btnState = HIGH; //else change btnstate to high
}
if (btnState == LOW && ledOn == false) { //if button pressed(low) and led isnt on
ledOn = true; // change ledon to true
for (;;) { //blinking led infinitely
digitalWrite(ledG, HIGH);
delay(900);
digitalWrite(ledG, LOW);
delay(900);
}
}
if (btnState == LOW && ledOn == true) { //if button pressed and ledon is true
ledOn = false; //ledon is false now
digitalWrite(ledG, LOW); //disable led
}
}
//automatically turns on and doesnt stop when button pressed
(I have no idea if it matters, but my pushbutton is black. The black one was broken in the program I'm using.)
I know that I linked GND wrong, but it was a mistake when making the schematic.
The command: for ( ; ; ) {
tells it to just loop for ever, so as soon as this starts nothing you do will stop it
If you want it just to blink a few times then you could change it to: for (int i = 0; i < 3; i++) {
Also, the GND wire in the diagram is not linked to the other blue wires (at least if the breadboard is a standard one)
The button switch and the current limit resistor of the LED both connect to the + rail. The ground from the Arduino connects to the - rail. The + and - rails are not connected. How do the LED and switch see ground?
Did you change that to INPUT_PULLUP? If not the switch input is floating when open. A floating input's state is indeterminate (random).
Have you read the replies so far? Do you understand what you have been told?
Well, sorry about that. I want to use a pushbutton as a toggle button. Basically one press, LED turns on, another press, it turns off, next one it turns on, and... well I think you can get my point now.
Yes, I did read the replies and change btn's pinMode to INPUT_PULLUP. Also replaced
if (btnState == LOW && ledOn == false) { //if button pressed(low) and led isnt on
ledOn = true; // change ledon to true
for (;;) { //blinking led infinitely
digitalWrite(ledG, HIGH);
delay(900);
digitalWrite(ledG, LOW);
delay(900);
}
}
if (btnState == LOW && ledOn == true) { //if button pressed and ledon is true
ledOn = false; //ledon is false now
digitalWrite(ledG, LOW); //disable led
}
}
with
if (btnState == LOW && ledOn == false) { //if button pressed(low) and led isnt on
ledOn = true; // change ledon to true
digitalWrite(ledG, HIGH); //turn on led
}
if (btnState == LOW && ledOn == true) { //if button pressed and ledon is true
ledOn = false; //ledon is false now
digitalWrite(ledG, LOW); //disable led
}
But now, I didn't write a very important thing. I'm trying to use a pushbutton as a toggle button. I thought I'd need to implement a debounce timer, but I'm having trouble with it.
Also, I've shot myself in the foot again, everything is correctly connected with ground, I'm just wacky at doing these schematics.
Here is how to toggle the state of an output with a momentary switch using the state change detection method, active low. Change the pins to match your setup. If you will notice there are NO delay() calls in this code. It is not blocking code like is yours with delays.
// by C Goulding aka groundFungus
const byte buttonPin = 12; // the pin that the pushbutton is attached to
const byte ledPin = 13; // the pin that the LED is attached to
bool buttonState = 0; // current state of the button
bool lastButtonState = 0; // previous state of the button
void setup()
{
// initialize the button pin as a input with internal pullup enabled
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 50; // check switch 20 times per second
if (millis() - timer >= interval)
{
timer = millis();
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the new buttonState to its previous state
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
// if the current state is LOW then the button
// went from off to on:
digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the output
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}
That is NOT a schematic. It is, at best, a diagram. The difference is in the details which are absent.
If you want to create schematics, this tutorial will get you started.