I have a toggle switch and want it to act like a button. When the switch is on, the Serial.print, rapidly prints it and when it's off, nothing happens. But I want it to print once when the switch is on.
Thanks
int SwitchPin = 2; // Switch connected to digital pin 2
int val = 0; // variable to store the read value
void setup()
{
pinMode(SwitchPin, INPUT); // sets the digital pin 2 as input
Serial.begin(9600);
}
void loop()
{
val = digitalRead(SwitchPin); // read the input pin
if (val == 1)
{
Serial.print("On or Off");
}
}
Don't use val as the "shall I print again" indicator, do something like this to ensure that you only print once
const byte SwitchPin = 2; // Switch connected to digital pin 2
byte val = 0; // variable to store the read value
boolean okToPrint = true;
void setup()
{
pinMode(SwitchPin, INPUT); // sets the digital pin 2 as input
Serial.begin(9600);
}
void loop()
{
val = digitalRead(SwitchPin); // read the input pin
if (val == HIGH && okToPrint)
{
Serial.print("On or Off");
okToPrint = false;
}
}
Note the changes I made to the variable declarations to save memory. Have you got a pulldown resistor on the switch input to hold it LOW when not activated ?
Sadly neither of the replies worked. Just to clarify, when the switch is up, I want nothing to happen, but when its flicked down, I want it to give my computer an input, like a keyboard. And when it's flick back up I want it to give the same input.
jAMDup:
Sadly neither of the replies worked. Just to clarify, when the switch is up, I want nothing to happen, but when its flicked down, I want it to give my computer an input, like a keyboard. And when it's flick back up I want it to give the same input.
Sigh... I'll try some mind reading here... you want it to register a key event whenever the switch is toggled, regardless of whether it is currently up or down?
As in... the switch is down... I click it up, it says, "hey, the switch moved". I click it back again, it says, "hey, the switch moved"?
/*
Debounce
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
a minimum delay between toggles to debounce the circuit (i.e. to ignore
noise).
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground
* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.
created 21 November 2006
by David A. Mellis
modified 30 Aug 2011
by Limor Fried
modified 28 Dec 2012
by Mike Walters
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Debounce
*/
// 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 current reading from the input pin
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.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop()
{
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState)
{
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState)
{
buttonState = reading;
Serial.println("On or Off");
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH)
{
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
You do not need the +5 volt connection on the switch as the internal pull-up resistor is enabled.
I assume the wires are soldered at the switch terminals.
Using the sketch I attached, tell us what is happening on the serial monitor when you flip the switch in one direction then the other.
.