Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 17th, 2012  |  syntax: C++  |  size: 1.17 KB  |  views: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Milano 0.1
  2.  
  3. int buttonPin = 4;
  4. int ledPin = 9;
  5. int fanPin = 10;
  6. boolean lastButton = LOW;
  7. boolean currentButton = LOW;
  8. int ledpwmLevel = 0;
  9. boolean fanOn = false;
  10.  
  11. void setup()
  12. {
  13.   pinMode(buttonPin, INPUT);
  14.   pinMode(ledPin, OUTPUT);
  15.   pinMode(fanPin, OUTPUT);
  16.   Serial.begin(9600);
  17. }
  18.  
  19. boolean lastButtonSample;
  20. long lastButtonTime;
  21. boolean debounceButton(boolean debounced)
  22. {
  23.   boolean buttonSample = digitalRead(buttonPin);
  24.   if (lastButtonSample != buttonSample)
  25.   {
  26.     lastButtonTime = millis();
  27.   }
  28.   lastButtonSample = buttonSample;
  29.  
  30.   if (millis() - lastButtonTime > 10)
  31.     debounced = buttonSample;
  32.   }  
  33.   return debounced;
  34. }
  35.  
  36. void loop()
  37. {
  38.   currentButton = debounce(lastButton);
  39.   if (lastButton == LOW && currentButton == HIGH)
  40.   {
  41.     fanOn = true;
  42.     analogWrite(fanPin, 255);
  43.     ledpwmLevel = ledpwmLevel + 85;
  44.     Serial.println("BUTTON");
  45.  
  46.     if (ledpwmLevel > 255)
  47.     {
  48.       ledpwmLevel = 0;
  49.       lastLEDTime = millis();
  50.     }
  51.     analogWrite(ledPin, ledpwmLevel);
  52.   }
  53.  
  54.   if (fanOn && millis() - lastLEDTime > 2000)
  55.   {
  56.     fanOn = false;
  57.     analogWrite(fanPin, 0);
  58.   }
  59.   lastButton = currentButton;
  60. }
clone this paste RAW Paste Data