Home > Blockchain >  LED at full brightness after dim button is not touched for certain ammount of time
LED at full brightness after dim button is not touched for certain ammount of time

Time:03-24

I made a simple 1 button LED dimmer and I would like to have the LED go back to full brightness after the button has not been touched for a set amount of time. But the code I came up with does not seem to be working as I cannot dim the LED

int buttonPin = 12;
int LEDPin = 3;
int buttonVal;
int LEDbright = 0;
int dt = 500;
int fdt = 60000;
void setup() {
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT);

  pinMode(LEDPin, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  static unsigned long whenButtonWasTouchedMs = 0;
  buttonVal = digitalRead(buttonPin);

  Serial.print("Button = ");
  Serial.print(buttonVal);
  Serial.print(", ");

  delay(dt);

  if (buttonVal == 1) {
    LEDbright = LEDbright   20;
    whenButtonWasTouchedMs = millis();
    
  }
  Serial.println(LEDbright);
  //
  if (LEDbright > 255) {
    LEDbright = 255;
    delay(dt);
  }

  unsigned long afterButtonWasTouchedMs = millis() - whenButtonWasTouchedMs;

  if (afterButtonWasTouchedMs >= 60000) {
    LEDbright = 0;

    analogWrite(LEDPin, LEDbright);
  }
}

CodePudding user response:

Your question doesn't seem to line up with how the code was written. It looks like you want to do the opposite (have the light get brighter when the button is held down, and then switch off 60 seconds after the button was released). Here's the code I came up with to help with your stated problem:

int buttonPin = 12;
int LEDPin = 3;
int buttonVal;
int whenButtonWasTouched;
unsigned int LEDbright = 255;
int dt = 500;
int fdt = 60000;
unsigned long whenButtonWasTouchedMs = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT);

  pinMode(LEDPin, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  //static unsigned long whenButtonWasTouchedMs = 0;
  buttonVal = digitalRead(buttonPin);

  Serial.print("Button = ");
  Serial.print(buttonVal);
  Serial.print(", ");

  delay(dt);

  if (buttonVal == 1) {
    LEDbright = LEDbright - 20;
    whenButtonWasTouchedMs = millis();
    
  }

  //
  if (LEDbright < 20 || LEDbright > 255) { // The LEDbright > 255 part is because sometimes there can be an integer overflow if we don't keep this.
    LEDbright = 0;
  }  
  Serial.println(LEDbright);

  unsigned long afterButtonWasTouchedMs = millis() - whenButtonWasTouched;

  if (afterButtonWasTouchedMs >= 60000) {
    LEDbright = 255;
  }    
  analogWrite(LEDPin, LEDbright);
}

As you can see I changed it so that analogWrite is not in the if statement, it checks for integer overflows, and prints out the adjusted value for the led brightness. To make it grow brighter as the button is held and then switch off after 60 seconds just change the minus sign to a plus sign in the LEDbright adjustment line, and change some of the other values around.

CodePudding user response:

int buttonPin = 12;
int LEDPin = 3;
int buttonVal;

unsigned int LEDbright = 0;
int dt = 500;
int fdt = 60000;
unsigned long whenButtonWasTouchedMs = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT);

  pinMode(LEDPin, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  //static unsigned long whenButtonWasTouchedMs = 0;
  buttonVal = digitalRead(buttonPin);

  Serial.print("Button = ");
  Serial.print(buttonVal);
  Serial.print(", ");

  delay(dt);

  if (buttonVal == 1) {
    LEDbright = LEDbright   20;
    whenButtonWasTouchedMs = millis();
    
  }

  if (LEDbright > 255) {
    LEDbright = 255;
    delay(dt);
  } 
  Serial.println(LEDbright);

  unsigned long afterButtonWasTouchedMs = millis() - whenButtonWasTouchedMs;

  if (afterButtonWasTouchedMs >= 60000) {
    LEDbright = 0;
  }    
  analogWrite(LEDPin, LEDbright);
}
  • Related