Friday, November 1, 2024

Potentiometers: Extension Challenge 1 - Blinking Lights

Challenge 1: Can you control two lights with the same blink rate?

This challenge wasn't too bad because we just had to go back to using the digital pins and set them to alternate:

int ledPin1 = 13;      // Connect LED to digital pin 13
int ledPin2 = 12;      // Connect LED to digital pin 12

and later...

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

and

  digitalWrite(ledPin1, 255);       // Turn the LED on
  digitalWrite(ledPin2, 0);       // Turn the LED on
  delay(sensorValue);             // Pause for sensorValue milliseconds
 
  digitalWrite(ledPin1, 0);      // Turn the LED off
  digitalWrite(ledPin2, 255);       // Turn the LED on
  delay(sensorValue);             // Pause for sensorValue milliseconds

This worked both in real life and in Tinkercad, which is where I'm getting the drawing from.


Challenge 2: Can you control two lights with one potentiometer but have them controlled where one blinks fast while the other blinks slow?

This one was definitely more difficult, unless you just do something where the LED 1 blinks quickly, the LED 2 blinks slowly, then LED 1 blinks quickly again, the LED 2 blinks slowly again.  That can be achieved just by changing up the order and adding in a variable.

  delay1 = sensorValue;
  delay2 = delay1/20;

and

  digitalWrite(ledPin1, 255);     // Turn the LED on
  delay(delay1);                  // Pause for delay1 milliseconds
  digitalWrite(ledPin1, 0);       // Turn the LED off
  delay(delay1);                  // Pause for delay1 milliseconds
 
  digitalWrite(ledPin2, 0);       // Turn the LED on
  delay(delay2);                  // Pause for delay2 milliseconds
  digitalWrite(ledPin2, 255);     // Turn the LED on
  delay(delay2);                  // Pause for delay2 milliseconds

But this isn't really what we want!  (Or maybe it is, but let's see if we can get them blinking at different rates independently of each other.  

We're going to need to keep track of when the elements are turning on and off.  To do this, we need to declare some new global variables.  We're going to use unsigned long because it's a data type that can hold positive whole numbers up to about 4.29 billion. Since we're using milliseconds, our numbers will always be positive whole numbers, and if we go above 4.29 billion seconds, we've spent way too much time on this assignment.

// Variables to track the timing for each LED
unsigned long previousTime1 = 0;   // Store last update time for LED1
unsigned long previousTime2 = 0;   // Store last update time for LED2

Now going into the void loop() function, we want to check and see what time it is.  The function millis() returns the number of milliseconds since the Arduino board was powered on or reset.

  unsigned long currentTime = millis();   // Get the current time

To set the delay on the second LED, we'll just use the complementary value (that gets us to 1024).  This means that if we set the dial in the middle, both LEDs should flash at approximately the same rate.

  delay1 = sensorValue;
  delay2 = (1024 - delay1);

Then essentially what we need to do is, if it's been the right amount of time, we need to see what the current state of the LED is and set it to the opposite of that, using the ! operator.  Then we reset our previousTime to the current time and continue in the loop.

// Check if it's time to toggle LED 1
  if (currentTime - previousTime1 >= delay1) {
    previousTime1 = currentTime;    // Update last toggle time for LED 1
   
    // Read the current status of the pin
    currentStatus = digitalRead(ledPin1);
   
    // Flip it to the opposite.
    digitalWrite(ledPin1, !currentStatus);
  }

We do this for both LED 1 and LED 2 and get this.


We notice that it's weird because the LED that is ON is actually flashing along with the other LED.  This actually makes sense because they're sharing a resistor and on the same circuit.  If we want them to behave entirely independently, we need to wire them entirely separately.


With that adjustment, we can get the two lights blinking at related rates, independently of each other.



And for fun, here is the full swatch of code:

/*
Use Potentiometer to control blinking speed
*/

// Use global variables for pin numbers
int sensorPin = A0;    // Connect potentiometer to analog pin A0
int ledPin1 = 13;      // Connect LED to digital pin 13
int ledPin2 = 12;      // Connect LED to digital pin 12

// Variables to track the timing for each LED
// These are the last times we've changed the status of the LED
// Initially set these to zero
unsigned long previousTime1 = 0;  
unsigned long previousTime2 = 0;  

void setup() {

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
 
  // Assuming if there's an OUTPUT there's also an INPUT
  pinMode(sensorPin, INPUT);

  // Do this so we get outputs for troubleshooting if necessary
  Serial.begin(9600);
}


void loop() {
 
  // Declare integer sensorValue to read in each time
  int sensorValue;
  int currentStatus;    
  int delay1;
  int delay2;                  

  unsigned long currentTime = millis();   // Get the current time

  // Let's assume since there was an analogRead() that there's also a digitalRead()
  sensorValue = analogRead(sensorPin);    
  delay1 = sensorValue;
  delay2 = (1024 - delay1);

  // Print the sensorValue to the serial monitor
  // Serial.println(delay1);
  // Serial.println(delay2);

// Check if it's time to toggle LED 1
  if (currentTime - previousTime1 >= delay1) {
    previousTime1 = currentTime;    // Update last toggle time for LED 1
   
    // Read the current status of the pin
    currentStatus = digitalRead(ledPin1);
   
    // Flip it to the opposite.
    digitalWrite(ledPin1, !currentStatus);
  }

// Check if it's time to toggle LED 2
  if (currentTime - previousTime2 >= delay2) {
    previousTime2 = currentTime;    // Update last toggle time for LED 2
   
    // Read the current status of the pin
    currentStatus = digitalRead(ledPin2);

    // Flip it to the opposite.
    digitalWrite(ledPin2, !currentStatus);
  }
 
}


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