Video

Homework 04 – Combine Circuits – Daniel Scott

7 Nov

I used the button to control the green LED – which triggered the LDR which affected the pitch of the buzzer. Also controlled by the button were the blue and red LED which made the buzzer tick when the button was pushed to turn the green LED off. Code as follows:

int LED = 13; //green light pin
int red = 2; //red LED Pin
int blue = 3; //Green LED Pin
int button = 7; //Button Pin
int value = 1; //LDR pin
int state = 0; // read button state
int previous_value = 0; //read previous button state

void setup() {
pinMode(LED, OUTPUT); //green led is output
pinMode(red, OUTPUT);//red led is output
pinMode(blue, OUTPUT);//blue led is output
pinMode(button, INPUT);//button contols both light systems
Serial.begin(9600);//read LDR values

}
void loop(){
value =digitalRead(button);
if((value == HIGH) && (previous_value == LOW))
{
state = 1 – state;
delay(100);
}
previous_value = value;
if (state == 1) {
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
{
digitalWrite(red, HIGH); //light on
delay(200); //light on, LDR controls light on time
digitalWrite(red, LOW); //light off
delay(200); //light off, LDR controls light off time
digitalWrite(blue, HIGH); //light on
delay(200); //light on, LDR controls light on time
digitalWrite(blue, LOW); //light off
delay(200); //light off, LDR controls light off time
}
}{
int sensorReading =analogRead(A1);//input for LDR value
Serial.println(sensorReading);//outputs data values for LDR
int thisPitch =map(sensorReading, 570, 850, 370, 800); //values to change tones
tone(9, thisPitch, 20); //pitch
delay(1); //
}
}

Leave a comment