Friday, November 1, 2024

Potentiometers: Extension Challenge 2 - Fading Lights

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

The bright and fading lights can probably be controlled via the analog pins instead of the digital ones.  Based on what we've learned before, we know that the brightness is controlled from 0 to 255, but the potentiometer is going to give us a reading from 0 to 1023.

This means, we'll need to adjust the value from the potentiometer.  This is going to be a bit annoying because of integer arithmetic.  For example, if we read the number 100 from the potentiometer, to scale it on a range from 0 to 255, we would normally just say "100 divided by 1024 times 255."  Unfortunately, since 100 is stored as an integer, doing 100 / 1024 gives us zero, because integer division takes the floor function (rounds down) to the nearest integer.  So zero times 255 would have to still be zero.

To fix this, we need to trick the computer into doing "normal arithmetic" and then convert that answer back to an integer to store it.  Using the value 1024.0 tricks the computer into doing normal arithmetic, and then we use static_cast<int>() to convert the answer from a double back to an integer.

  sensorValue = static_cast<int>((sensorValue / 1024.0) * 255);

We can now make the brightness of both LEDs increase or decrease together.  

However, this still didn't work! 

Through some Googling, we found that we need to be using the pins marked with a ~ on the board, since that means they are PWM (Pulse Width Modulation) outputs, which allow you to vary the levels of power by turning the signal on and off very quickly.  Once we make that change, then everything works great!


Challenge 2: With just one potentiometer can you control two lights so that as one light gets dimmer, the other gets brighter?

Absolutely!  Now that we have this done, instead of having the brightness values match, we make value1 = 255 - value2.

brightness2 = 255 - sensorValue;

Let's see this happen!


And here we can see the circuit diagram and the code:


/*
Use Potentiometer to control blinking speed
*/

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

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 brightness1;
  int brightness2;                  

  // Let's assume since there was an analogRead() that there's also a digitalRead()
  sensorValue = analogRead(sensorPin);    
  sensorValue = static_cast<int>((sensorValue / 1024.0) * 255);
 
  brightness1 = sensorValue;
  brightness2 = 255 - sensorValue;


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

  analogWrite(ledPin1, brightness1);
  analogWrite(ledPin2, brightness2);
 
}


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