Combination Lock Study

A twist on this week’s combination assignment to create a single LED combination lock, using the colors green & red.  I am using a digital read switch button and an analog potentiometer range.

CASE 1: The switch turns on the color GREEN and locks it,  until the potentiometer turns all the way to the end, making it turn into RED.

CASE 2: vice versa

int brightness = 0;
int potPin = A0;
int switchPin = 2;

int green = 10;
int red = 9;
int val;
int val2;
int switchValue;
int mode = 0;
void setup() {

pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
pinMode(switchPin, INPUT);

switchValue = digitalRead(switchPin); }
void loop() {
val = digitalRead(switchPin);
delay(10);
val2 = digitalRead(switchPin);
if (val == val2) {
if (val != switchValue) {
if (val == LOW) {
if (mode == 0) {
mode = 1;
} else {
if (mode == 1) {
mode = 0;}
}
}
}
}
switchValue = val;brightness = analogRead(potPin);
if (mode == 0) {
analogWrite(red, brightness / 4);
}

if (mode == 1) {
analogWrite(green, brightness / 4);}

}