Advertisement
baldengineer

shaun-better practices

Dec 28th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // code is not verified to work.  just illustrating some changes to be made.
  2.  
  3. int delaytime = 2000;
  4. int switchpin = 8;
  5. int ledpin = 13;
  6. bool statechanged = false;  // notice how this is different?
  7. bool lastbutton = LOW;
  8. bool currentButton = LOW;
  9.  
  10.  
  11. void setup()
  12. {
  13.   pinMode(ledpin, OUTPUT);
  14.   pinMode(switchpin, INPUT);
  15. }
  16.  
  17. void loop()
  18. {
  19.   currentButton = digitalRead(switchpin);
  20.   if (currentButton == HIGH && lastbutton == LOW)
  21.   {
  22.     statechanged = true;
  23.     lastbutton = HIGH;
  24.   }
  25.   else
  26.   {
  27.     lastbutton = currentButton;  // calling digitalRead() like could have a problem if the pin's state changed since you last checked
  28.   }
  29.  
  30.  
  31.   digitalWrite(ledpin, HIGH);
  32.   delay(delaytime);
  33.   digitalWrite(ledpin, LOW);
  34.   delay(delaytime);
  35.  
  36.   if  ( statechanged == true && delaytime > 10)
  37.   {
  38.     delaytime = delaytime - 499;
  39.   }
  40.   else
  41.   {
  42.     delaytime = 2000;
  43.   }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement