Advertisement
Guest User

Arduino source

a guest
Oct 19th, 2012
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. #include <String.h>;
  2. #include "pitches.h"
  3.  
  4.  
  5. //define our variables
  6. int ledPin = 11;
  7. int x = 0;
  8. int oldbreathe;
  9. int slot;
  10. String incomingWord, incomingByte, breathe, postscore;
  11.  
  12.  
  13. //defines the melody played and the duration of each note
  14. /////////////////////////////////////////////////////////
  15. int melody[] = {
  16. NOTE_G4, NOTE_B4, NOTE_D6, NOTE_G5, NOTE_GS4, NOTE_C5, NOTE_DS7, NOTE_GS5, NOTE_AS5, NOTE_D5, NOTE_F6, NOTE_AS5};
  17. int noteDurations[] = {
  18. 32, 32, 32, 32,32,32,32,32,32,32,32,16 };
  19. /////////////////////////////////////////////////////////
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. void setup() {
  27. pinMode(ledPin, OUTPUT);
  28. Serial.begin(9600);
  29. breathe = 49;
  30. slot = 1;
  31. }
  32.  
  33.  
  34. void loop() {
  35.  
  36. if (breathe == 49){ // if (breathe = 1) then start breathe math function
  37. int outVal = yforx(x);
  38. analogWrite(ledPin, outVal);
  39. delay(6); // modify the pace of breathing
  40. x++;
  41. }
  42. if (breathe == 48){
  43. analogWrite(ledPin, 0); // override the LED to OFF if (breathe = 0)
  44. }
  45.  
  46. /////////////////////////////
  47. /////////////////////////////
  48.  
  49. if (Serial.available() > 0) { //If we get a character over the serial line (serial activity)
  50. incomingByte = Serial.read();
  51.  
  52. if (incomingByte == 44){ //44 we received a delimiting comma. dump incomingWord to open array slot
  53. if (slot > 2){
  54. slot=1;
  55. }
  56.  
  57.  
  58. if (slot == 1){
  59. breathe = incomingWord;
  60. // Serial.print("new breathe value: ");
  61. // Serial.println(breathe);
  62. if (breathe == 49){playmusic();}
  63. slot++;
  64.  
  65.  
  66. }
  67. else
  68. if (slot == 2){
  69. postscore = incomingWord;
  70. Serial.print("new postscore value: ");
  71. Serial.println(postscore);
  72. slot++;
  73. }
  74.  
  75.  
  76. incomingWord = "";
  77. }
  78.  
  79.  
  80. else if (incomingByte != 13){ //received normal character. add to word
  81. incomingWord = incomingWord + incomingByte;
  82. }
  83.  
  84. else
  85. if (incomingByte == 13){ //13 is carriage return
  86. slot = 1;
  87. incomingWord = "";
  88.  
  89. }
  90. }
  91. }
  92.  
  93.  
  94. void playmusic() {
  95. for (int thisNote = 0; thisNote < 12; thisNote++) {
  96. int noteDuration = 1000/noteDurations[thisNote];
  97. tone(8, melody[thisNote],noteDuration);
  98. int pauseBetweenNotes = noteDuration * 1.30;
  99. delay(pauseBetweenNotes);
  100. noTone(8);
  101. }
  102. }
  103.  
  104.  
  105.  
  106. int yforx(int x) {
  107. return (-240*abs(sin(x*0.01)))+255; //sine wave
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement