Guest User

Untitled

a guest
Dec 10th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. /*
  2. * RunningLights.ino
  3. * Created by Christoffer Andersson, 2012-08-26
  4. * This file is released under the GNU GPL
  5. */
  6.  
  7.  
  8. //First we define starting and ending pin for the leds
  9. #define lowPin 4
  10. #define highPin 12
  11.  
  12.  
  13. //Various declarations
  14. int currentPin; //Which pin as the current
  15. boolean reverse; //The current direction
  16. boolean run; //Is the lights paused
  17.  
  18. unsigned long tick = 0, now; //Variables for loop speed
  19. unsigned long speed = 0; //The speed (we read this from the pot)
  20.  
  21. boolean btnVal; //The buttons value the last time we checked
  22. unsigned long btnLastChange = 0; //At what time did we read the button
  23.  
  24. void setup()
  25. {
  26. //Lets initialize the Arduiono
  27. int i;
  28.  
  29. //Lets set the outputs
  30. for(i = lowPin;i <= highPin;i++)
  31. {
  32. pinMode(i, OUTPUT);
  33. }
  34.  
  35. //The switch is attached to pin 13
  36. pinMode(13, INPUT);
  37.  
  38. reverse = false;
  39. run = false;
  40. currentPin = lowPin+1;
  41.  
  42. // Serial.begin(9600);
  43. };
  44.  
  45.  
  46.  
  47.  
  48. void loop()
  49. {
  50. speed = analogRead(0); //Read the value from the pot
  51. now = millis(); //What "time is it now?
  52.  
  53. //I want this function to toggle run when the button is released, regarless of how long its been pressed
  54. if(btnLastChange+50 <= now){ //The 50ms delay is to avoid switch bounce
  55. if(btnVal != digitalRead(13)) //Has the switch changed since last time we checked?
  56. {
  57. btnVal = !btnVal; //Change the value
  58. btnLastChange = now; //Update last read time
  59. if(btnVal) //if the button was pressed, stop the lights from running
  60. toggleRun();
  61. }
  62. }
  63.  
  64.  
  65. //Check if enough time has passed, if so update the leds
  66. if(((tick+speed) <= now) && run) {
  67. runningLights();
  68. tick = millis();
  69. }
  70.  
  71. };
  72.  
  73.  
  74. void toggleRun()
  75. {
  76. //This function turns of all leds and then toggles the run.
  77. int i;
  78. for(i=lowPin;i<=highPin;i++)
  79. digitalWrite(i, LOW);
  80.  
  81. run = !run;
  82. }
  83.  
  84.  
  85.  
  86. void runningLights()
  87. {
  88. switch(currentPin)
  89. {
  90. case lowPin:
  91. reverse = !reverse;
  92. break;
  93. case highPin:
  94. reverse = !reverse;
  95. break;
  96. default:
  97. break;
  98. };
  99.  
  100. if(reverse){
  101. digitalWrite(currentPin, LOW);
  102. currentPin--;
  103. digitalWrite(currentPin, HIGH);
  104. } else {
  105. digitalWrite(currentPin, LOW);
  106. currentPin++;
  107. digitalWrite(currentPin, HIGH);
  108. }
  109. }
Add Comment
Please, Sign In to add comment