Thursday, November 7, 2024

Project 4 - Flashing Lights, Arrays, and For Loops

This one didn't give me too much trouble either, and I had some fun with the code!  I added a random number generator in the initialization function and then a case/switch in the loop so that it would pick one of the light patterns at random.  That was pretty neat to get working.

In the video, you can see me reloading the code onto the board and then the pattern changing.  (If you look really carefully you can see the "Case #" indicated as well so you can tell in theory what you're supposed to be looking at.  I don't think the video actually shows all the different options, but that's because it's a random number and you can't guarantee a good distribution.

One little think about the random number generator though is if you wanted a number from 1 to 5, you actually have to use this code:

option = random(1,6);

I'm guessing it's because it uses a floor function or something so it never actually gets to the upper bound, but honestly I didn't lose too much sleep over it!


And as usual, here's the circuit diagram:

And the code!

// Arduino is like C so it indexes starting at 0
int ledPins[] = {2,3,4,5,6,7,8,9};
int option;

void setup(){

  int index;
 
  for(index = 0; index <= 7; index++){
    pinMode(ledPins[index],OUTPUT);
  }

  Serial.begin(9600);
 
  randomSeed(analogRead(0));
  option = random(1,6); // Generate a random # from 1 to 5
 
} // end setup


void loop(){

  switch(option) {
    case 1:
      Serial.println("Case 1");
      oneAfterAnotherLoop();
      break;
    case 2:
      // Turn on one at a time, scrolling
      Serial.println("Case 2");
      oneOnAtATime();  
      break;
    case 3:
      // Light the LEDs middle to the edges
      Serial.println("Case 3");
      pingPong();
      break;
    case 4:
      // Chase lights like you see on signs
      Serial.println("Case 4");
      marquee();
      break;
    case 5:
      // Blink LEDs randomly
      Serial.println("Case 5");
      randomLED();
      break;
    default:
      Serial.println("Unexpected option!");
      break;
  } // end switch

} // end main loop


void oneAfterAnotherLoop()
{
  int index;
  int delayTime = 100; // milliseconds to pause between LEDs

  // Turn all the LEDs on:
  for(index = 0; index <= 7; index++)  {
    digitalWrite(ledPins[index], HIGH);
    delay(delayTime);                
  }                                  

  // Turn all the LEDs off:
  for(index = 7; index >= 0; index--)  {
    digitalWrite(ledPins[index], LOW);
    delay(delayTime);
  }              
} // end oneAfterAnotherLoop()

 
void oneOnAtATime() {
  int index;
  int delayTime = 100; // milliseconds to pause between LEDs
 
  // step through the LEDs, from 0 to 7  
  for(index = 0; index <= 7; index++) {
    digitalWrite(ledPins[index], HIGH);  // turn LED on
    delay(delayTime);                    // pause to slow down
    digitalWrite(ledPins[index], LOW);   // turn LED off
  }
} // end oneOnAtATime()

void pingPong(){
  int index;
  int delayTime = 100; // milliseconds to pause between LEDs
 
  // step through the LEDs, from 0 to 7
  for(index = 0; index <= 7; index++)  {
    digitalWrite(ledPins[index], HIGH);  // turn LED on
    delay(delayTime);                    // pause to slow down
    digitalWrite(ledPins[index], LOW);   // turn LED off
  }

  // step through the LEDs, from 7 to 0
  for(index = 7; index >= 0; index--) {
    digitalWrite(ledPins[index], HIGH);  // turn LED on
    delay(delayTime);                    // pause to slow down
    digitalWrite(ledPins[index], LOW);   // turn LED off
  }
} // end pingPong()


void marquee(){
  int index;
  int delayTime = 200; // milliseconds to pause between LEDs
                       // Make this smaller for faster switching
 
  // Step through the first four LEDs
  // (We'll light up one in the lower 4 and one in the upper 4)
 
  for(index = 0; index <= 3; index++) { // Step from 0 to 3  
    digitalWrite(ledPins[index], HIGH);    // Turn a LED on
    digitalWrite(ledPins[index+4], HIGH);  // Skip four, and turn that LED on
    delay(delayTime);                      // Pause to slow down the sequence
    digitalWrite(ledPins[index], LOW);     // Turn the LED off
    digitalWrite(ledPins[index+4], LOW);   // Skip four, and turn that LED off
  }
} // end marquee()

void randomLED(){
  int index;
  int delayTime;
 
  // The random() function will return a semi-random number each
  // time it is called. See http://arduino.cc/en/Reference/Random
  // for tips on how to make random() even more random.
 
  index = random(8);  // pick a random number between 0 and 7
  delayTime = 100;
 
  digitalWrite(ledPins[index], HIGH);  // turn LED on
  delay(delayTime);                    // pause to slow down
  digitalWrite(ledPins[index], LOW);   // turn LED off
} // end randomLED()



No comments:

Post a Comment

Nooooo, don't make me reflect on my learning!

 ... I just want to stick wires in breadboards and make froggies dance!!! Okay, but seriously, that stupid dancing frog was clearly my most ...