4 Commits

Author SHA1 Message Date
Sam Perry bd49e8758a Added comments to code 2016-10-01 20:58:14 +01:00
Sam Perry 619eaa12d9 Implemented keyboard 2016-10-01 20:50:00 +01:00
Sam Perry 75cdda61df Added digital button inputs 2016-10-01 18:14:57 +01:00
Sam Perry 9c6faddf30 Implemented FSR modulated tone generation 2016-10-01 14:22:01 +01:00
+52 -13
View File
@@ -9,21 +9,60 @@
void setup()
{
// initialize serial comms
Serial.begin(9600);
// Communication with the computer is not needed in this project.
//Serial.begin(9600);
// Set digital pins as inputs ready to read incoming data from buttons.
for(int i = 5; i < 13; ++i) {
pinMode(i, INPUT);
}
}
// Define integer frequencies in hertz from A (440hz) chromatically.
int notes[8] = {440, 466, 493, 523, 554, 587, 622, 659};
// Define a a global variable for pitch.
int pitch;
void loop()
{
// read A0
int val1 = analogRead(0);
// read A1
int val2 = analogRead(1);
// print to serial
Serial.print(val1);
Serial.print(" ");
Serial.print(val2);
Serial.print("\n");
// wait
delay(50);
// Declare a bool for testing if button is active.
bool active = false;
// For each button index, check if button is currently pressed...
int j = 0;
for(int i = 5; i < 13; ++i) {
active = digitalRead(i);
// If it is, set the output pitch to the frequency stored in the notes
// variable at the index of the button pressed
if(active) {
pitch = notes[j];
// break out of for loop as further iterations are not neccesary.
break;
}
// If note isn't active then increment index counter.
++j;
}
// If no note is active then silence instrument.
if(!active) {
noTone(13);
}
// Otherwise, output a tone.
else {
tone(13,pitch,20);
}
// Disable FSR for now...
/*
int sensorVal = analogRead(0);
// Scale value read from between the known minimum and maximum values of
// the sensor, to a desired range (in Hz)
pitch = map(sensorVal, 0, 1023, 0, 2000);
// Output a tone, via digital output 13, to the speaker.
// The 3rd arguments represents the duration of the tone in milliseconds.
// Pause for 50 milliseconds
delay(50);
*/
}