Seesaw Effect Using LEDs

Solving Approach:

How do you plan to solve it?

I identified that the key to solve this task is to know how can I increase the bright of a LED at the same time it decreases for another one?

To increase the brightness is a very straightforward method using the following code line:   

analogWrite(led0, pwm);

 Where led0, is the number of LED and pwm a parameter that comes from an ADC conversion, scaled to work in a range of 0 to 255 (using the map function).

Then to decrease the brightness is a little bit different because you have to do 255 - pwm. In this way when you get pwm = 255, by subtracting 255 - pwm it is equeal to 0 and this is the main structure to design the whole program. When one LED has its full bright the other one is theoretically off. 

The last step to achieve a seesaw effect is to control the LED1 and LED3 because they are never completelly OFF or ON, so I set that they are within a range of pwm values of 55 and 200 thats why I used the "constrain" function to get that range and to avoid negative numbers whrn the substract has done.

Code

/*Paste your code here*/

// Declaramos pines PWM donde 3,9,10 y 11 tienen 490Hz por default
// mienttras que 5 y 6 tienen 980Hz a su salida
const char leds[] = {3,5,6,9,10};
char pot = A0;  // Declaramos el potenciometro en el canal analogico A0

void balancinLED(int); // De claramos el prototipo de una funcion para manejar el efecto balancin

void setup() {
  // Configuramos los pines PWM como salidas digitales
for(int i=0; i<5; i++) pinMode(leds[i], OUTPUT);
// No es necesario configurar la entrada analogica como tal
}

void loop() {
int pwm = map(analogRead(pot), 0, 1023, 0, 255); // Obtenemos y mapeamos la conversion ADC de 0 a 1023 en un rango valido para el PWM de 0 a 255

balancinled(pwm);  // Pasamos el valor de la conversion como parametro de la funcion que realiza la logica del efecto balancin

}

void balancinled(int pwm){

 analogWrite(leds[0], pwm);
 analogWrite(leds[1], constrain(pwm, 55, 200));
 analogWrite(leds[2], 80);
 analogWrite(leds[3], constrain(255-pwm, 150, 200));
 analogWrite(leds[4], 255-pwm);
 delay(1);

}

Output

Video

Add video of output (know more)

 

 

 

 

Photo of Output

Add a photo of your hardware showing the output.

 

 

 

Upvote
Downvote

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!