Approach
For this assignment, I prepared 2 separate codes. Most of them are merged and modified from codes I submitted for previous assignments.
For Code 1, the system receives signal from FSR, and one of the LED will light up depending on the strength exerted. This also translates to the notes the speaker will play. User can also choose the LED color by adjusting the potentiometer. Unfortunately, due to time constraint, my code only can allow the user to pick 1 color at a time.
For Code 2, the system will ask the user to enter the notes. Different notes will light up different LEDs. The user can use the potentiometer to adjust the brightness of the light after each interval.
Future goal is goal is to include Processing, and have different notes show up as different visual effect on screen to make the system more entertaining to play with. More like a visual musicbox?
Materials
Arduino UNO
Solderless breadboard
Red, Green, Blue LED
220 ohm resistor x 3
Potentiometer x 1
10k ohm resistor
USB cable, Jameco
Force sensitive resistor
Piezo speaker
Code 1
int pot1Pin = 5; // select the input pin for the potentiometer
int pot1Val = 0; // variable to store the value coming from pot
int led1Pin = 9; // select the pin for the LED 1
int led2Pin = 11; // select the pin for the LED 2
int led3Pin = 10; // select the pin for the LED 3
int potPin = 0; // select the input pin for the potentiometer
int speakerPin = 7;
int val = 0;
void setup() {
pinMode(led1Pin, OUTPUT); // declare the led1Pin as an OUTPUT
pinMode(led2Pin, OUTPUT); // declare the led2Pin as an OUTPUT
pinMode(led3Pin, OUTPUT); // declare the led3Pin as an OUTPUT
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
pot1Val = analogRead(pot1Pin); // read the value from pot 1, between 0 - 1024, for dimming
if (pot1Val <=300) { // condition 1 to turn on led2pin
analogWrite(led1Pin, val); // dim LED to value from pot1
analogWrite(led2Pin, 0);
analogWrite(led3Pin, 0);
}
else if (pot1Val > 301 && pot1Val <=600) { // condition 2 to turn on led1pin
analogWrite(led2Pin, val);
analogWrite(led1Pin, 0);
analogWrite(led3Pin, 0);
}
else if (pot1Val > 600) { // condition 3 to turn on led3pin
analogWrite(led3Pin, val);
analogWrite(led1Pin, 0);
analogWrite(led2Pin, 0);
}
digitalWrite(speakerPin, LOW);
val = analogRead(potPin); // read value from the sensor
val = val*2; // process the value a little
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(val);
digitalWrite(speakerPin, LOW);
delayMicroseconds(val);
}
}
Code 2
int led1Pin = 9; // select the pin for the LED 1
int led2Pin = 11; // select the pin for the LED 2
int led3Pin = 10; // select the pin for the LED 3
int potPin = 5; // select the input pin for the potentiometer
int potVal=0 ;
int speakerPin = 7;
// note names and their corresponding half-periods
byte names[] ={ 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
int serByte = -1;
int ledState = LOW;
int count = 0;
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(led1Pin, OUTPUT); // declare the led1Pin as an OUTPUT
pinMode(led2Pin, OUTPUT); // declare the led2Pin as an OUTPUT
pinMode(led3Pin, OUTPUT); // declare the led3Pin as an OUTPUT
Serial.begin(9600);
Serial.println("Ready. Press N to turn off.");
}
void loop() {
digitalWrite(speakerPin, LOW);
serByte = Serial.read();
if (serByte !=1) {
if (serByte == 'c' || serByte == 'f' ||serByte == 'b') {
Serial.print(serByte);
ledState = !ledState;
digitalWrite(led1Pin, ledState);
analogWrite(led1Pin, potVal/4);
digitalWrite(led3Pin, 0);
digitalWrite(led2Pin, 0);
}
else if (serByte == 'd' || serByte == 'g'||serByte == 'C') {
Serial.print(serByte);
ledState = !ledState;
digitalWrite(led2Pin, ledState);
analogWrite(led2Pin, potVal/4);
digitalWrite(led1Pin, 0);
digitalWrite(led3Pin, 0);
}
else if (serByte == 'e' || serByte == 'a') {
Serial.print(serByte);
ledState = !ledState; // flip the LED state
digitalWrite(led3Pin, ledState); // write to LED
analogWrite(led3Pin, potVal/4);
digitalWrite(led1Pin, 0);
digitalWrite(led2Pin, 0);
}
}
if (serByte == 'N'){
digitalWrite(led3Pin, 0);
digitalWrite(led2Pin, 0);
digitalWrite(led1Pin, 0);
}
for (count=0;count<=8;count++) { // look for the note
if (names[count] == serByte) { // ahh, found it
for( int i=0; i<50; i++ ) { // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tones[count]);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tones[count]);
potVal = analogRead(potPin); // read the value from the sensor, between 0 - 1024
}
}
}
}
- Login to post comments