Homework 04 – Combine Circuits – Kierra Thomas

5 Nov

I used the pushbutton, potentiometer, LEDs and if statement in my circuit. The push button turns the circuit on and off. When the LEDs are on the blue one blinks as the red one stays constant. The potentiometer controls the speed of the LEDs. 

Image

int red = 3; //setting red variable
int blue = 5; //setting blue variable
int potpin = 0; //analog pin used to connect to potrntiometer
int button = 13; //input pin where pushbutton is connected
int val = 0; //value used to store state of input pin
int old_val = 0; //stores previous value of val
int state = 0; //0 turns LED off, 1 turns LED on
int vpotpin; //variable to read the value from the analog pin

void setup() {
pinMode(red, OUTPUT); //sets LED as output
pinMode(blue, OUTPUT); //sets LED as output
pinMode(button, INPUT); //sets button as input
}

void loop() {
val = digitalRead(button); //read input value and store it
vpotpin = analogRead(potpin); //reads the value of the potentiometer

//check if there was a transition
if ((val == HIGH) && (old_val == LOW)) {
state = 1 – state;
delay(10);
}

old_val = val; //stores val

if (state == 1) {
potpin = (analogRead(A0)); //reads value of potentometer
digitalWrite(red, HIGH); //turns red LED on
delay(potpin); //controls speed of LED through potentiometer
digitalWrite(blue, HIGH); //turns blue LED on
delay(potpin);
digitalWrite(blue, LOW);
delay(potpin);
} else {
digitalWrite(red, LOW);
digitalWrite(blue, LOW);
}
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s