Blinking Light with Switch

Hi All,

I am very fresh in this area and am still have a lot to learn.

I followed a video tutorial and decided to make some improvement. Initially it was an LED with a switch, the LED get brighter as the switch being pressed, then back to off after the value reached past 255. I tried to make changed to this working solution. The change is that the light will BLINK brighter as the switch being pressed. It will never get brighter and needed multiple presses to get a signal (I used serial write to test this).

Please ,please help me...
Please find below is my code:

int switchPin = 8;
int ledPin = 3;
boolean lastButton = LOW;
int ledLevel = 0;
boolean ledOn = false;

boolean currentButton = LOW;

void setup(){
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

boolean debounce(boolean last){
  boolean current = digitalRead(switchPin);
  if(last != current){
    delay(5);
    current=digitalRead(switchPin);    
  }  
  return current;
}

void loop(){
  /*  LIGHT BRIGHTER AS CLICKED - ATTEMPT: BLINK*/
  currentButton = debounce(lastButton);
  if(lastButton == LOW && currentButton == HIGH){
    ledLevel = ledLevel + 51;
    ledOn = true;
    Serial.println("Button just pressed");
  }
  lastButton = currentButton;
  if(ledLevel > 255){
    ledLevel = 0;
    analogWrite(ledPin, 0);
    ledOn = !ledOn;
  }
    if(ledOn){
      analogWrite(ledPin, ledLevel);
      delay(500);
     analogWrite(ledPin, 0);
      delay(500);  
    }  
}

I appreciate your help. Thank you.

dK

and needed multiple presses to get a signal

Those delays() will do that.

Of course, not using an external resistor or the internal pullup resistors will do that to. Too bad you didn't explain how the switch is wired.

Paul,

Thank you very much for your reply.

What should I do if not using delay to blink the LED?

Please find attached is the schema of how the switch is wired.

What should I do if not using delay to blink the LED?

In your code, if ledOn is true then two 500 millisecond delays will be triggered and during each of them the Arduino will do nothing. So, for one second it cannot read your switch. One second may not sound like much but it will seriously affect how responsive your system is to inputs.

You need to consider using millis() as in the BlinkWithoutDelay example in the IDE so that your program can read a switch whilst blinking the LED.

Thank very, very much UKHeliBob.

I've been looking for this solution since yesterday. I can now move on.

Below is the working code. I hope I can help others too.

int switchPin = 8;
int ledPin = 3;
boolean lastButton = LOW;
int ledLevel = 0;
boolean ledOn = false;

boolean currentButton = LOW;

long interval = 500; 
long previousMillis = 0;  

void setup(){
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

boolean debounce(boolean last){
  boolean current = digitalRead(switchPin);
  if(last != current){
    delay(5);
    current=digitalRead(switchPin);    
  }  
  return current;
}

int tempLEDLevel = 0;
void loop(){

/*  LIGHT BRIGHTER AS CLICKED - ATTEMPT: BLINK*/
  currentButton = debounce(lastButton);
  if(lastButton == LOW && currentButton == HIGH){
    ledLevel = ledLevel + 51;
    ledOn = true;
    Serial.println("Button just pressed");
  }
  lastButton = currentButton;
  if(ledLevel > 255){
    ledLevel = 0;
    analogWrite(ledPin, 0);
    ledOn = !ledOn;
  }

    if(ledOn){
      unsigned long currentMillis = millis();
 
      if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;   

      // if the LED is off turn it on and vice-versa:
      if (tempLEDLevel > 0){
        tempLEDLevel = 0;
      }else
        tempLEDLevel = ledLevel;
      }
    // set the LED with the ledState of the variable:
      analogWrite(ledPin, tempLEDLevel);
    } 
}