Skip to content

Instantly share code, notes, and snippets.

@shinyshiny
Created August 25, 2015 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinyshiny/cc91ab4a8e4962afcdff to your computer and use it in GitHub Desktop.
Save shinyshiny/cc91ab4a8e4962afcdff to your computer and use it in GitHub Desktop.
Arduino sketch for project #2
/* ---------------------------------------------------------
* | Arduino Experimentation Kit Example Code |
* | CIRC-02 .: 8 LED Fun :. (Multiple LEDs) |
* ---------------------------------------------------------
*
* A few Simple LED animations
*
* For more information on this circuit http://tinyurl.com/d2hrud
*
*/
//LED Pin Variables
int ledPins[] = {2,3,4,5,6,7,8,9}; //An array to hold the pin each LED is connected to
//i.e. LED #0 is connected to pin 2, LED #1, 3 and so on
//to address an array use ledPins[0] this would equal 2
//and ledPins[7] would equal 9
/*
* setup() - this function runs once when you turn your Arduino on
* We the three control pins to outputs
*/
void setup()
{
//Set each pin connected to an LED to output mode (pulling high (on) or low (off)
for(int i = 0; i < 8; i++){ //this is a loop and will repeat eight times
pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
} //the code this replaces is below
/* (commented code will not run)
* these are the lines replaced by the for loop above they do exactly the
* same thing the one above just uses less typing
pinMode(ledPins[0],OUTPUT);
pinMode(ledPins[1],OUTPUT);
pinMode(ledPins[2],OUTPUT);
pinMode(ledPins[3],OUTPUT);
pinMode(ledPins[4],OUTPUT);
pinMode(ledPins[5],OUTPUT);
pinMode(ledPins[6],OUTPUT);
pinMode(ledPins[7],OUTPUT);
(end of commented code)*/
}
/*
* loop() - this function will start after setup finishes and then repeat
* we call a function called oneAfterAnother(). if you would like a different behaviour
* uncomment (delete the two slashes) one of the other lines
*/
void loop() // run over and over again
{
//oneAfterAnotherNoLoop(); //this will turn on each LED one by one then turn each off
//oneAfterAnotherLoop(); //does the same as oneAfterAnotherNoLoop but with
//much less typing
//oneOnAtATime(); //this will turn one LED on then turn the next one
//on turning the
//former off (one LED will look like it is scrolling
//along the line
inAndOut(); //lights the two middle LEDs then moves them out then back
//in again
}
/*
* inAndOut() - This will turn on the two middle LEDs then the next two out
* making an in and out look
*/
void inAndOut(){
int delayTime = 300; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower
//runs the LEDs out from the middle
for(int i = 0; i <= 3; i++){
int offLED = i - 1; //Calculate which LED was turned on last time through
if(i == 0) { //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
offLED = 3; //turn on LED 2 and off LED 1)
} //however if i = 0 we don't want to turn of led -1 (doesn't exist)
//instead we turn off LED 7, (looping around)
int onLED1 = 2 - i; //this is the first LED to go on ie. LED #3 when i = 0 and LED
//
//#0 when i = 3
int onLED2 = 4 + i; //this is the first LED to go on ie. LED #4 when i = 0 and LED
//
//#7 when i = 3
int offLED1 = 3 - offLED; //turns off the LED we turned on last time
int offLED2 = 4 + offLED; //turns off the LED we turned on last time
digitalWrite(ledPins[onLED1], HIGH);
digitalWrite(ledPins[onLED2], HIGH);
digitalWrite(ledPins[offLED1], LOW);
digitalWrite(ledPins[offLED2], LOW);
delay(delayTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment