My first thing to do this week was to brainstorm some ideas. My most prominent one was that I really wanted to play with many lights, almost like a carousel of lights. After noting down some random notes, I decided to aim for a code in which I could have a bunch of lights, and have my analogue reader blend through the lights. In this case I wanted to use the potentiometer, just because it seemed the most appropriate for me.

Setting up the circuit was much smoother than last week for me, I knew what I was doing way more. The first thing i needed to do was get the code to work where the lights would pass through. It came really easy for me, I just mapped the analogue reads from 0 – 800, just because that could make it easily divisible by 8 (my number of lights), without changing the values too much. Then I created a bunch of if-statements on when to light up. When I first did this, it made the leds light up and not shut off. Although this was also a cool effect, it wasnt what I wanted. I figured this out using ‘else statements.’

But that gave me the idea on what to use with a digital reading. I decided that once the button was clicked, the setting would change, and the led’s would turn on one after the other without turning each off as they go. After that, I realised I faced an issue where once they were all on, there would be no way (in that setting to turn them off). While washing my dishes, I kept thinking about how to code a way to fix that, and I found it. I measured the previous analogue reading, if it was higher than the current one that means the pattern was moving down, so I set the LED’s to turn off. If the previous reading was lower, the pattern was moving up, making the LED’s turn on. This meant switching the direction of the potentiometer would change whether you are turning the lights on or off.

Finally, I started adding some small features (such as all the lights turning on at the highest reading, and debugging). Here is the final project ! (I accidentally filmed vertically)

int button = 2;
int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;
int led6 = 8;
int led7 = 9;
int led8 = 10;
bool buttonState = false;
bool prevState = false;
int knob = A0;
int prevLed = 0;

void setup() {
// put your setup code here, to run once:
pinMode(button,INPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
pinMode(led6,OUTPUT);
pinMode(led7,OUTPUT);
pinMode(led8,OUTPUT);
Serial.begin(9600);

}

void loop() {
//   put your main code here, to run repeatedly:
changeState();

//mapping the potentiometer values from 0 - 800 for easy reference
int mappedValue =  map(knobValue, 0,1023, 0, 800);

//Button setting 1: where each light only appears individually
if (buttonState == false){
if (mappedValue >= 0 && mappedValue <100){
digitalWrite(led1, HIGH);
}  else {
digitalWrite(led1, 0);
}
if (mappedValue >= 100 && mappedValue <200){
digitalWrite(led2, HIGH);
} else {
digitalWrite(led2, 0);
}
if (mappedValue >= 200 && mappedValue <300){
digitalWrite(led3, mappedValue );
} else {
digitalWrite(led3, 0);
}
if (mappedValue >= 300 && mappedValue <400){
digitalWrite(led4, mappedValue );
}  else {
digitalWrite(led4, 0);
}
if (mappedValue >= 400 && mappedValue <500){
digitalWrite(led5, mappedValue );
} else {
digitalWrite(led5, 0);
}
if (mappedValue >= 500 && mappedValue <600){
digitalWrite(led6, mappedValue );
} else {
digitalWrite(led6, 0);
}
if (mappedValue >= 600 && mappedValue <700){
digitalWrite(led7, mappedValue );
} else {
digitalWrite(led7, 0);
}
if (mappedValue >= 700 && mappedValue <800){
digitalWrite(led8, mappedValue );
} else {
digitalWrite(led8, 0);
}
//if the potentiometer is at its max, all lights turn on
if (mappedValue ==800 ){
digitalWrite(led1, mappedValue );
digitalWrite(led2, mappedValue );
digitalWrite(led3, mappedValue );
digitalWrite(led4, mappedValue );
digitalWrite(led5, mappedValue );
digitalWrite(led6, mappedValue );
digitalWrite(led7, mappedValue );
digitalWrite(led8, mappedValue );
}

}
//button state 2, where the lights turn on and off after one another
if (buttonState == true){
//if analogue is increasing, they are turning on
if (prevLed <= mappedValue){
if (mappedValue >= 0 && mappedValue <100){
digitalWrite(led1, HIGH);
prevLed = mappedValue;
}
if (mappedValue >= 100 && mappedValue <200){
digitalWrite(led2, HIGH);
prevLed = mappedValue;
}
if (mappedValue >= 200 && mappedValue <300){
digitalWrite(led3, mappedValue );
prevLed = mappedValue;
}
if (mappedValue >= 300 && mappedValue <400){
digitalWrite(led4, mappedValue );
prevLed = mappedValue;
}
if (mappedValue >= 400 && mappedValue <500){
digitalWrite(led5, mappedValue );
prevLed = mappedValue;
}
if (mappedValue >= 500 && mappedValue <600){
digitalWrite(led6, mappedValue );
prevLed = mappedValue;
}
if (mappedValue >= 600 && mappedValue <700){
digitalWrite(led7, mappedValue );
prevLed = mappedValue;
}
if (mappedValue >= 700 && mappedValue <800){
digitalWrite(led8, mappedValue );
prevLed = mappedValue;
}
//if analogue is decreasing, they are turning off
} else if (prevLed >= mappedValue){
if (mappedValue >= 0 && mappedValue <100){
digitalWrite(led1, 0);
prevLed = mappedValue;
}
if (mappedValue >= 100 && mappedValue <200){
digitalWrite(led2, 0);
prevLed = mappedValue;
}
if (mappedValue >= 200 && mappedValue <300){
digitalWrite(led3,0 );
prevLed = mappedValue;
}
if (mappedValue >= 300 && mappedValue <400){
digitalWrite(led4, 0);
prevLed = mappedValue;
}
if (mappedValue >= 400 && mappedValue <500){
digitalWrite(led5, 0 );
prevLed = mappedValue;
}
if (mappedValue >= 500 && mappedValue <600){
digitalWrite(led6, 0 );
prevLed = mappedValue;
}
if (mappedValue >= 600 && mappedValue <700){
digitalWrite(led7, 0);
prevLed = mappedValue;
}
if (mappedValue >= 700 && mappedValue <800){
digitalWrite(led8, 0 );
prevLed = mappedValue;
}
}
}

Serial.print(knobValue);
Serial.print(" ");
Serial.println(mappedValue);
}

//toggling the button state
void changeState() {
if (digitalRead(button) == HIGH && prevState == LOW) {
if (buttonState) {
buttonState= false;
} else if (!buttonState) {
buttonState= true;
}
}