Pages: [1]   Go Down
Author Topic: PWM dimming + Fan output  (Read 521 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: