Simple arduino button... HELP

Ya’akov

Joined Jan 27, 2019
10,235
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.
It wasn't clear that it was a 13 second deal before going low. It read as if it was 3 seconds until it went low, adding to the 10 in software. You need a resistor on the output to discharge the cap faster. It will mean more current dissipation when operating, though.
 

ericgibbs

Joined Jan 29, 2010
21,442
hi NN,
No one is trying to discourage you, quite the opposite. always consider alternatives.

BTW: what is the code inside the Red /* doing.???
In your sketch use those /* and */ to rem out the code, run it see what happens
E
Sorry about the editing, but the paste Code is having a problem

state = 1;
}
/*
digitalWrite(pin_LED1, LOW);
digitalWrite(pin_LED2, LOW);

if (state == 0) {
Serial.println(0);
digitalWrite(pin_LED1, LOW);
digitalWrite(pin_LED2, LOW);
}
*/
if (state == 1) {
 
Last edited:

Thread Starter

NorwegianNoob

Joined Aug 28, 2021
15
No, no, i appreciate all feedback of course :)

The code inside the red ensures that when i start the arduino up the first time, everything should be LOW.

Im sorry if my descriptions was not easily understandable... Im still learning ;)

If i add a "bleeder" resistor, how many ohms should it be and do i add it in series with the input 5v line that is connected to digital input 6? Im guessing if i put a 10 ohms resistor, too much current will damage the arduino, so what is the "sweet spot" ?
 

ericgibbs

Joined Jan 29, 2010
21,442
The code inside the red ensures that when i start the arduino up the first time, everything should be LOW.
Hi,
Consider;
Byte state=0

then when you start void loop you are incrementing the state to 1, so the program jumps over those lines to state==1 coding, so those first lines are never called.
E


You have already set all low here.
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);
}
 

ericgibbs

Joined Jan 29, 2010
21,442
If i add a "bleeder" resistor, how many ohms should it be and do i add it in series with the input 5v line that is connected to digital input 6? Im guessing if i put a 10 ohms resistor, too much current will damage the arduino, so what is the "sweet spot" ?
Hi,
Have you measured the 5V to ensure that it is 5Volts.?
and when it is Off it's close to 0V.?
E
 

ericgibbs

Joined Jan 29, 2010
21,442
hi NN,
Try this.
Tweak to suit your project,
E
C-like:
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 ==1)
    {         
         Serial.println(1); // test ONLY
        digitalWrite(pin_LED1, HIGH);
        digitalWrite(pin_LED2, LOW);
        delay(2000);
        digitalWrite(pin_LED1, LOW);
    
         Serial.println(2); // test ONLY
        digitalWrite(pin_LED2, HIGH);
        digitalWrite(pin_LED1, LOW);
        delay(2000);
        digitalWrite(pin_LED2, LOW);   
        delay(10000);  // wait until Vin times out   
        Serial.println("Ready"); // test ONLY
      }         
}
 

ericgibbs

Joined Jan 29, 2010
21,442
hi NN,
You may find this more interesting to work with.
Uses the UNO Onboard LED to show the Ready Statr for a new voice 'Volts' input, when lit.

E
C-like:
int pin_LED1 = 2;
int pin_LED2 = 4;
int pin_switch = 6;

const int ledPin =  LED_BUILTIN;// the LED pin for READY
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
 

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);
  pinMode(ledPin, OUTPUT); // OnBoard LED Used to Show Ready State
  digitalWrite(ledPin, HIGH);
}
void loop(){
  newSwitchState = digitalRead(pin_switch);
  if ( newSwitchState ==1)
    {     
      digitalWrite(ledPin, LOW);   
        digitalWrite(pin_LED1, HIGH);
        digitalWrite(pin_LED2, LOW);
        delay(2000);
        digitalWrite(pin_LED1, LOW);
    
        digitalWrite(pin_LED2, HIGH);
        digitalWrite(pin_LED1, LOW);
        delay(2000);
        digitalWrite(pin_LED2, LOW);   
        delay(10000);  // wait until Vin times out       
      digitalWrite(ledPin, HIGH);
      }         
}
 

ericgibbs

Joined Jan 29, 2010
21,442
hi,
If you wanted to be 'cautious' about the Voice Vin level, add a 1K in series with pin #6

Try the Sketch in post #27, it uses the onboard LED to indicate the Vin time has expired.
E
Lets know what you think.

Alter the delays to suit your project.....
 

ericgibbs

Joined Jan 29, 2010
21,442
hi NN,
A final, tidied Sketch.
Note the +5Vin signal on pin#6, will initiate the program to start the LED1 time On, LED2 Off, followed by the LED1 Off LED2 On periods.

Then the program will wait for the +5Vin to go to 0V before it will be ready to detect the next +5Vin on pin#6.

Ask if you have query
E
C-like:
int pin_LED1 = 2;
int pin_LED2 = 4;
int pin_switch = 6;
const int ledPin =  LED_BUILTIN;// the LED pin shows there is +5v on pin#6

void setup(){
  pinMode(pin_LED1, OUTPUT);  
  digitalWrite(pin_LED1, LOW);
  pinMode(pin_LED2, OUTPUT);  
  digitalWrite(pin_LED2, LOW);
  pinMode(pin_switch, INPUT);

// OnBoard LED Used to Show Vinp State
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}
void loop(){
  if (digitalRead(pin_switch) == HIGH){    
      digitalWrite(ledPin, HIGH);  
        digitalWrite(pin_LED1, HIGH);
        delay(2000);
        digitalWrite(pin_LED1, LOW);
   
        digitalWrite(pin_LED2, HIGH);
        delay(2000);
        digitalWrite(pin_LED2, LOW);  
    while(digitalRead(pin_switch)==HIGH){
    }      
      digitalWrite(ledPin, LOW);
      delay(1000);
      }        
}
 

Thread Starter

NorwegianNoob

Joined Aug 28, 2021
15
hi NN,
A final, tidied Sketch.
Note the +5Vin signal on pin#6, will initiate the program to start the LED1 time On, LED2 Off, followed by the LED1 Off LED2 On periods.

Then the program will wait for the +5Vin to go to 0V before it will be ready to detect the next +5Vin on pin#6.

Ask if you have query
E
C-like:
int pin_LED1 = 2;
int pin_LED2 = 4;
int pin_switch = 6;
const int ledPin =  LED_BUILTIN;// the LED pin shows there is +5v on pin#6

void setup(){
  pinMode(pin_LED1, OUTPUT); 
  digitalWrite(pin_LED1, LOW);
  pinMode(pin_LED2, OUTPUT); 
  digitalWrite(pin_LED2, LOW);
  pinMode(pin_switch, INPUT);

// OnBoard LED Used to Show Vinp State
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}
void loop(){
  if (digitalRead(pin_switch) == HIGH){   
      digitalWrite(ledPin, HIGH); 
        digitalWrite(pin_LED1, HIGH);
        delay(2000);
        digitalWrite(pin_LED1, LOW);
  
        digitalWrite(pin_LED2, HIGH);
        delay(2000);
        digitalWrite(pin_LED2, LOW); 
    while(digitalRead(pin_switch)==HIGH){
    }     
      digitalWrite(ledPin, LOW);
      delay(1000);
      }       
}
Thanks, looks great :)
Will try it later today
 

Thread Starter

NorwegianNoob

Joined Aug 28, 2021
15
Thanks, looks great :)
Will try it later today
It didnt quite work, as LED2 turned on automatically right after LED1 turned off.
But i have a working code now, so no need for any more iterations.

I want to thank everyone who helped and took an interest in my little project.
Much appreciated ❤
 

Thread Starter

NorwegianNoob

Joined Aug 28, 2021
15
C++:
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;
  }
}
 

ericgibbs

Joined Jan 29, 2010
21,442
hi NN.
For information only,
You could have just moved the Line #23
This would give LED1 On period when Vin =5V
when Vin went Low then LED2 would come on for the delay, then Off.
E

C-like:
int pin_LED1 = 2;
int pin_LED2 = 4;
int pin_switch = 6;
const int ledPin =  LED_BUILTIN;// the LED pin for READY

void setup(){
  pinMode(pin_LED1, OUTPUT);   
  digitalWrite(pin_LED1, LOW);
  pinMode(pin_LED2, OUTPUT);   
  digitalWrite(pin_LED2, LOW);
  pinMode(pin_switch, INPUT);
 
 // OnBoard LED Used to Show Vinp State
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}
void loop(){
  if (digitalRead(pin_switch) == HIGH){     
      digitalWrite(ledPin, HIGH);   
        digitalWrite(pin_LED1, HIGH);
        delay(2000);
        digitalWrite(pin_LED1, LOW);
      while(digitalRead(pin_switch)==HIGH){  //*************
    }       
        digitalWrite(pin_LED2, HIGH);
        delay(2000);
        digitalWrite(pin_LED2, LOW);   
  
      digitalWrite(ledPin, LOW);
      delay(1000);
      }         
}
 
Top