Homework 04 — Combine Circuits — Alexa Zyllo

4 Nov

This Arduino does the following: Blinks a green LED light in a loop, has a blue LED light connected to a push button, and a proximity sensor connected to a buzzer. Here’s the code:

_________________________________________________________

int led = 13; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int buttonPin = 2; // the number of the pushbutton pin
int ledPin = 8; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
analogWrite(led, brightness); // set the brightness of pin 13
brightness = brightness + fadeAmount; // change the brightness for next loop
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ; // reverse the direction of the fading at the ends
}
delay(30); // wait for 30 milliseconds

int sensorReading =analogRead(A0); // read the sensor
Serial.println(sensorReading); //print the sensor reading to serial monitor
int thisPitch =map(sensorReading , 430, 810 , 120, 800); //map the anolog input range to output
tone(9, thisPitch, 10); //play this pitch
delay(1); //delay inbetween

buttonState = digitalRead(buttonPin); // read the state of the pushbutton value
if (buttonState == HIGH) { // check if the pushbutton is pressed, if it is, the buttonState is HIGH
digitalWrite(ledPin, HIGH); // turn LED on
}
else {
digitalWrite(ledPin, LOW); // turn LED off:
}

}

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