#include ; #include "pitches.h" //define our variables int ledPin = 11; int x = 0; int oldbreathe; int slot; String incomingWord, incomingByte, breathe, postscore; //defines the melody played and the duration of each note ///////////////////////////////////////////////////////// int melody[] = { NOTE_G4, NOTE_B4, NOTE_D6, NOTE_G5, NOTE_GS4, NOTE_C5, NOTE_DS7, NOTE_GS5, NOTE_AS5, NOTE_D5, NOTE_F6, NOTE_AS5}; int noteDurations[] = { 32, 32, 32, 32,32,32,32,32,32,32,32,16 }; ///////////////////////////////////////////////////////// void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); breathe = 49; slot = 1; } void loop() { if (breathe == 49){ // if (breathe = 1) then start breathe math function int outVal = yforx(x); analogWrite(ledPin, outVal); delay(6); // modify the pace of breathing x++; } if (breathe == 48){ analogWrite(ledPin, 0); // override the LED to OFF if (breathe = 0) } ///////////////////////////// ///////////////////////////// if (Serial.available() > 0) { //If we get a character over the serial line (serial activity) incomingByte = Serial.read(); if (incomingByte == 44){ //44 we received a delimiting comma. dump incomingWord to open array slot if (slot > 2){ slot=1; } if (slot == 1){ breathe = incomingWord; // Serial.print("new breathe value: "); // Serial.println(breathe); if (breathe == 49){playmusic();} slot++; } else if (slot == 2){ postscore = incomingWord; Serial.print("new postscore value: "); Serial.println(postscore); slot++; } incomingWord = ""; } else if (incomingByte != 13){ //received normal character. add to word incomingWord = incomingWord + incomingByte; } else if (incomingByte == 13){ //13 is carriage return slot = 1; incomingWord = ""; } } } void playmusic() { for (int thisNote = 0; thisNote < 12; thisNote++) { int noteDuration = 1000/noteDurations[thisNote]; tone(8, melody[thisNote],noteDuration); int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); noTone(8); } } int yforx(int x) { return (-240*abs(sin(x*0.01)))+255; //sine wave }