# Simple arduino button... HELP

#### NorwegianNoob

Joined Aug 28, 2021
15
Setup:
Arduino UNO
Button pin 6 (regular pushbutton)
LED1 pin 2
LED2 pin 4

What i want to happen:
When sketch starts, both LEDs are low.
When i push and hold button, LED1 turns on and stays on for 10 seconds (Even if i hold the button pressed for longer).
When i release the button, LED2 turns on and stays on for 10 seconds.

Any ideas for me how to do this?

#### ericgibbs

Joined Jan 29, 2010
13,881
hi NN.
Welcome to AAC.
Is this a College or homework assignment question.?
E

#### NorwegianNoob

Joined Aug 28, 2021
15
This is more of a hobbyist question

#### ericgibbs

Joined Jan 29, 2010
13,881
hi,
greit

Do you have any Code written for this project.?
E

#### NorwegianNoob

Joined Aug 28, 2021
15
No i just tried modifying the "Button" example with no luck...I am a complete beginner at programming.

#### MrChips

Joined Oct 2, 2009
24,457
You are off to a good start by defining what you want to happen.
Now you need to add the unintentional actions.

1) What should happen if you release the button sooner than 10 seconds?
2) What should happen if you press and release the button multiple times within 10 seconds, 20 seconds?

#### NorwegianNoob

Joined Aug 28, 2021
15
To clarify i should go into more detail:

The button is not really a button, but i thought it would be easier to understand.
I have a voice activated power plug with a 5v adapter in it where positive goes to digital pin 6, and ground to ground on the arduino.
The power plug is off in the beginning of the sketch, so everything is LOW. When activated it would act like i push a button and hold it...

#### MrChips

Joined Oct 2, 2009
24,457
It is always preferred in the long run if you give the full details otherwise one would be addressing the wrong problem.

After you have defined the basic operation and all the possible events that can happen, the next step is to write out what you imagine should happen within a program, something like the following.

1) Initialize - set LED1 and LED2 off.
2) Poll the input signal
3) On the rising transition of the input signal, turn on LED1.
4) After 10 seconds, turn off LED1 and turn on LED2.
5) After 10 seconds, turn off LED2.
6} Go back to step 2.

Now go over this sequence and see if this satisfies all possible time sequences that might occur with the input signal.

#### NorwegianNoob

Joined Aug 28, 2021
15
1-3 looks fine but:
4) After 10 seconds turn off LED1
5) Digital pin 6 goes low when i turn off the power plug and LED2 goes on for 10 seconds.
6) Go back to step 2.

#### ericgibbs

Joined Jan 29, 2010
13,881
The power plug is off in the beginning of the sketch, so everything is LOW. When activated it would act like i push a button and hold it...
Hi.
Could you explain what 'activates'?

E

#### ericgibbs

Joined Jan 29, 2010
13,881
hi,
All Off. ie: UNO

Voice activated output applies a 5V momentary signal to Pin6 of the UNO.

If the UNO is Off, applying 5V signal to pin #6 will not turn On the UNO.

E

A simple drawing would help.

#### NorwegianNoob

Joined Aug 28, 2021
15
The Arduino itself will always be powered.

The LEDs are just until the program work, after that LED1 and 2 will be relays.

#### ericgibbs

Joined Jan 29, 2010
13,881
hi NN,
I will look at the project, see what Sketch will suit.
I assume you have all the materials, Arduino IDE, UNO and cables installed and assembled.?

E

#### NorwegianNoob

Joined Aug 28, 2021
15
I appreciate that, but i think i figured it out:

Working sketch:
int pin_LED1 = 2;
int pin_LED2 = 4;
int pin_switch = 6;

boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
byte state = 0;
void setup()
{
Serial.begin(9600);
pinMode(pin_LED1, OUTPUT);    digitalWrite(pin_LED1, LOW);
pinMode(pin_LED2, OUTPUT);   digitalWrite(pin_LED2, LOW);
pinMode(pin_switch, INPUT);
}
void loop()
{
if ( newSwitchState != oldSwitchState )
{

{

state++;
if (state > 2) {
state = 1;
}

digitalWrite(pin_LED1, LOW);
digitalWrite(pin_LED2, LOW);

if (state == 0) {
digitalWrite(pin_LED1, LOW);
digitalWrite(pin_LED2, LOW);
}
if (state == 1) {
digitalWrite(pin_LED1, HIGH);
digitalWrite(pin_LED2, LOW);
delay(2000);
digitalWrite(pin_LED1, LOW);
}
if (state == 2) {
digitalWrite(pin_LED2, HIGH);
digitalWrite(pin_LED1, LOW);
delay(2000);
digitalWrite(pin_LED2, LOW);

}

}
oldSwitchState = newSwitchState;
}
}

#### NorwegianNoob

Joined Aug 28, 2021
15
The only issue now is a hardware issue and i dont know if this is the right place for those questions.

The issue is:
When the powerplug turns off it takes 13 seconds before SwitchState goes LOW... Im thinking its the capasitor in the powerplug that takes time to discharge.

Anyone know how to remedy that ?

#### ericgibbs

Joined Jan 29, 2010
13,881
Hi,
Using push buttons.
E

Ref: the power plug 13 secs.
If the UNO is powered all the time. the program checks the pin #6, for High only once, then tests the pin state for a Low.
When it detects a Low, the program will wait and then check for the next High.

How long a time will elapse after 6) Go back to step 2. .?

Can you modify your Sketch to do that.??

#### NorwegianNoob

Joined Aug 28, 2021
15
Yes, the arduino is powered all the time.
But i dont understand why i would modify a sketch that works perfectly? Now i only have a hardware issue.

#### Yaakov

Joined Jan 27, 2019
3,608
Yes, the arduino is powered all the time.
But i dont understand why i would modify a sketch that works perfectly? Now i only have a hardware issue.
Because the RC time constant that determines the time it takes for the capacitor to discharge should be reliable, so if it is 3 seconds, you can count 7 instead of 10 to get turn off at 10 seconds after the power is removed. A software solution.

#### ericgibbs

Joined Jan 29, 2010
13,881
But i dont understand why i would modify a sketch that works perfectly? Now i only have a hardware issue.
hi NN,
A software fix is cheaper and easier to implement than a hardware fix.

You could ask why are you using a MCU, when two 555 timers, set as sequential Monostables would do the same task..???
ie: a hardware solution.
E

Last edited: