Building an Arduino Pest Repeller on Linux

 

Building an Arduino Pest Repeller on Linux

fig-1 rinkydinkrig

// CritterScare.ino. Plays scary sound effects
// when a deer or other freeloader comes within 
// 24 inches of the sonar rangefinder
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card; 
FatVolume vol;
FatReader root;
FatReader f;
WaveHC wave;
// declare functions; function bodies are 
// below loop() function
int freeRam(void);
void sdErrorCheck(void);
void playfile(char *name);
void playfile(char *name);
// setup() is a required Arduino function
void setup() {
  Serial.begin(9600);  
  Serial.print("\nThe serial port is set to 9600 baud. \nWelcome to the CritterScare Sketch!");
  Serial.print("\nYou have ");
  Serial.println(freeRam());
  Serial.print("bytes of free RAM.");
  
// Set the output pins for the DAC control
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  if (!card.init()) {      
    Serial.print("\nCard init. failed!");
    sdErrorCheck();
    while(1); 
  }
  
// enable optimize read - some cards may timeout. 
// Disable if you're having problems
  card.partialBlockRead(true);
 
// Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {
    if (vol.init(card, part)) 
      break;    
  }
  if (part == 5) {    
    putstring_nl("Sorry, there is no valid FAT partition!");
    sdErrorCheck();  
    while(1);   
  }
  
// Print partition and filesystem information
  Serial.print("\nI'm using partition ");
  Serial.print(part, DEC);
  Serial.print(" on the SD card, and the filesystem type is FAT");
  Serial.println(vol.fatType(),DEC);
  
// Try to open the root directory
  if (!root.openRoot(vol)) {
    Serial.print("\nCan't open root dir!");
    while(1); 
  }
  
// Whew! We got past the tough parts.
  Serial.print("\nEverything checks out, so let's get going!\n");
}
// loop() is a required Arduino function
// convert voltage from sensor to inches
void loop() {
    float a0value = analogRead(0); 
    float inches = 0.496 * a0value; 
    Serial.print("\nThe value captured from pin a0 is: "); 
    Serial.println(a0value);
    Serial.print("\nThe distance in inches is "); 
    Serial.println(inches);
    delay(3000);
    
    int playback = 0;
    if (inches < 24 && playback == 0){
    playback = 1;
    playcomplete("COYOTEHOWL.WAV");
  }
  else if (inches < 24 && playback == !0){
  }
  else { playback = 0;
    wave.stop();
  }
}
// And now the other function bodies
// this handy function will return the number
// of bytes currently free in RAM, great for debugging!   
int freeRam(void)
{
  extern int  __bss_end; 
  extern int  *__brkval; 
  int free_memory; 
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end); 
  }
  else {
    free_memory = ((int)&free_memory) - ((int)__brkval); 
  }
  return free_memory; 
} 
void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  Serial.print("\n\rSD I/O error: ");
  Serial.println(card.errorCode(), HEX);
  Serial.print(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}
// Plays a WAV file from beginning to end with no pause.
void playcomplete(char *name) {
// call our helper to find and play this name
  playfile(name);
  while (wave.isplaying) {
    //do nothing else
     }
}
  
void playfile(char *name) {
// see if the wave object is currently doing something
  if (wave.isplaying) {
    wave.stop();
  }
// look in the root directory and open the file
  if (!f.open(root, name)) {
    Serial.print("\nCouldn't open file "); 
    Serial.println(name); 
    return;
  }
// OK read the file and turn it into a wave object
  if (!wave.create(f)) {
    Serial.print("\nNot a valid WAV"); return;
  }  
// ok time to play! start playback
  wave.play();
}