When we looked into project 6, we thought it would be fun to add more photoresistors, so we made a few changes to the project to be able to control 4 photoresistors which allow a wider range of possibilities. We also added 4 leds to show which photoresistor is used to play the sound.
We made a short video to show how it work.
We found this tool : circuits.io on the web to generate the schema but were unable to use the exact same design as our board (piezo component has been replaced by a capacitor). So we're unsure of the accuracy of the design, but you might find this tool useful.
We added a photo of the board and the generated schema as attachments in this post.
Here are the changes we made to the code :
First we declared our constants and arrays of values. To get notes frequencies, we used this website : http://www.phys.unsw.edu.au/jw/notes.html
const int SENSORS_COUNT = 4;
const int soundLedPins[SENSORS_COUNT] = {4, 5, 6, 7};
const int lowSensorValues[SENSORS_COUNT] = {262, 349, 466, 622};
const int hiSensorValues[SENSORS_COUNT] = {330, 440, 587, 784};
Then in the setup() function we added the following loop, which allow us to output to the leds:
for (int i = 0; i < SENSORS_COUNT; i++)
{
pinMode(soundLedPins[i], OUTPUT);
}
We changed the loop function that way :
void loop()
{
int values[SENSORS_COUNT];
values[0] = analogRead(A0);
values[1] = analogRead(A1);
values[2] = analogRead(A2);
values[3] = analogRead(A3);
int sensorValue = 1023;
int sensorIndex = -1;
for (int i = 0; i < SENSORS_COUNT; i++)
{
if (values[i] < sensorValue)
{
sensorValue = values[i];
sensorIndex = i;
}
}
if (sensorIndex >= 0)
playSound(sensorValue, sensorIndex);
}
and created a function to play sound based on sensor value and index :
void playSound(int value, int sensorIndex)
{
// define pitch based on sensor value and calibration values
int pitch = map(value, sensorLow, sensorHigh, lowSensorValues[sensorIndex], hiSensorValues[sensorIndex]);
// play the tone for 20 ms on pin 8
tone(8, pitch, 20);
// leds
int pinIndex = SENSORS_COUNT - sensorIndex - 1;
digitalWrite(soundLedPins[pinIndex], HIGH);
delay(10);
digitalWrite(soundLedPins[pinIndex], LOW);
}
We hope you will like it, if you have any question don't hesitate to reply to this post !