// name: Assignment 5 Jui Hong
// purpose: to create a monophonic synthesizer using the Arduino, the potentiometer and the pushbutton
// commentary: I have no idea of how to connect buzzer and potentionmeter......
// author: Jui Hong
// ip/domain: https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody,
// https://www.arduino.cc/reference/en/language/variables/conversion/intcast/,
// https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/,
// https://www.arduino.cc/reference/en/language/functions/time/delay/,
// https://www.arduino.cc/reference/en/language/structure/further-syntax/include/,
// https://www.arduino.cc/reference/en/language/functions/communication/serial/,
// https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/,
// https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/
// Released under MIT license
// variables
int analog_in = A0; // from a potientiometer voltage divider
int adc_reading = 0; // used to acquire the adc reading from the analogue input
int interval = 1; // the interval used to flash the LED
int controltone = 0; // analog pin used to connect the potentiometer
int val;
#include "pitches.h"
// notes in the melody:
int melody[] = {
sound_C, sound_D, sound_E, sound_F, sound_G, sound_A,
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8, 8, 8, 8, 8, 8
};
void setup() {
Serial.begin(9600);
//pinMode(LED_BUILTIN, OUTPUT);
(adc_reading) = analogRead(analog_in); // get the analogue voltage present at the analogue input pin
interval = map(adc_reading, 0, 1023, 31, 988); // scale the values
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
(val) = analogRead(controltone);
adc_reading = analogRead(analog_in); // get the analogue voltage present at the analogue input pin
interval = map(adc_reading, 0, 1023, 100, 1000); // scale the values
digitalWrite(LED_BUILTIN, HIGH); // activate the led
delay(interval); // delay for a limited period of time
digitalWrite(LED_BUILTIN, LOW); // inactivate the led
delay(interval); // delay for a limited period of time
Serial.println(interval); // confirmation message for mapped reading
}