I am working on a project to link a two way radio to a phone and i need the arduino to carry out a simple task to make it all work, If you would like more information i can certainly explain.
But I am stuck with a bit of code
Basicly I am using the debounce code which i have modified to include an extra led.
according to the code below.
I want outPin13 to go high when i push the button
At the same time
I want outPin12 to go high (only for say 2 seconds) and then go low
When i push the button again i want outPin13 to go low (as outPin12 is already low)
I have tried every thing i can think of under " digitalWrite(outPin13, state); " but nothing is working
I hope that makes sense. Any help would be greatly appreciated I also hope i haven't broken any rules here as i am new to this stuff.
/* switch
*
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).
*/
int inPin = 2; // the number of the input pin
int outPin13 = 13; // the number of the output pin
int outPin12 = 12;
int state = LOW; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = HIGH; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
I have tried every thing i can think of under " digitalWrite(outPin13, state); " but nothing is working
Then, perhaps the problem is hardware related. You are not using the internal pullup resistors, and the logic of the code implies pulldown resistors. So, do you actually have external pulldown resistors wired with the switch? How IS the switch wired?
Its not hardware related it is wired up correctly, its just a matter of getting outPin12 to turn off after those two seconds once the button is pushed while outpin13 stays on.
I have tried every thing i can think of under " digitalWrite(outPin13, state); " but nothing is working
Its not hardware related it is wired up correctly, its just a matter of getting outPin12 to turn off after those two seconds once the button is pushed while outpin13 stays on.
Which is it?
If you want to turn pin 12 on, and off after a while, do that. You need to record when the LED is turned on (in an unsigned long, using millis()). Then, on every pass through loop, see if it is time to turn the light off. (now - then > interval). If it is, turn it off.
I want outPin13 to go high when i push the button
At the same time
I want outPin12 to go high (only for say 2 seconds) and then go low
Stop trying to control the LEDs directly in code. Instead, create state variables for each (such buttonLEDState and delayLEDState or whatever makes sense to you.)
in the beginning of loop() always set the LEDs to those state variables:
Now using if-statements and millis() determine the conditions for what those state variables should be. For example, when you detect a button press, then invert the value of buttonLEDState (which is what I think you are trying to do.) At the same time, turn on delayLEDState, record the number of millis(), and wait two seconds to turn the LED off.
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).
David A. Mellis
21 November 2006
*/
int inPin = 2; // the number of the input pin
int outPin13 = 13; // the number of the output pin
int outPin10 = 10;
int state = LOW; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
digitalWrite(outPin13, state);
}
if (state==HIGH) {
digitalWrite(outPin10, state);
} else
digitalWrite(outPin10, LOW);
previous = reading;
}
how do i get outPin10 to turn off after 2 seconds?
At pin 2, did you connect the switch that way ---> +5 - a switch ( on/off or push on ) - to pin 2 - 1 k to 10 K resistor - GND <---
Yes or No ?
if (state==HIGH)
{
digitalWrite(outPin10, state);
delay(2000); // <---- your 2 second - pin 10 turn On for 2 second if state is HIGH
}
else
{ // <--- You forgot the bracet
digitalWrite(outPin10, LOW);
} // <-- You forgot the bracet