For this project, our goal was to craft a musical instrument within the constraints of limited resources available. Despite the restricted options, we brainstormed various concepts and ultimately settled on developing a hybrid piano/synthesizer. To accomplish this, we repurposed buttons as keys by integrating two types of sensors, analog and digital. Using a breadboard, we configured the keys and delved into the coding phase. Each button was programmed to generate distinct notes, connected to a borrowed speaker from the IM lab for clearer and better sound quality.
As we progressed with the initial setup, the need for an analog sensor arose. We incorporated a potentiometer, not to alter the current, but as a knob to modulate the notes produced by the four buttons. The potentiometer’s range was segmented into three sections, and within the programming loop, an ‘if-else’ construct acted akin to cases, adjusting the notes played in response to the potentiometer’s adjustments.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Code:
#include "pitches.h"
const int speaker = 12;
const int tone_G = 2;
const int tone_A = 3;
const int tone_B = 4;
const int tone_C = 5;
const int tone_D = 9;
const int tone_E = 10;
const int tone_F = 11;
int buttonState_G = 0;
int buttonState_A = 0;
int buttonState_B = 0;
int buttonState_C = 0;
int buttonState_D = 0;
int buttonState_E = 0;
int buttonState_F = 0;
int potPin = A0;
int potVal = 0;
voidsetup(){
// iterate over the notes of the melody:
pinMode(tone_G, INPUT);
pinMode(tone_A, INPUT);
pinMode(tone_B, INPUT);
pinMode(tone_C, INPUT);
pinMode(tone_D, INPUT);
pinMode(tone_E, INPUT);
pinMode(tone_F, INPUT);
}
voidloop(){
// no need to repeat the melody.
potVal = analogRead(potPin);
buttonState_G = digitalRead(tone_G);
buttonState_A = digitalRead(tone_A);
buttonState_B = digitalRead(tone_B);
buttonState_C = digitalRead(tone_C);
buttonState_D = digitalRead(tone_D);
buttonState_E = digitalRead(tone_E);
buttonState_F = digitalRead(tone_F);
if(potVal <341)// Lowest third of the potentiometer's range (0-340)
{
if(buttonState_G == HIGH){
// play the sound
tone(speaker,NOTE_G4);
}
elseif(buttonState_A == HIGH){
// play the sound
tone(speaker,NOTE_AS4);
}
elseif(buttonState_B == HIGH){
// play the sound
tone(speaker,NOTE_F4);
}
elseif(buttonState_C == HIGH){
// play the sound
tone(speaker,NOTE_C3);
}
else
{
noTone(speaker);
}
}
elseif(potVal <682)// Lowest third of the potentiometer's range (0-340)
{
if(buttonState_G == HIGH){
// play the sound
tone(speaker,NOTE_A4); //random note not matching the name
}
elseif(buttonState_A == HIGH){
// play the sound
tone(speaker,NOTE_B4); //random note not matching the name
}
elseif(buttonState_B == HIGH){
// play the sound
tone(speaker,NOTE_C4); //random note not matching the name
}
elseif(buttonState_C == HIGH){
// play the sound
tone(speaker,NOTE_D4); //random note not matching the name
}
else
{
noTone(speaker);
}
}
else
{
if(buttonState_G == HIGH){
// play the sound
tone(speaker,NOTE_AS4);
}
elseif(buttonState_A == HIGH){
// play the sound
tone(speaker,NOTE_FS4);
}
elseif(buttonState_B == HIGH){
// play the sound
tone(speaker,NOTE_GS4);
}
elseif(buttonState_C == HIGH){
// play the sound
tone(speaker,NOTE_CS3);
}
else
{
noTone(speaker);
}
}
}
Code:
#include "pitches.h"
const int speaker = 12;
const int tone_G = 2;
const int tone_A = 3;
const int tone_B = 4;
const int tone_C = 5;
const int tone_D = 9;
const int tone_E = 10;
const int tone_F = 11;
int buttonState_G = 0;
int buttonState_A = 0;
int buttonState_B = 0;
int buttonState_C = 0;
int buttonState_D = 0;
int buttonState_E = 0;
int buttonState_F = 0;
int potPin = A0;
int potVal = 0;
void setup() {
// iterate over the notes of the melody:
pinMode(tone_G, INPUT);
pinMode(tone_A, INPUT);
pinMode(tone_B, INPUT);
pinMode(tone_C, INPUT);
pinMode(tone_D, INPUT);
pinMode(tone_E, INPUT);
pinMode(tone_F, INPUT);
}
void loop() {
// no need to repeat the melody.
potVal = analogRead(potPin);
buttonState_G = digitalRead(tone_G);
buttonState_A = digitalRead(tone_A);
buttonState_B = digitalRead(tone_B);
buttonState_C = digitalRead(tone_C);
buttonState_D = digitalRead(tone_D);
buttonState_E = digitalRead(tone_E);
buttonState_F = digitalRead(tone_F);
if (potVal < 341) // Lowest third of the potentiometer's range (0-340)
{
if (buttonState_G == HIGH) {
// play the sound
tone(speaker,NOTE_G4);
}
else if (buttonState_A == HIGH) {
// play the sound
tone(speaker,NOTE_AS4);
}
else if (buttonState_B == HIGH) {
// play the sound
tone(speaker,NOTE_F4);
}
else if (buttonState_C == HIGH) {
// play the sound
tone(speaker,NOTE_C3);
}
else
{
noTone(speaker);
}
}
else if (potVal < 682) // Lowest third of the potentiometer's range (0-340)
{
if (buttonState_G == HIGH) {
// play the sound
tone(speaker,NOTE_A4); //random note not matching the name
}
else if (buttonState_A == HIGH) {
// play the sound
tone(speaker,NOTE_B4); //random note not matching the name
}
else if (buttonState_B == HIGH) {
// play the sound
tone(speaker,NOTE_C4); //random note not matching the name
}
else if (buttonState_C == HIGH) {
// play the sound
tone(speaker,NOTE_D4); //random note not matching the name
}
else
{
noTone(speaker);
}
}
else
{
if (buttonState_G == HIGH) {
// play the sound
tone(speaker,NOTE_AS4);
}
else if (buttonState_A == HIGH) {
// play the sound
tone(speaker,NOTE_FS4);
}
else if (buttonState_B == HIGH) {
// play the sound
tone(speaker,NOTE_GS4);
}
else if (buttonState_C == HIGH) {
// play the sound
tone(speaker,NOTE_CS3);
}
else
{
noTone(speaker);
}
}
}
Code:
#include "pitches.h"
const int speaker = 12;
const int tone_G = 2;
const int tone_A = 3;
const int tone_B = 4;
const int tone_C = 5;
const int tone_D = 9;
const int tone_E = 10;
const int tone_F = 11;
int buttonState_G = 0;
int buttonState_A = 0;
int buttonState_B = 0;
int buttonState_C = 0;
int buttonState_D = 0;
int buttonState_E = 0;
int buttonState_F = 0;
int potPin = A0;
int potVal = 0;
void setup() {
// iterate over the notes of the melody:
pinMode(tone_G, INPUT);
pinMode(tone_A, INPUT);
pinMode(tone_B, INPUT);
pinMode(tone_C, INPUT);
pinMode(tone_D, INPUT);
pinMode(tone_E, INPUT);
pinMode(tone_F, INPUT);
}
void loop() {
// no need to repeat the melody.
potVal = analogRead(potPin);
buttonState_G = digitalRead(tone_G);
buttonState_A = digitalRead(tone_A);
buttonState_B = digitalRead(tone_B);
buttonState_C = digitalRead(tone_C);
buttonState_D = digitalRead(tone_D);
buttonState_E = digitalRead(tone_E);
buttonState_F = digitalRead(tone_F);
if (potVal < 341) // Lowest third of the potentiometer's range (0-340)
{
if (buttonState_G == HIGH) {
// play the sound
tone(speaker,NOTE_G4);
}
else if (buttonState_A == HIGH) {
// play the sound
tone(speaker,NOTE_AS4);
}
else if (buttonState_B == HIGH) {
// play the sound
tone(speaker,NOTE_F4);
}
else if (buttonState_C == HIGH) {
// play the sound
tone(speaker,NOTE_C3);
}
else
{
noTone(speaker);
}
}
else if (potVal < 682) // Lowest third of the potentiometer's range (0-340)
{
if (buttonState_G == HIGH) {
// play the sound
tone(speaker,NOTE_A4); //random note not matching the name
}
else if (buttonState_A == HIGH) {
// play the sound
tone(speaker,NOTE_B4); //random note not matching the name
}
else if (buttonState_B == HIGH) {
// play the sound
tone(speaker,NOTE_C4); //random note not matching the name
}
else if (buttonState_C == HIGH) {
// play the sound
tone(speaker,NOTE_D4); //random note not matching the name
}
else
{
noTone(speaker);
}
}
else
{
if (buttonState_G == HIGH) {
// play the sound
tone(speaker,NOTE_AS4);
}
else if (buttonState_A == HIGH) {
// play the sound
tone(speaker,NOTE_FS4);
}
else if (buttonState_B == HIGH) {
// play the sound
tone(speaker,NOTE_GS4);
}
else if (buttonState_C == HIGH) {
// play the sound
tone(speaker,NOTE_CS3);
}
else
{
noTone(speaker);
}
}
}