Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed SDFileSystem
Diff: main.cpp
- Revision:
- 2:81f364f7a4a6
- Parent:
- 1:b0962a8bcc6d
- Child:
- 3:07afde41e7e6
--- a/main.cpp Fri Apr 12 13:58:01 2019 +0000
+++ b/main.cpp Fri Apr 19 13:29:31 2019 +0000
@@ -1,22 +1,122 @@
// ESE350 Final Project: Drue
#include "mbed.h"
-#include <string>
-// Pin setup / variable declarations
-DigitalIn butC(p6);
-DigitalIn butD(p8);
-DigitalIn butE(p10);
-DigitalIn butF(p14);
-DigitalIn butG(p16);
-DigitalOut ledC(p5);
-DigitalOut ledD(p7);
-DigitalOut ledE(p9);
-DigitalOut ledF(p13);
-DigitalOut ledG(p15);
-DigitalIn butMode(p17);
-PwmOut speaker(p26);
+
+// Pin setup ////////////////////////////////////////////
+// Button Inputs
+DigitalIn butC(p14);
+DigitalIn butD(p15);
+DigitalIn butE(p16);
+DigitalIn butF(p17);
+DigitalIn butG(p19);
+DigitalIn butA(p20);
+DigitalIn butB(p30);
+DigitalIn butCh(p29);
+DigitalIn butMode(p22);
+
+// LED Outputs Temporarily
+DigitalOut ledC(p28);
+DigitalOut ledD(p27);
+DigitalOut ledE(p26);
+DigitalOut ledF(p25);
+DigitalOut ledG(p24);
+DigitalOut ledH(p25);
+
+PwmOut speaker(p21);
+
+/////////////////////////////////////////////////////////
+
+// Global Variables
bool switchPressed;
+bool notePressed;
+int prevNote;
int mode;
int numModes = 2;
+
+////////////////////////////////////////////////////////
+
+void buttonSetup() {
+ butC.mode(PullUp);
+ butD.mode(PullUp);
+ butE.mode(PullUp);
+ butF.mode(PullUp);
+ butG.mode(PullUp);
+ butA.mode(PullUp);
+ butB.mode(PullUp);
+ butCh.mode(PullUp);
+ butMode.mode(PullUp);
+}
+
+void pressKey() {
+ if (butC == 0 || butD == 0 || butE == 0 || butF == 0 || butG == 0
+ || butA == 0 || butB == 0 || butCh == 0) {
+ // this if statement makes transitions between notes smoother
+ // given that a note was already pressed before
+ if (prevNote != -1) {
+ switch(prevNote) {
+ case 0:
+ if (butC == 1) {notePressed = false;}
+ break;
+ case 1:
+ if (butD == 1) {notePressed = false;}
+ break;
+ case 2:
+ if (butE == 1) {notePressed = false;}
+ break;
+ case 3:
+ if (butF == 1) {notePressed = false;}
+ break;
+ case 4:
+ if (butG == 1) {notePressed = false;}
+ break;
+ case 5:
+ if (butA == 1) {notePressed = false;}
+ break;
+ case 6:
+ if (butB == 1) {notePressed = false;}
+ break;
+ case 7:
+ if (butCh == 1) {notePressed = false;}
+ break;
+ }
+ }
+ // wait(0.003); needed this before to give period time to update
+ // this if statement initializes first note sound
+ if (!notePressed) {
+ if (butC == 0) {
+ speaker.period(1.0/523.25);
+ prevNote = 0;
+ } else if (butD == 0) {
+ speaker.period(1.0/587.33);
+ prevNote = 1;
+ } else if (butE == 0) {
+ speaker.period(1.0/659.25);
+ prevNote = 2;
+ } else if (butF == 0) {
+ speaker.period(1.0/698.46);
+ prevNote = 3;
+ } else if (butG == 0) {
+ speaker.period(1.0/783.99);
+ prevNote = 4;
+ } else if (butA == 0) {
+ speaker.period(1.0/880.0);
+ prevNote = 5;
+ } else if (butB == 0) {
+ speaker.period(1.0/987.77);
+ prevNote = 6;
+ } else if (butCh == 0) {
+ speaker.period(1.0/1040.50);
+ prevNote = 7;
+ }
+ }
+ notePressed = true;
+ speaker = 0.5;
+ } else {
+ notePressed = false;
+ speaker = 0;
+ prevNote = -1;
+ }
+}
+
void switchModeCheck() {
if (butMode == 1) {
if (!switchPressed) {
@@ -30,24 +130,25 @@
switchPressed = false;
}
}
+
void cycleSound() {
static int count = 0;
if (count == 0){
- speaker.period(1.0/523.25);
+ speaker.period(1.0/523.25);
} else if (count == 1){
- speaker.period(1.0/587.33);
- }else if (count == 2){
- speaker.period(1.0/659.25);
- }else if (count == 3){
- speaker.period(1.0/698.46);
- }else if (count == 4){
- speaker.period(1.0/783.99);
- }else if (count == 5){
- speaker.period(1.0/880.0);
- }else if (count == 6){
- speaker.period(1.0/987.77);
- }else if (count == 7){
- speaker.period(1.0/1040.50);
+ speaker.period(1.0/587.33);
+ } else if (count == 2){
+ speaker.period(1.0/659.25);
+ } else if (count == 3){
+ speaker.period(1.0/698.46);
+ } else if (count == 4){
+ speaker.period(1.0/783.99);
+ } else if (count == 5){
+ speaker.period(1.0/880.0);
+ } else if (count == 6){
+ speaker.period(1.0/987.77);
+ } else if (count == 7){
+ speaker.period(1.0/1040.50);
}
speaker = 0.5;
wait(0.2);
@@ -55,89 +156,17 @@
count++;
if (count == 8) {count = 0;}
}
+
int main() {
+ buttonSetup();
+
switchPressed = false;
+ notePressed = false;
mode = 0;
bool needNote = true;
- ledC = 1; ledD = 1; ledE = 1; ledF = 1; ledG = 1;
+ prevNote = -1;
+
while(1) {
- switchModeCheck();
-
- if (mode == 0) {
- if (butC == 1) {
- ledC = 0;
- cycleSound();
- } else {
- ledC = 1;
- }
- if (butD == 1) {
- ledD = 0;
- cycleSound();
- } else {
- ledD = 1;
- }
- if (butE == 1) {
- ledE = 0;
- cycleSound();
- } else {
- ledE = 1;
- }
- if (butF == 1) {
- ledF = 0;
- cycleSound();
- } else {
- ledF = 1;
- }
- if (butG == 1) {
- ledG = 0;
- cycleSound();
- } else {
- ledG = 1;
- }
- }
-
- if (mode == 1) {
- int random = rand() % 5;
- if (needNote) {
- needNote = false;
- if (random == 0) {
- ledC = 0;
- } else if (random == 1) {
- ledD = 0;
- } else if (random == 2) {
- ledE = 0;
- } else if (random == 3) {
- ledF = 0;
- } else if (random == 4) {
- ledG = 0;
- }
- }
- if (butC == 1 && ledC == 0) {
- cycleSound();
- ledC = 1;
- needNote = true;
- } else if (butD == 1 && ledD == 0) {
- ledD = 1;
- cycleSound();
- needNote = true;
- } else if (butE == 1 && ledE == 0) {
- ledE = 1;
- cycleSound();
- needNote = true;
- } else if (butF == 1 && ledF == 0) {
- ledF = 1;
- cycleSound();
- needNote = true;
- } else if (butG == 1 && ledG == 0) {
- ledG = 1;
- cycleSound();
- needNote = true;
- }
-
-
-
-
-
- }
+ pressKey();
}
}
\ No newline at end of file
