Hi everyone,
I am making a sketch to control a lamp with leds and a cooling fan. Until now I have been using this sketch below, where booleans let me divide the pwm numbers into different parts.. When the button is pressed the light gets brighter, a bit like those ikea touch lamps.
Now, I need another pin to connect a fan (i am using pin 10 at the moment) to the circuit. This fan should be on when the leds are on but should also stay on for some seconds when the leds will be switched off.. just to cool down the lamp.
I have tried using millis, but didn't get really far. Maybe is the case to change my previous bit of code too? Maybe I should use different cases to dim the leds instead?
I know it's very simple, but would be good to head someones help asap!
//
int buttonPin = 4;
int ledPin = 9;
int fanPin = 10;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledpwmLevel = 0;
int fanpwmLevel = 0;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(fanPin, OUTPUT);
Serial.begin(9600);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(buttonPin);
if (last != current)
{
delay(5);
current = digitalRead(buttonPin);
}
return current;
}
void loop()
{
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
ledpwmLevel = ledpwmLevel + 85;
Serial.println("BUTTON");
}
lastButton = currentButton;
if (ledpwmLevel > 255) ledpwmLevel = 0;
fanpwmLevel =
analogWrite(ledPin, ledpwmLevel);
}