Design changes Link to heading
the previous week I decided to revise the model in order to enhance usability. I moved the USB port closer to the edge in order to allow easier access even for bulkier USBs. I increased the size of the copper pads in order for it to be more comfortable to play.
Below is a demo video of the mini piano:
Possible improvements Link to heading
There are a couple of things that can be taken away from this project and that will help me with my final project. I should plan to add a battery to my final project in order to improve portability. This will most likely imply using a two sided board in order to access the additional powering pins on the back of the microcontroller.
Another possible improvement would be to improve the code used to play music so that I would be able to play full chords instead of just individual tones.
The final concern is the size of the speaker, it seems pretty unwieldy for my final project. I should search around the shop for smaller equivalents.
Code Link to heading
#include "Adafruit_FreeTouch.h"
Adafruit_FreeTouch t6 = Adafruit_FreeTouch(A6,OVERSAMPLE_64,RESISTOR_100K,FREQ_MODE_NONE);
Adafruit_FreeTouch t7 = Adafruit_FreeTouch(A7,OVERSAMPLE_64,RESISTOR_100K,FREQ_MODE_NONE);
Adafruit_FreeTouch t8 = Adafruit_FreeTouch(A8,OVERSAMPLE_64,RESISTOR_100K,FREQ_MODE_NONE);
Adafruit_FreeTouch t9 = Adafruit_FreeTouch(A9,OVERSAMPLE_64,RESISTOR_100K,FREQ_MODE_NONE);
Adafruit_FreeTouch t10 = Adafruit_FreeTouch(A10,OVERSAMPLE_64,RESISTOR_100K,FREQ_MODE_NONE);
#define delay(n) {volatile uint32_t i; for (i = 0; i < n; ++i) {}}
#define pwm_cycle_sec 5.7e-6 // for on+off count = 10
#define SPEAKER_PORT PORT_PA02
#define TOUCH_THRESHOLD 100
#define NOTE_TIME 0.05
int t6min,t7min,t8min,t9min,t10min;
void setup() {
REG_PORT_DIR0 = SPEAKER_PORT; // set pin to output
t6.begin();
t7.begin();
t8.begin();
t9.begin();
t10.begin();
t6min = t7min = t8min = t9min = t10min = 1e6;
}
void play_note(uint32_t frequency_Hz, float ontime_sec) {
uint32_t wave_cycle,wave_cycle_count = frequency_Hz*ontime_sec;
uint32_t on_count = 5,off_count = 5;
uint32_t pwm_cycle,pwm_cycle_count = 1/(frequency_Hz*pwm_cycle_sec);
for (wave_cycle = 0; wave_cycle < wave_cycle_count; ++wave_cycle) {
for (pwm_cycle = 0; pwm_cycle < pwm_cycle_count; ++pwm_cycle) {
PORT_IOBUS->Group[0].OUTSET.reg = SPEAKER_PORT;
delay(on_count)
PORT_IOBUS->Group[0].OUTCLR.reg = SPEAKER_PORT;
delay(off_count)
}
for (pwm_cycle = 0; pwm_cycle < pwm_cycle_count; ++pwm_cycle) {
PORT_IOBUS->Group[0].OUTCLR.reg = SPEAKER_PORT;
delay(on_count)
PORT_IOBUS->Group[0].OUTCLR.reg = SPEAKER_PORT;
delay(off_count)
}
}
}
void loop() {
int result_t6, result_t7, result_t8, result_t9, result_t10;
result_t6 = t6.measure();
if (result_t6 < t6min) t6min = result_t6;
result_t7 = t7.measure();
if (result_t7 < t7min) t7min = result_t7;
result_t8 = t8.measure();
if (result_t8 < t8min) t8min = result_t8;
result_t9 = t9.measure();
if (result_t9 < t9min) t9min = result_t9;
result_t10 = t10.measure();
if (result_t10 < t10min) t10min = result_t10;
if(TOUCH_THRESHOLD <= result_t6 - t6min)
play_note(261, NOTE_TIME);
if(TOUCH_THRESHOLD <= result_t7 - t6min)
play_note(293, NOTE_TIME);
if(TOUCH_THRESHOLD <= result_t8 - t6min)
play_note(329, NOTE_TIME);
if(TOUCH_THRESHOLD <= result_t9 - t6min)
play_note(349, NOTE_TIME);
if(TOUCH_THRESHOLD <= result_t10 - t6min)
play_note(392, NOTE_TIME);
}