Simple arduino button... HELP

Thread Starter

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.
Now return to start and repeat.....

Any ideas for me how to do this?
 

MrChips

Joined Oct 2, 2009
30,708
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?
 

Thread Starter

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
30,708
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.
 

Thread Starter

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
18,766
hi,
If I follow you correctly.
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.
 

ericgibbs

Joined Jan 29, 2010
18,766
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
 

Thread Starter

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()
{
  newSwitchState = digitalRead(pin_switch);
  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;
  }
}
 

Thread Starter

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
18,766
Hi,
Will download your Sketch and give a try, I have project built on my bench,
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.??
 

Thread Starter

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.
 

Ya’akov

Joined Jan 27, 2019
9,069
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
18,766
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:

Thread Starter

NorwegianNoob

Joined Aug 28, 2021
15
Since this will be a voice controlled device, as soon as i say "Hey google, turn off device" the powerplug turns off and the 13 second countdown until the cap is dissipated until digital input turns LOW. I cant see a way to anticipate that in software.

Well as my name suggests im a noob, haha... I have used arduinos in the past, but only at a beginner level... 555 timers dont seem as intuitive as working with an arduino where i can control timing more easily. The 10 seconds delay i have implemented is just a placeholder until i know just how long my "actuator" takes to travel the desired distance.
 
Top