Week 3 Li: Digital/Analogue Input/Output

Concept:

The idea behind this project is to use two LEDs and one buzzer as outputs, and the potentiometer and a button as inputs. When the button is pressed, the LEDs start blinking. The rate of blinking corresponds to the value of the potentiometer. The buzzer plays tones in sync with the blinking, and with a faster rate of blinking the frequency of the notes increases too.

video demonstration

Circuit Schematic:

Problems/Solutions:

When using the button, I had forgotten how to wire it and used the circuit given in the Arduino booklet, which stated to connect one end to the ground and the other directly to the input on the board, which did not work for me. I tried it with different buttons, wires, and input slots but nothing seemed to work and it gave random results. Eventually I looked at the video from last week’s assignment to recreate the wiring the way we learned it in class. 

Code:

const int potentiometer = A5;
const int blueLED = 4;
const int redLED = 3;
const int buzzer = 10;
int buttonState = 0;
int sensorValue = 0;
const int button = 8;

void setup() {
pinMode(blueLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(button, INPUT);
Serial.begin(9600);
}

#define c 261
#define d 294
#define e 329
#define f 349
#define g 392
#define a 440
#define h 493
#define C 523

void loop() {
sensorValue = analogRead(potentiometer);
sensorValue = map(sensorValue, 0, 1023, 1, 5);

if (digitalRead(button) == true) {
if (sensorValue == 1) {
digitalWrite(blueLED, LOW);
digitalWrite(redLED, HIGH);
tone(buzzer, 261);
delay(700);
digitalWrite(blueLED, HIGH);
digitalWrite(redLED, LOW);
tone(buzzer, 294);
delay(700);
}
else if (sensorValue == 2) {
digitalWrite(blueLED, LOW);
digitalWrite(redLED, HIGH);
tone(buzzer, e);
delay(400);
digitalWrite(blueLED, HIGH);
digitalWrite(redLED, LOW);
tone(buzzer, f);
delay(400);
}
else if (sensorValue == 3) {
digitalWrite(blueLED, LOW);
digitalWrite(redLED, HIGH);
tone(buzzer, g);
delay(200);
digitalWrite(blueLED, HIGH);
digitalWrite(redLED, LOW);
tone(buzzer, a);
delay(200);
}
else if (sensorValue == 4) {
digitalWrite(blueLED, LOW);
digitalWrite(redLED, HIGH);
tone(buzzer, h);
delay(100);
digitalWrite(blueLED, HIGH);
digitalWrite(redLED, LOW);
tone(buzzer, C);
delay(100);
}}
else {
noTone(buzzer);
digitalWrite(blueLED, HIGH);
digitalWrite(redLED, HIGH);
}}

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *