Assignment 6.1

posted Jun 30, 2010, 9:00 PM by Unknown user   [ updated Jun 30, 2010, 9:04 PM ]
I got lucky on this one because I actually wrote fairly customizable code back for the unit 2 assignment so I just had to throw in a few new variables

Potentiometer Blink

/*
  Blink Modified With Patterns
 
 Repeatedly blinks leds in a group of 6 in 5 predefined patterns
 
 Original Created 1 June 2005
 By David Cuartielles
 
 Modified 23 June 2010
 By Joseph Constan
 */

#define led1 12   //replace all instances of "led#" with their respective pin #s
#define led2 11   //(more economical in terms of memory than int declarations)
#define led3 10
#define led4 9
#define led5 8
#define led6 7

#define pot1 5

// The setup() method runs once, when the sketch starts
void setup()   {                
  // initialize the digital pins 1-6 as an output:
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);  
  pinMode(led4, OUTPUT);  
  pinMode(led5, OUTPUT);  
  pinMode(led6, OUTPUT);    
}

/*this light pattern will blink all leds on and off 
  for a number of times (int repetitions) at a given speed (int intervalLength)*/
void patternAllBlink(int repetitions, int intervalLength)
{
  for(int i=0; i < repetitions; i++) {
     //set all leds on
     digitalWrite(led1, HIGH);
     digitalWrite(led2, HIGH);
     digitalWrite(led3, HIGH);
     digitalWrite(led4, HIGH);
     digitalWrite(led5, HIGH);
     digitalWrite(led6, HIGH);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength); 
     
     //set all leds off
     digitalWrite(led1, LOW);
     digitalWrite(led2, LOW);
     digitalWrite(led3, LOW);
     digitalWrite(led4, LOW);
     digitalWrite(led5, LOW);
     digitalWrite(led6, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength); 
  }
}

/*this light pattern blink odds on and evens off, and vice versa,
  for a number of times (int repetitions) at a given speed (int intervalLength)*/
void patternAlternateOddsEvens(int repetitions, int intervalLength)
{
  for(int i=0; i < repetitions; i++) {
     //set all odd leds on
     digitalWrite(led1, HIGH);
     digitalWrite(led3, HIGH);
     digitalWrite(led5, HIGH);
     //set all even leds off
     digitalWrite(led2, LOW);
     digitalWrite(led4, LOW);
     digitalWrite(led6, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);   
     
     //set all odd leds off
     digitalWrite(led1, LOW);
     digitalWrite(led3, LOW);
     digitalWrite(led5, LOW);
     //set all even leds on
     digitalWrite(led2, HIGH);
     digitalWrite(led4, HIGH);
     digitalWrite(led6, HIGH);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);   
  }
}

/*this light pattern "bounces" 2 leds spaced 1 led apart back and forth
  for a number of times (int repetitions) at a given speed (int intervalLength)*/
void patternBounceSpaced(int repetitions, int intervalLength)
{
  for(int i=0; i < repetitions; i++) {
     //set leds 1 and 3 on
     digitalWrite(led1, HIGH);
     digitalWrite(led3, HIGH);
     //set all other leds off
     digitalWrite(led2, LOW);
     digitalWrite(led4, LOW);
     digitalWrite(led5, LOW);     
     digitalWrite(led6, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);   
     
     //set leds 2 and 4 on
     digitalWrite(led2, HIGH);
     digitalWrite(led4, HIGH);
     //set leds 1 and 3 off
     digitalWrite(led1, LOW);
     digitalWrite(led3, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);   
     
     //set leds 3 and 5 on
     digitalWrite(led3, HIGH);
     digitalWrite(led5, HIGH);
     //set leds 2 and 4 off
     digitalWrite(led2, LOW);
     digitalWrite(led4, LOW);     
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);  
    
     //set leds 4 and 6 on
     digitalWrite(led4, HIGH);
     digitalWrite(led6, HIGH);
     //set leds 3 and 5 off
     digitalWrite(led3, LOW);
     digitalWrite(led5, LOW);     
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);    
     
     //set leds 3 and 5 on
     digitalWrite(led3, HIGH);
     digitalWrite(led5, HIGH);
     //set leds 4 and 6 off
     digitalWrite(led6, LOW);
     digitalWrite(led4, LOW);     
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength); 
     
     //set leds 2 and 4 on
     digitalWrite(led2, HIGH);
     digitalWrite(led4, HIGH);
     //set leds 5 and 3 off
     digitalWrite(led5, LOW);
     digitalWrite(led3, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength); 
  }
}

/*this light pattern "bounces" 2 adjacent leds back and forth
  for a number of times (int repetitions) at a given speed (int intervalLength)*/
void patternBounceTwo(int repetitions, int intervalLength)
{
  for(int i=0; i < repetitions; i++) {
     //set leds 1 and 2 on
     digitalWrite(led1, HIGH);
     digitalWrite(led2, HIGH);
     //set all other leds off
     digitalWrite(led3, LOW);
     digitalWrite(led4, LOW);
     digitalWrite(led5, LOW);     
     digitalWrite(led6, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);   
     
     //set led 3 on
     digitalWrite(led3, HIGH);
     //set led 1 off
     digitalWrite(led1, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);   
     
     //set led 4 on
     digitalWrite(led4, HIGH);
     //set led 2 off
     digitalWrite(led2, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);  
    
     //set led 5 on
     digitalWrite(led5, HIGH);
     //set led 3 off
     digitalWrite(led3, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);    
     
     //set led 6 on
     digitalWrite(led6, HIGH);
     //set led 4 off
     digitalWrite(led4, LOW);     
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength); 
     
     //set led 4 on
     digitalWrite(led4, HIGH);
     //set led 6 off
     digitalWrite(led6, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength); 
     
     //set led 3 on
     digitalWrite(led3, HIGH);
     //set led 5 off
     digitalWrite(led5, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength); 
     
     //set led 2 on
     digitalWrite(led2, HIGH);
     //set led 4 off
     digitalWrite(led4, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength); 
  }
}

/*this light pattern "bounces" 2 leds, mirrored in 2 groups of 3
  for a number of times (int repetitions) at a given speed (int intervalLength)*/
void patternBounceMirrored(int repetitions, int intervalLength)
{
  for(int i=0; i < repetitions; i++) {
     //set leds 1 and 6 on
     digitalWrite(led1, HIGH);
     digitalWrite(led6, HIGH);
     //set all other leds off
     digitalWrite(led2, LOW);
     digitalWrite(led3, LOW);
     digitalWrite(led4, LOW);     
     digitalWrite(led5, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);   
     
     //set leds 2 and 5 on
     digitalWrite(led2, HIGH);
     digitalWrite(led5, HIGH);
     //set leds 1 and 6 off
     digitalWrite(led1, LOW);
     digitalWrite(led6, LOW);
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);   
     
     //set leds 3 and 4 on
     digitalWrite(led3, HIGH);
     digitalWrite(led4, HIGH);
     //set leds 2 and 5 off
     digitalWrite(led2, LOW);
     digitalWrite(led5, LOW);     
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);  
    
     //set leds 2 and 5 on
     digitalWrite(led2, HIGH);
     digitalWrite(led5, HIGH);
     //set leds 3 and 4 off
     digitalWrite(led3, LOW);
     digitalWrite(led4, LOW);     
     //wait for number of miliseconds in argument "intervalLength"
     delay(intervalLength);    
  }
}

// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()                     
{
  patternAllBlink(3, analogRead(pot1));            //blink all leds 5 times at 1/2 second intervals
  patternAlternateOddsEvens(3, analogRead(pot1));  //alternate odds and evens 5 times at 1/2 second intervals
  patternBounceSpaced(3, analogRead(pot1));        //bounce spaced leds 3 times at 100 ms intervals
  patternBounceTwo(3, analogRead(pot1));           //bounce 2 adjacent leds 3 times at 100 ms intervals
  patternBounceMirrored(3, analogRead(pot1));      //bounce 2 mirrored leds 3 times at 100 ms intervals
}

Comments