Trouble with LED trigger

Hi all,

I am trying to trigger an LED to stay on for 1.5 seconds when a momentary button is pressed. However, I am having a lot of trouble getting it to work. Any help would be appreciated.


#define POWER_LED 8
#define START_LED 7
#define BUTTON_PIN 6

unsigned long previousTime = 0;
unsigned long waitTime = 1500;

  void setup()
{
  pinMode(POWER_LED, OUTPUT);
  digitalWrite(POWER_LED, HIGH);
  delay(500);
  digitalWrite(POWER_LED, LOW);
  
}

void loop()
{
  
  previousTime = millis();
  
  if (digitalRead(BUTTON_PIN) == HIGH) {
    digitalWrite(START_LED, HIGH);
  
  }
   while (previousTime < previousTime + waitTime) {
     digitalWrite(START_LED, HIGH);
   }
  
 
   digitalWrite(START_LED, LOW);
  
  
 }

Look at the this example:

When you fully understand it, then try your project again.


FYI

1 Like

Nothing in the body of the while loop changes the values of the variables in the condition you test. So without even thinking about it, one can say the code will do nothing OR get stuck infinite-like.

a7

@LarryD I don't understand what you are trying to tell me with the diagram. Could you please explain? (Sorry, this is all new to me!)

@LarryD I understand the code for blinking the LED continuously, but I don't understand how to trigger it with a button press.

This should work if you wire your button between pin 6 and GND.

#define POWER_LED 8
#define START_LED 7
#define BUTTON_PIN 6

const unsigned long waitTime = 1500;

void setup()
{
  pinMode(START_LED, OUTPUT);
  pinMode(POWER_LED, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  digitalWrite(POWER_LED, HIGH);
  delay(500);
  digitalWrite(POWER_LED, LOW);
}

void loop()
{
  if (digitalRead(BUTTON_PIN) == LOW) 
  {
    digitalWrite(START_LED, HIGH);
    delay(waitTime ); 
    digitalWrite(START_LED, LOW);
  }
 }
  • buttons are typically connected between the pin and ground, the pint configures as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

  • if you're going to use a while loop, why not just do a delay (1500)?

otherwise consider

void loop ()
{
    unsigned long msec = millis ();

    if (digitalRead (BUTTON_PIN) == LOW) {
        previousTime = msec;
        digitalWrite (START_LED, HIGH);
    }

    if (msec - previousTime >= waitTime) {
        digitalWrite (START_LED, LOW);
    }
}

@gcjr @ToddL1962 I want to avoid using delay because I will be running a timer and LCD screen on this circuit and I don't want any hangups.

When I run this, the led just turns on with the arduino and stays on.

Fixed that for ya.

a7

1 Like

Your original code had a while loop for the delay which would have blocked the code anyhow. You didn't mention any other functionality in your original post. This is why it is important to be clear as to requirements for your code.

Thanks! :grinning:

In the schematic switch, S3 is the recommended way of for wiring switches.

In setup( ), we run this line of code:

pinMode(mySwitch, INPUT_PULLUP);

To examine the switch state we look for a LOW when the switch is pushed.

if(digitalRead(myswitch) == LOW)
{
    //do something 
}

add prints to verify when the button is pressed and when/if the timer expires

Oh. Okay. Would that explain why my LED was dim?

Thanks. I figured it out. I was just being dumb.

Does this mean everything is working as you need ?

Yes, thank you! Now I'm dealing with a different issue.

BTW

Always start with a schematic (hand drawn or otherwise).

Hi all. I have an espresso machine that I'd like to make some modifications to using an arduino. However, I've never used an arduino before and I've never programmed before so any help on programming/parts/feasibility would be greatly appreciated.

Here is what I want to do:

I want to add an RGB LCD screen, thermocouple, and push button. I want to have the LCD display the temperature of the thermocoil and change color with the temperature. When the button is pushed I want it to start the brew process and a timer on the LCD screen. When it is pressed a second time I want it to stop/reset the timer and stop the brew process.

Any help is appreciated! Thank you!