Crash course: Embedded programming with Arduino

Try your hand at embedded programming with this project

Page 4 of 4

Singing snowman sketch

 
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
 
AF_Wave card;
File f;
Wavefile wave;      
 
// save RAM by using program memory strings
#define playcomplete(x) ROM_playcomplete(PSTR(x))         
 
#define servo 7
#define redled 9
#define eyeleds 18
#define mouthleds 17
#define midmouthleds 16
#define outermouthleds 19
 
// set up Serial library at 9600 bps
void setup() {
  Serial.begin(9600);           
  Serial.println("Wave test!");
 
/* Setup the LEDs */
 
  pinMode(2, OUTPUT); 
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(redled, OUTPUT);
  pinMode(servo, OUTPUT);
  pinMode(eyeleds, OUTPUT);
  pinMode(outermouthleds, OUTPUT);
  pinMode(midmouthleds, OUTPUT);
  pinMode(mouthleds, OUTPUT);
  
  randomSeed(analogRead(0));
 
/* Useful feedback if the SD card is missing 
 *  or cannot be read */
 
  if (!card.init_card()) {
    putstring_nl("Card init. failed!"); return;
  }
  if (!card.open_partition()) {
    putstring_nl("No partition!"); return;
  }
  if (!card.open_filesys()) {
    putstring_nl("Couldn't open filesys"); return;
  }
 
 if (!card.open_rootdir()) {
    putstring_nl("Couldn't open dir"); return;
  }
 
  putstring_nl("Files found:");
  ls();
}
 
void ls() {
  char name[13];
  int ret;
  
  card.reset_dir();
  putstring_nl("Files found:");
  while (1) {
    ret = card.get_next_name_in_dir(name);
    if (!ret) {
       card.reset_dir();
       return;
    }
    Serial.println(name);
  }
}
 
 
void pulseServo(uint8_t servopin, uint16_t p) {
  
 digitalWrite(servopin, HIGH);
 delayMicroseconds(600);
 while (p--) {
   delayMicroseconds(4);
 }
 digitalWrite(servopin, LOW);
  delay(18);
}
 
/* typedef unsigned char "snowmanstate"
 * You can change this name to something 
 * suitable for  your own project 
 * Measures how close people are and plays
 * audio files according to distance    */
 
uint8_t snowmanstate = 0;
 
void loop() { 
   int distsensor, i;
   long time;
   /*
   for (i=0; i<50; i++) {
     pulseServo(servo,0);
   }
   for (i=0; i<50; i++) {
     pulseServo(servo,400);
   }
   return;
   */
   distsensor = 0;
   for (i=0; i<8; i++) {
     distsensor += analogRead(0);
     delay(50);
   }
   distsensor /= 8;
 
// putstring("Sensor = "); 
   Serial.println(distsensor);
 
/* A reading of 500 = more than 6 meters away, so once 
 * every 200 times play one of of three scary sounds 
 * when no one is present */
   
   if (distsensor<= 500) {
     digitalWrite(eyeleds, HIGH); 
   } 
   if (distsensor > 500) {
     digitalWrite(eyeleds, LOW);  
 
 // nobody there. one out of 200 times play one of 
 // the scary sounds (once every few minutes)
 
/* Substitute your own audio file names */ 
 
     snowmanstate = 1;
     i = random(200);
     //Serial.println(i);
     if (i == 0) {
       i = random(3);
       if (i == 0) {
           playcomplete("merryxmas.wav");
       } else if (i == 1) {
           playcomplete("hollyjolly.wav");
       } else {
           playcomplete("jinglebellsbatmansmells.wav");   
       }
     }
 
/* When someone comes closer play these files 
 * Substitute your own audio file names */
 
   } else if ((distsensor > 300) && (distsensor < 400)) {
     if (snowmanstate <= 1) {    
        playcomplete("rudolph.wav"); 
     } else {
       i = random(60);  
       //Serial.println(i);
       if (i == 0) {
         i = random(3);
         if (i == 0) {
           playcomplete("frosty.wav");
         } else if (i == 1) {
           playcomplete("kissingsanta.wav");
         } else {
           playcomplete("deckhalls.wav");   
         }
       } 
     }
     snowmanstate = 2; 
   } else if ((distsensor > 100) && (distsensor < 200)) {
     if (snowmanstate <= 2) { 
       playcomplete("frosty.wav"); 
     } else {
       i = random(50); // more often
       //Serial.println(i);
       if (i == 0) {
         i = random(3);
         if (i == 0) {
           playcomplete("gotell.wav");
         } else if (i == 1) {
           playcomplete("joyworld.wav");
         } else {
           playcomplete("letitsnow.wav");   
         }
       }
     }
     snowmanstate = 3;
   } else if (distsensor < 50) {
     if (snowmanstate <= 3) { 
         playcomplete("linussong.wav");    
     } else {
       i = random(30);  // more often
     //Serial.println(i);
     if (i == 0) {
       i = random(2);
       if (i == 0) {
           playcomplete("mrgrinch.wav");
       } else if (i == 1) {
           playcomplete("sleighride.wav");
       } 
     }
       
   }
    snowmanstate = 4;
 }
}
 
/* This part lights the LEDs according to
 * the strength of the audio signal */
 
void ROM_playcomplete(const char *romname) {
  char name[13], i;
  uint8_t volume;
  int v2;
  
  for (i=0; i <13; i++) {
    name[i] = pgm_read_byte(&romname[i]);
  }
  name[12] = 0;
  Serial.println(name);
  playfile(name);
  while (wave.isplaying) {
   volume = 0;
   for (i=0; i<8; i++) {
     v2 = analogRead(1) - 512;
     if (v2< 0) 
        v2 *= -1;
     if (v2 > volume)
       volume = v2;
     delay(5);
   }
   if (volume > 200) {
     digitalWrite(outermouthleds, HIGH);
   } else {
     digitalWrite(outermouthleds, LOW);
   }
   if (volume > 150) {
     digitalWrite(midmouthleds, HIGH);
   } else {
     digitalWrite(midmouthleds, LOW);
   } 
   if (volume > 100) {
     digitalWrite(mouthleds, HIGH);
   } else {
     digitalWrite(mouthleds, LOW);
   } 
   //putstring("vol = "); Serial.println(volume, DEC);
  }
  card.close_file(f);
}
 
void playfile(char *name) {
   f = card.open_file(name);
   if (!f) {
      putstring_nl(" Couldn't open file"); return;
   }
   if (!wave.create(f)) {
     putstring_nl(" Not a valid WAV"); return;
   }
   // ok time to play!
   wave.play();
}

| 1 2 3 4 Page 4
ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon