Go Down

Topic: PWM dimming + Fan output (Read 1 time) previous topic - next topic


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);

boolean debounce(boolean last)
  boolean current = digitalRead(buttonPin);
  if (last != current)
    current = digitalRead(buttonPin);
  return current;

void loop()
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
    ledpwmLevel = ledpwmLevel + 85;
  lastButton = currentButton;

  if (ledpwmLevel > 255) ledpwmLevel = 0;
  fanpwmLevel =
  analogWrite(ledPin, ledpwmLevel);



You ran away before I had time to reply.  Try something like this (untested): http://pastebin.com/4v0pBFWH


hey thanks dude.
i am gonna test it in a second, but just by clicking on the "play" button.. there seems to be some issue with the debounced thing you introduced. How does that work? the end bit seems great though! thanks a lot


Sorry dude, you've got to put some effort in yourself.  Have you tried Googling any error messages that come up?

Go Up