Pcom HW week 3-Grace: Combination lock

IMG_1660

I decided to take a twist on this week’s combination homework to do a single LED (the led light with 4 legs) combination lock. using the colours green & red.  I am using a digital read switch button and an analog potentiometer range.

CASE 1: The switch turns on the colour 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);
}

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s