100Toby1

Untitled

Mar 19th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. const int incrPin = 2;
  2. const int decrPin = 4;
  3. const int ledPin = 13;
  4.  
  5. int ledState = LOW;
  6. int changeSpd = 500;
  7. int changeStep = 0;
  8. int prevTime = 0;
  9.  
  10. int incrButtonState;
  11. int decrButtonState;
  12.  
  13. int lastIncrButtonState = LOW;
  14. int lastDecrButtonState = LOW;
  15.  
  16. unsigned long lastIncrDebounceTime = 0;
  17. unsigned long lastDecrDebounceTime = 0;
  18. unsigned long debounceDelay = 50;
  19.  
  20. void setup() {
  21.         Serial.begin(9600);
  22.         Serial.println("init");
  23.        
  24.         prevTime = millis();
  25.  
  26.     pinMode(incrPin, INPUT);
  27.     pinMode(decrPin, INPUT);
  28.     pinMode(ledPin, OUTPUT);
  29.  
  30.     digitalWrite(ledPin, ledState);
  31. }
  32.  
  33. void loop() {
  34.     int incrState = digitalRead(incrPin);
  35.     int decrState = digitalRead(decrPin);
  36.  
  37.     if (incrState != lastIncrButtonState) {
  38.         lastIncrDebounceTime = millis();
  39.     }
  40.     if (decrState != lastDecrButtonState) {
  41.         lastDecrDebounceTime = millis();
  42.     }
  43.  
  44.         //Serial.println("Current " + String(incrState) + ", Last: " + String(lastIncrButtonState));
  45. //          Serial.println("Tick: " + String(changeStep));
  46. Serial.println(String(changeSpd));
  47.                
  48.     if ((millis() - lastIncrDebounceTime) > debounceDelay) {
  49.                 if (incrState != incrButtonState) {
  50.                         incrButtonState = incrState;
  51.                 if (incrState == HIGH) {
  52.                                handleIncr();
  53.                 }
  54.                 }
  55.     }
  56.  
  57.     if ((millis() - lastDecrDebounceTime) > debounceDelay) {
  58.                 if (decrState != decrButtonState) {
  59.                         decrButtonState = decrState;
  60.                 if (decrState == HIGH) {
  61.                                handleDecr();
  62.                 }
  63.                 }
  64.     }
  65.  
  66.     lastIncrButtonState = incrState;
  67.     lastDecrButtonState = decrState;
  68.  
  69.         //Change the light state
  70.        
  71.         changeStep += (millis() - prevTime);
  72.         prevTime = millis();
  73.        
  74.         if (changeStep >= changeSpd) {
  75.               ledState = (ledState == 0) ? 1 : 0;
  76.               digitalWrite(ledPin, ledState);
  77.               changeStep = 0;
  78.         }
  79. }
  80.  
  81. void handleIncr() {
  82.         Serial.println("incr");
  83.         changeSpd += 50;
  84. }
  85.  
  86. void handleDecr() {
  87.         Serial.println("decr");
  88.         changeSpd -= 50;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment