Advertisement
Guest User

HEIKINLEDIT

a guest
Nov 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. const int ledPin = 3;
  2. const int button = 7;
  3. const int maxPWM = 255;
  4. const int minPWM = 0;
  5. int fadeDirection = 1;
  6. int buttonState = 0;
  7. int lastButtonState = 0;
  8. int brightness = 0;
  9. int counter = 1;
  10. byte fade = 5;
  11. int fadeInterval = 50;
  12. unsigned long lastMillis;
  13. unsigned long currentMillis;
  14.  
  15. void setup() {
  16. pinMode(ledPin, OUTPUT);
  17. pinMode(button, INPUT);
  18. Serial.begin(9600);
  19. }
  20.  
  21. void LightLoop(){
  22. currentMillis = millis();
  23. if (currentMillis - lastMillis >= fadeInterval){
  24. if (fadeDirection == 1) {
  25. brightness = brightness + fade;
  26. if (brightness >= maxPWM) {
  27. brightness = maxPWM;
  28. fadeDirection = 0;
  29. }
  30. }
  31. else {
  32. brightness = brightness - fade;
  33. if (brightness <= minPWM){
  34. brightness = minPWM;
  35. fadeDirection = 1;
  36. }
  37. }
  38. }
  39. analogWrite(ledPin, brightness);
  40. }
  41.  
  42. void loop() {
  43. buttonState = digitalRead(button);
  44. delay(200);
  45. if (counter <= 1) {
  46. LightLoop();
  47. if (buttonState != lastButtonState){
  48. Serial.println("Push");
  49. fadeDirection = 0;
  50. counter ++;
  51. }
  52. }
  53. else {
  54. LightLoop();
  55. if (buttonState != lastButtonState){
  56. Serial.println("Push");
  57. fadeDirection = 1;
  58. counter = 1;
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement