Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * RunningLights.ino
- * Created by Christoffer Andersson, 2012-08-26
- * This file is released under the GNU GPL
- */
- //First we define starting and ending pin for the leds
- #define lowPin 4
- #define highPin 12
- //Various declarations
- int currentPin; //Which pin as the current
- boolean reverse; //The current direction
- boolean run; //Is the lights paused
- unsigned long tick = 0, now; //Variables for loop speed
- unsigned long speed = 0; //The speed (we read this from the pot)
- boolean btnVal; //The buttons value the last time we checked
- unsigned long btnLastChange = 0; //At what time did we read the button
- void setup()
- {
- //Lets initialize the Arduiono
- int i;
- //Lets set the outputs
- for(i = lowPin;i <= highPin;i++)
- {
- pinMode(i, OUTPUT);
- }
- //The switch is attached to pin 13
- pinMode(13, INPUT);
- reverse = false;
- run = false;
- currentPin = lowPin+1;
- // Serial.begin(9600);
- };
- void loop()
- {
- speed = analogRead(0); //Read the value from the pot
- now = millis(); //What "time is it now?
- //I want this function to toggle run when the button is released, regarless of how long its been pressed
- if(btnLastChange+50 <= now){ //The 50ms delay is to avoid switch bounce
- if(btnVal != digitalRead(13)) //Has the switch changed since last time we checked?
- {
- btnVal = !btnVal; //Change the value
- btnLastChange = now; //Update last read time
- if(btnVal) //if the button was pressed, stop the lights from running
- toggleRun();
- }
- }
- //Check if enough time has passed, if so update the leds
- if(((tick+speed) <= now) && run) {
- runningLights();
- tick = millis();
- }
- };
- void toggleRun()
- {
- //This function turns of all leds and then toggles the run.
- int i;
- for(i=lowPin;i<=highPin;i++)
- digitalWrite(i, LOW);
- run = !run;
- }
- void runningLights()
- {
- switch(currentPin)
- {
- case lowPin:
- reverse = !reverse;
- break;
- case highPin:
- reverse = !reverse;
- break;
- default:
- break;
- };
- if(reverse){
- digitalWrite(currentPin, LOW);
- currentPin--;
- digitalWrite(currentPin, HIGH);
- } else {
- digitalWrite(currentPin, LOW);
- currentPin++;
- digitalWrite(currentPin, HIGH);
- }
- }
Add Comment
Please, Sign In to add comment