Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  1. This is the sketch that is causing issues when I use the WAV shield:
  2.  
  3. [code]
  4.  
  5. #include <Wire.h>
  6.  
  7. //WAV shield vars
  8. #include <FatReader.h>
  9. #include <SdReader.h>
  10. #include <avr/pgmspace.h>
  11. #include "WaveUtil.h"
  12. #include "WaveHC.h"
  13.  
  14. SdReader card; // This object holds the information for the card
  15. FatVolume vol; // This holds the information for the partition on the card
  16. FatReader root; // This holds the information for the filesystem on the card
  17. FatReader f; // This holds the information for the file we're play
  18.  
  19. WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
  20. //WAV shield vars
  21.  
  22. //motion sensor vars
  23. int timer = 500;
  24. int alarmPin = 0;
  25. int alarmValue = 0;
  26. //motion sensor vars
  27.  
  28. #include <ServoTimer2.h> // the servo library
  29.  
  30. //servo vars
  31. int activeServo= 0;
  32.  
  33. ServoTimer2 myservo1;
  34. ServoTimer2 myservo2;
  35. int servoPin1 = 6;//9;
  36. int servoPin2 = 7;//10;
  37. #define degreesToUS( _degrees) (_degrees * 6 + 900) // macro to convert degrees to microseconds
  38. int pos = 50; // variable to store the servo position
  39. //servo vars
  40.  
  41. boolean motionIsOn = false;
  42. boolean speakJetIsOn = false;
  43.  
  44. void setup()
  45. {
  46. Wire.begin(4); // join i2c bus with address #4
  47. Wire.onReceive(receiveEvent); // register event
  48. Serial.begin(9600); // start serial for output
  49. Serial.println("slave setup");
  50.  
  51.  
  52. initWAVShield();
  53. initMotionSensor();
  54. initServo();
  55. }
  56.  
  57. // this handy function will return the number of bytes currently free in RAM, great for debugging!
  58. int freeRam(void)
  59. {
  60. extern int __bss_end;
  61. extern int *__brkval;
  62. int free_memory;
  63. if((int)__brkval == 0) {
  64. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  65. }
  66. else {
  67. free_memory = ((int)&free_memory) - ((int)__brkval);
  68. }
  69. return free_memory;
  70. }
  71.  
  72. void sdErrorCheck(void)
  73. {
  74. if (!card.errorCode()) return;
  75. putstring("\n\rSD I/O error: ");
  76. Serial.print(card.errorCode(), HEX);
  77. putstring(", ");
  78. Serial.println(card.errorData(), HEX);
  79. while(1);
  80. }
  81.  
  82.  
  83. //TODO: Initialize WAV shield when Twitter searches yeild 0 results
  84. void initWAVShield()
  85. {
  86. Serial.println("initWAVShield");
  87. putstring("Free RAM: "); // This can help with debugging, running out of RAM is bad
  88. Serial.println(freeRam()); // if this is under 150 bytes it may spell trouble!
  89.  
  90. // Set the output pins for the DAC control. This pins are defined in the library
  91. // pinMode(6, OUTPUT);
  92. // pinMode(7, OUTPUT);
  93. // pinMode(8, OUTPUT);
  94. // pinMode(5, OUTPUT);
  95.  
  96. pinMode(2, OUTPUT);
  97. pinMode(3, OUTPUT);
  98. pinMode(4, OUTPUT);
  99. pinMode(5, OUTPUT);
  100.  
  101. // if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  102. if (!card.init()) { //play with 8 MHz spi (default faster!)
  103. putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
  104. sdErrorCheck();
  105. while(1); // then 'halt' - do nothing!
  106. }
  107.  
  108. // enable optimize read - some cards may timeout. Disable if you're having problems
  109. card.partialBlockRead(true);
  110.  
  111. // Now we will look for a FAT partition!
  112. uint8_t part;
  113. for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
  114. if (vol.init(card, part))
  115. break; // we found one, lets bail
  116. }
  117. if (part == 5) { // if we ended up not finding one :(
  118. putstring_nl("No valid FAT partition!");
  119. sdErrorCheck(); // Something went wrong, lets print out why
  120. while(1); // then 'halt' - do nothing!
  121. }
  122.  
  123. // Lets tell the user about what we found
  124. putstring("Using partition ");
  125. Serial.print(part, DEC);
  126. putstring(", type is FAT");
  127. Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
  128.  
  129. // Try to open the root directory
  130. if (!root.openRoot(vol)) {
  131. putstring_nl("Can't open root dir!"); // Something went wrong,
  132. while(1); // then 'halt' - do nothing!
  133. }
  134.  
  135. // Whew! We got past the tough parts.
  136. putstring_nl("Ready!");
  137. }
  138.  
  139. void initMotionSensor()
  140. {
  141. Serial.println("initMotionSensor");
  142. pinMode(alarmPin, INPUT);
  143. }
  144.  
  145. void initServo()
  146. {
  147. Serial.println("initServo");
  148.  
  149. pinMode(servoPin1, OUTPUT);
  150. pinMode(servoPin2, OUTPUT);
  151.  
  152. // set up servo pin
  153. myservo1.attach(servoPin1);
  154. myservo2.attach(servoPin2);
  155.  
  156. stepper1();
  157. delay(5000);
  158. stepper2();
  159. delay(5000);
  160. stepper1();
  161. stepper2();
  162. }
  163.  
  164. void loop()
  165. {
  166. if(motionIsOn==true){
  167. alarmValue = analogRead(alarmPin);
  168.  
  169. //Serial.println(alarmValue);
  170.  
  171. if(alarmValue < 100) {
  172. Serial.println("motion detected");
  173. playRandomSound();
  174. }
  175.  
  176. delay(timer);
  177. }
  178.  
  179. if(speakJetIsOn==true){
  180. animateMouth();
  181. }
  182. }
  183.  
  184. void playRandomSound(){
  185.  
  186. int i;
  187. i = random(3);
  188.  
  189. Serial.println (i);
  190. switch (i) {
  191. case 1:
  192. playcomplete("test0.wav");
  193. break;
  194. case 2:
  195. playcomplete("test1.wav");
  196. break;
  197. case 3:
  198. playcomplete("test2.wav");
  199. break;
  200. }
  201. }
  202.  
  203. // Plays a full file from beginning to end with no pause.
  204. void playcomplete(char *name) {
  205. // call our helper to find and play this name
  206. playfile(name);
  207. while (wave.isplaying) {
  208. // do nothing while its playing
  209. //animateMouth();
  210. animateMouthWAV();
  211. }
  212. // now its done playing
  213.  
  214. }
  215.  
  216. void animateMouthWAV(){
  217. Serial.println("animateMouthWAV");
  218.  
  219. char i;
  220. uint8_t volume;
  221. int v2;
  222.  
  223. volume = 0;
  224. for (i=0; i<8; i++) {
  225. v2 = analogRead(1) - 512;
  226. if (v2 < 0)
  227. v2 *= -1;
  228. if (v2 > volume)
  229. volume = v2;
  230. delay(5);
  231. }
  232.  
  233. if (volume > 200) {
  234. for(pos = 1400; pos < 1700; pos += 15) // goes from 0 degrees to 180 degrees
  235. { // in steps of 1 degree
  236. myservo1.write(pos); // tell servo to go to position in variable 'pos'
  237. delay(15); // waits 15ms for the servo to reach the position
  238. }
  239. } else {
  240. for(pos = 1700; pos>=1400; pos -=15) // goes from 180 degrees to 0 degrees
  241. {
  242. myservo1.write(pos); // tell servo to go to position in variable 'pos'
  243. delay(15); // waits 15ms for the servo to reach the position
  244. }
  245. }
  246.  
  247. // Serial.println("volume");
  248. // Serial.println(volume);
  249. }
  250.  
  251. void animateMouth(){
  252. if(activeServo==0){
  253. stepper1();
  254. } else if(activeServo==1){
  255. stepper2();
  256. }
  257. }
  258.  
  259. int stepper1(){
  260. Serial.println("stepper1");
  261.  
  262. for(pos = 1400; pos < 1700; pos += 15) // goes from 0 degrees to 180 degrees
  263. { // in steps of 1 degree
  264. myservo1.write(pos); // tell servo to go to position in variable 'pos'
  265. delay(15); // waits 15ms for the servo to reach the position
  266. }
  267. for(pos = 1700; pos>=1400; pos -=15) // goes from 180 degrees to 0 degrees
  268. {
  269. myservo1.write(pos); // tell servo to go to position in variable 'pos'
  270. delay(15); // waits 15ms for the servo to reach the position
  271. }
  272. }
  273.  
  274. int stepper2(){
  275. Serial.println("stepper2");
  276.  
  277. for(pos = 1400; pos < 1700; pos += 15) // goes from 0 degrees to 180 degrees
  278. { // in steps of 1 degree
  279. myservo2.write(pos); // tell servo to go to position in variable 'pos'
  280. delay(15); // waits 15ms for the servo to reach the position
  281. }
  282. for(pos = 1700; pos>=1400; pos -=15) // goes from 180 degrees to 0 degrees
  283. {
  284. myservo2.write(pos); // tell servo to go to position in variable 'pos'
  285. delay(15); // waits 15ms for the servo to reach the position
  286. }
  287. }
  288.  
  289.  
  290. void playfile(char *name) {
  291.  
  292. Serial.println(name);
  293. // see if the wave object is currently doing something
  294. if (wave.isplaying) {// already playing something, so stop it!
  295. wave.stop(); // stop it
  296. }
  297. // look in the root directory and open the file
  298. if (!f.open(root, name)) {
  299. putstring("Couldn't open file "); Serial.print(name); return;
  300. }
  301. // OK read the file and turn it into a wave object
  302. if (!wave.create(f)) {
  303. putstring_nl("Not a valid WAV"); return;
  304. }
  305.  
  306. // ok time to play! start playback
  307. wave.play();
  308. }
  309.  
  310. // function that executes whenever data is received from master
  311. // this function is registered as an event, see setup()
  312. void receiveEvent(int howMany)
  313. {
  314. while(1 < Wire.available()) // loop through all but the last
  315. {
  316. char c = Wire.read(); // receive byte as a character
  317. //Serial.print(c); // print the character
  318. }
  319. int x = Wire.read(); // receive byte as an integer
  320. //Serial.println(x); // print the integer
  321.  
  322. if(x==0){
  323. motionIsOn=false;
  324. speakJetIsOn=false;
  325. } else if (x==1) {
  326. motionIsOn=true;
  327. } else if (x==2 && speakJetIsOn == false) {
  328. activeServo = random(2);
  329. Serial.println("activeServo");
  330. Serial.println(activeServo);
  331. speakJetIsOn=true;
  332. }
  333.  
  334. //Serial.println(x);
  335.  
  336. }[/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement