Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. //Initilize Vars
  2. const int ButtonUp = 12;
  3. const int ButtonDown = 11;
  4. boolean lastButtonUp = LOW;
  5. boolean lastButtonDown = LOW;
  6. boolean currentButtonUp = LOW;
  7. boolean currentButtonDown = LOW;
  8. int ledState = LOW;
  9. unsigned long previousMillis = 0;
  10. long Hz = 9;
  11. long interval = 1000/(2);
  12.  
  13. void setup(void) {
  14. // put your setup code here, to run once:
  15. Serial.begin(9600);
  16. pinMode(ButtonUp, INPUT_PULLUP);
  17. pinMode(ButtonDown, INPUT_PULLUP);
  18. pinMode(2,OUTPUT);
  19.  
  20. }
  21.  
  22. void loop() {
  23.  
  24. buttonPushed();
  25. blinkLED(interval/Hz);
  26.  
  27. }
  28.  
  29. void buttonPushed(){
  30. //Check if Up Button is pushed
  31. currentButtonUp = digitalRead(ButtonUp);
  32. if(lastButtonUp == LOW && currentButtonUp == HIGH && Hz < 999)
  33. {
  34. Hz = Hz + 1;
  35. //Serial.println(Hz);
  36. }
  37.  
  38. lastButtonUp = currentButtonUp;
  39.  
  40. //Check if Down Button is pushed
  41. currentButtonDown = digitalRead(ButtonDown);
  42. if(lastButtonDown == LOW && currentButtonDown == HIGH && Hz > 0)
  43. {
  44. Hz = Hz - 1;
  45. //Serial.println(Hz);
  46. if (Hz == 0){
  47. ledState= HIGH;
  48. }
  49. }
  50. lastButtonDown = currentButtonDown;
  51.  
  52. }
  53.  
  54. void blinkLED(const long interval) {
  55. //Serial.println(interval);
  56. unsigned long currentMillis = millis();
  57.  
  58. if (currentMillis - previousMillis >= interval){
  59. previousMillis = currentMillis;
  60.  
  61. if (ledState == LOW){
  62. ledState = HIGH;
  63. }
  64. else {
  65. ledState = LOW;
  66. }
  67.  
  68. }
  69. digitalWrite(2, ledState);
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement