Design:
I used a slide switch as the digital sensor, and a potentiometer + photometer as the analog sensors. The slide switch was originally just an on/off switch, but now allows the instrument to switch between two modes (which currently shifts the octave). The potentiometer acts as a volume control, while the photometer provides an additional input towards the tone frequency.
The physical component is a simple sort of keyboard made from paper and copper tape. It works by passing current through one of five different resistors (1k, 10k, 100k, 470k, 1m) and using analogRead() to identify which key was pressed. The five keys correspond to the notes A4/5 through E4/5. The initial steps of wiring the circuit and writing the code were fairly painless, which was not the case for converting it into its final result. I think it definitely suffered from my lack of ability with music as well as arts and crafts.
Code:
/* Musical Instrument */ #include "pitches.h" // // Global Variables const int LIGHT_PIN = A0; const int TOUCH_PIN = A5; const int MODE_PIN = 2; const int SOUND_PIN = 8; int low[] = {NOTE_A4, NOTE_B4, NOTE_C4, NOTE_D4, NOTE_E4}; int high[] = {NOTE_A5, NOTE_B5, NOTE_C5, NOTE_D5, NOTE_E5}; void setup() { Serial.begin(9600); pinMode(LIGHT_PIN, INPUT); pinMode(TOUCH_PIN, INPUT); pinMode(MODE_PIN, INPUT); pinMode(SOUND_PIN, OUTPUT); } void loop() { // // Read digital/analog int modeVal = digitalRead(MODE_PIN); // Serial.println(modeVal); int lightVal = analogRead(LIGHT_PIN); // Serial.println(lightVal); int touchVal = analogRead(TOUCH_PIN); Serial.println(touchVal); // // Choose note based on touch input // 1k = 932 // 10k = 512 // 100k = 92 // 470k = 20 // 1m = 8 int x; int mapped; if (touchVal < 4) {noTone(SOUND_PIN); return; } else if (touchVal < 16) {x = 0;} else if (touchVal < 64) {x = 1;} else if (touchVal < 128) {x = 2;} else if (touchVal < 768) {x = 3;} else if (touchVal < 1024) {x = 4;} // Serial.println(x); // // Choose high/low range based on mode int note; if (modeVal) { note = low[x]; mapped = map(lightVal, 400, 1200, 0, 2500); } else { note = high[x]; mapped = map(lightVal, 400, 1200, 2500, 5000); } // // Play sound tone(SOUND_PIN, note); }