Hi,
I try to find a simple toggle code.
I used the code explaining boolean operators at http://arduino.cc/de/Reference/BooleanVariables
and inly changes the pin numbers to be able to use the internal leds.
But I don´t get a toggle switch (one buttonpress=led on, next press=led off),
but a blinking led.
Any ideas what´s wrong?Many thanks
klaus
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
EDIT: I just realized that I did not insert the code which I tried, but the blink example.
Here is what I tried out:
int LEDpin = 5; // LED an pin 5
int switchPin = 13; // Taster an Pin 13, andere Seite auf Masse
boolean running = false;
void setup()
{
pinMode(LEDpin, OUTPUT);
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH); // pullup-Widerstand einschalten
}
void loop()
{
if (digitalRead(switchPin) == LOW)
{ // Schalter wird gedrückt - der Pin wird im Normalzustand durch den Pullup auf HIGH gezogen
delay(100); // Verzögerung um den Taster zu entprellen
running = !running; // Wert der Variablen hin- und herschalten (toggle)
digitalWrite(LEDpin, running) // indicate via LED - Anzeige über LED
}
}
daviddd:
But I don´t get a toggle switch (one buttonpress=led on, next press=led off),
but a blinking led.
Any ideas what´s wrong? Many thanks
It is because you are using the example for a blinking LED. There is no code to read a button. You probably want to start with a button debounce example:
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the latest stable state of the input pin
void setup() {
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH); // turn on the internal pull-up
pinMode(ledPin, OUTPUT);
}
void loop()
{
static int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
static unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
const unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int currentButtonState = digitalRead(buttonPin);
if (currentButtonState != lastButtonState) {
lastDebounceTime = millis();
lastButtonState = currentButtonState;
}
if ((millis() - lastDebounceTime) > debounceDelay &&
currentButtonState != buttonState) {
// We have a new and different stable button state
buttonState = currentButtonState;
if (buttonState == LOW) {
// The new state is "Pressed"
ledState = !ledState; // Toggle LED
digitalWrite(ledPin, ledState);
}
}
}
//zoomkat LED button toggle test 11-08-2012
int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;
void setup()
{
pinMode(13, OUTPUT); //LED on pin 13
pinMode(button, INPUT); //arduino monitor pin state
digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}
void loop()
{
press = digitalRead(button);
if (press == LOW)
{
if(toggle)
{
digitalWrite(13, HIGH); // set the LED on
toggle = !toggle;
}
else
{
digitalWrite(13, LOW); // set the LED off
toggle = !toggle;
}
}
delay(500); //delay for debounce
}
Buttons /switches don't always make a single contact on-> off, most of the time theybounce eg on-> off ->on ->off -> on ->off. So you need to get rid of the bounce.