Homework 04 – Combine Circuits – Josiah Song

5 Nov

ImageImageImage

I used the pushbutton, potentiometer, and sensor to control the LEDs. The button turns on the circuit, while the sensor determines the brightness of the LEDs. The potentiometer controls the speed that the LEDs blink.

int red = 13;//set red LED to pin 13
int blue = 6;//set blue LED to pin 6
int green = 5;//set green LED to pin 5
int button = 7;//set button to pin 7
int pot = 0;//analog pin used to connect the potentiometer
int value = 0;//setting variable value
int previous_value = 0;//store previous value
int state = 0;//state variable where 0 means off and 1 means on

void setup() {
  Serial.begin(9600);
  pinMode(red, OUTPUT);//set pin 13 to output
  pinMode(blue, OUTPUT);//set pin 6 to output
  pinMode(green, OUTPUT);//set pin 5 to output
  pinMode(button, INPUT);//set pin 7 to input
}

void loop() {//begin loop
  value = digitalRead(button);//set val to reading at pin 7
  if ((value==HIGH) && (previous_value==LOW)){
    state = 1 – state;
    delay(10);
  }
 
  previous_value = value;//set previous value to value
 
  if (state==1) {//do something if state is equal to 1
    int sensorReading = analogRead(A1);//set variable to read motion sensor
    Serial.println(sensorReading);//prints the reading to serial monitor
    pot = (analogRead(A0));
    analogWrite(red, sensorReading);//sets brightness based on motion sensor
    delay(pot);//delay based on potentiometer
    analogWrite(red, LOW);//turns led off
    delay(pot);//delay based on potentiometer
    analogWrite(blue, sensorReading);//sets brightness based on motion sensor
    delay(pot);//delay based on potentiometer
    analogWrite(blue, LOW);//turns led off
    delay(pot);//delay based on potentiometer
    analogWrite(green, sensorReading);//sets brightness based on motion sensor
    delay(pot);//delay based on potentiometer
    analogWrite(green, LOW);//turns led off
    delay(pot);//delay based on potentiometer
  } else{
    analogWrite(red, LOW);//send low signal to red led
    analogWrite(blue, LOW);//send low signal to blue led
    analogWrite(green, LOW);//send low signal to green led
  }
}

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