Wednesday, October 23, 2024

Final Week's Challenge: Blinking Lights with Zazzle

My goal for this final project was to get one light to be always on, two additional lights to alternate, and the fourth light to randomly come on and off.

The setup was straightforward.  Could be there was a better way to wire this, but I just repeated the same pattern four times.





Looking at the code, the first three LEDs were able to be programmed as expected, but in order to make the fourth LED alternate, I needed to store its current state (1 == HIGH) by declaring the variable outside both functions.

/*
Blinking Lights with Zazzle

Light 13 is always on
Lights 12 and 11 alternate
Light 10 turns on or off randomly
*/

// Declare this variable out here so that we can use it in both functions
int currentState = 1;

I ran into some trouble getting the random number generator to work so I ended up having to display the values to the serial monitor.  The code is now commented out, but I left it in for good measure.  I also put in a random seed so the pattern would be different every time.

void setup() {
  // initialize digital pins as an output
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);

  // Start the serial communication
  // Commenting this out - this was for troubleshooting
  // Serial.begin(9600);

  randomSeed(analogRead(A0)); // set the random seed to a pin we're not connected to

  digitalWrite(13,HIGH);  // set first red LED on permanently
  digitalWrite(10,currentState);  // set second red LED on temporarily
}

It turns out that the random() function in Arduino always generates integers, which was not what I wazs expecting.  As such, I told it to randomly generate a number from 1 to 100, and then for values greater than 50, to toggle the light.  The modulo arithmetic (with the percent sign) was an alternative so I didn't have to put in another if statement.  You can also see where I commented out the serial communication again.

void loop() {

  // declare randomNumber as integer
  int randNumber = random(1,100);

  // Print the random number to the serial monitor
  // Commenting this out - this was for troubleshooting
  // Serial.println(randNumber);

  if (randNumber > 50){
      currentState = (currentState + 1) % 2; // flips it on vs off
      digitalWrite(10,currentState);   // set second red LED on
  }

  digitalWrite(12,HIGH);  // set first yellow LED on
  digitalWrite(11,LOW);   // set second yellow LED off
  delay(500);            // wait half a second

  digitalWrite(12,LOW);  // set first yellow LED off
  digitalWrite(11,HIGH);   // set second yellow LED on
  delay(500);            // wait another half a second
}

All in all, I finally got everything working, and I'm happy with the result!



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 ...