Some new stuff
  1. Arduino Park Control
  2. Arduino USB2serial programming the bare bone
  3. Arduino Projects Videos added
  4. Arduino Xbee projects
  5. Arduino dB Meter 
  6. Trying to get the IOIO going. 
  7. IOIO is going....bluetooth
  8. Attiny 2313 bluetooth bee RC car
  9. Compass and temperature added
  10. AVR programmer
  11. USB2Serial and Ethernet POE
  12. Arduino LPG Sensor
  13. Program the Arduino Mini 05
  14. Lipo Rider Pro in action
  15. EZ Robot Builder kit arrived 10 Nov 2012
  16. Arduino VA Meter!
  17. 05 Nov 2012 Project Sentry Gun.
  18. Attiny85 PIR Sensor 10 Dec 2012 
  19. 0-30VDC3A and 2-28VDC10A bench PSU's
  20. 19 Dec 2012 Arduino Voltmeter LCD
  21. 29 Dec 2012 Arduino Tachometer&Speedometer LCD
  22. 03 Jan 2013 Arduino AC Phase Control. 
  23. 18 Jan 2013 Arduino Distance Meter
  24. 22 Jan 2013 Arduino Digital Clock and Date
  25. 31 Jan 2013 Arduino Digital Clock and Date 8x2 LCD
  26. Feb 2013 LCD and Backpack
  27. Feb 2013 7 Segment Serial Clock Sparkfun. Distance Sensor with "newping" library
  28. Feb 2013 Lelo Remote
  29. 01/03/2013 Arduino Adafruit 7 Seg Digital Clock with RTC and backpack 
  30. Arduino Frequency Meter 26/03/2013
  31. Another RC  Car controlled by Arduino 06/04/2013
  32. My quadcopter project update 25 April 2013
  33. My Aqua Quad Copter Flying 26 April 2013enlightened
  34. Another monster RC truck;strong one! 11July2013cool
  35. X-Frame Quad Copter 31 July 2013cool
  36. Arduino GSM Sheild added.28/8/2013
  37. 1.8inch TFT screen 13/09/2013cool
  38. 04/11/2014 added PID Soldering Iron Control
  39. Added bluetooth servo control 04/11/2014
  40. ESP8266 first try.16/03/2015
  41. Arduino Uno Quadcopter 19 May 2015
  42. IOT WemosD1 Amp Meter

 

 


Search    

Arduino speaking distance meter. 12/06/2014

Ultrasonic ping with HC-04 sensor.

16x2 LCD with backpack

Code was modified with assistance of  Wynand from house4hack.co.za and by me regading the LCD backpack and wave file generation.

Code:


#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
#include "Wire.h"
#include "LiquidCrystal.h"
LiquidCrystal lcd(0);

#define trigger 7  // Arduino Pin an HC-SR04 Trig
#define echo 6     // Arduino Pin an HC-SR04 Echo

SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card
FatReader f;      // This holds the information for the file we're play

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

// 0-9, 10-19, 20-21
//----------------------------
/*char * wavFiles[] = {
 "10.WAV","15.WAV","20.WAV","25.WAV","30.WAV","35.WAV","40.WAV","45.WAV","50.WAV","55.WAV",
 "60.WAV","65.WAV","70.WAV","75.WAV","80.WAV","85.WAV","90.WAV","95.WAV","100.WAV","more1m.WAV",
 "less10cm.WAV","hello.WAV"};
 */
////////////////////////////////////////////////////////////////
// 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;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}

/////////////////////////////////////////////////////
void setup() 
{
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);

  // set up serial port
  Serial.begin(9600);
  lcd.begin(8,2);
  lcd.setCursor(0,0);
  lcd.print("Distance Meter");

  putstring_nl("WaveHC");
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());      // if this is under 150 bytes it may spell trouble!

  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  // pin13 LED
  pinMode(13, OUTPUT);

  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init())
  {         //play with 8 MHz spi (default faster!)  
    putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
    sdErrorCheck();
    while(1);                            // then 'halt' - do nothing!
  }

  // 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++) 
  {     // we have up to 5 slots to look in
    if (vol.init(card, part)) 
      break;                             // we found one, lets bail
  }
  if (part == 5)
  {                       // if we ended up not finding one  :(
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();      // Something went wrong, lets print out why
    while(1);                            // then 'halt' - do nothing!
  }

  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?

  // Try to open the root directory
  if (!root.openRoot(vol))
  {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }

  playcomplete("hi.WAV");            // Startmessage 
  delay(1000);
 
  // Whew! We got past the tough parts.
  putstring_nl("Ready!");
}

long duration=0;
int distance=0;

////////////////////////////////////////////////////////////
void loop()
{

  digitalWrite(trigger, LOW);  
  delayMicroseconds(2); 

  digitalWrite(trigger, HIGH);  
  delayMicroseconds(10);

  digitalWrite(trigger, LOW);
  duration = pulseIn(echo, HIGH); 
  distance = duration / 48.2; // value 148 for distances in inch

  Serial.print("Distance ");  
  Serial.print(distance);
  Serial.println("cm");

  if (distance > 0 && distance <= 100) 
  {

    char wavFile[101];
    sprintf(wavFile, "%d.WAV", distance);  
    // Serial.println(wavFile); 
    // String wavFile = String(distance + ".WAV");
    playcomplete(wavFile);

    lcd.clear();
    lcd.setCursor(3,0);
    lcd.print("Distance = ");
    lcd.setCursor(5,1);
    lcd.print(distance);
    lcd.setCursor(9,1);
    lcd.print("cm");
  }
  /*
  // playing if distances are more than 1Meter  
   if (distance>102) 
   {
   Serial.println("more than 1m");    
   playcomplete(wavFiles[19]);
   }
   
   // select for playing distances between 1m and 10cm  
   if (distance>=97&&distance<102)
   {
   Serial.println("100");    
   playcomplete(wavFiles[18]); 
   }
   if (distance>=92&&distance<97)   {
   Serial.println("95");   
   playcomplete(wavFiles[17]); 
   }
   if (distance>=87&&distance<92)   {
   Serial.println("90");    
   playcomplete(wavFiles[16]); 
   }
   if (distance>=82&&distance<87)   {
   Serial.println("85");    
   playcomplete(wavFiles[15]); 
   }
   if (distance>=77&&distance<82)   {
   Serial.println("80");    
   playcomplete(wavFiles[14]); 
   }
   if (distance>=72&&distance<77)   {
   Serial.println("75");    
   playcomplete(wavFiles[13]); 
   }
   if (distance>=67&&distance<72)   {
   Serial.println("70");    
   playcomplete(wavFiles[12]); 
   }
   if (distance>=62&&distance<67)   {
   Serial.println("65");    
   playcomplete(wavFiles[11]); 
   }
   if (distance>=57&&distance<62)   {
   Serial.println("60");    
   playcomplete(wavFiles[10]); 
   }
   if (distance>=52&&distance<57)   {
   Serial.println("55");    
   playcomplete(wavFiles[9]); 
   }
   if (distance>=47&&distance<52)   {
   Serial.println("50");    
   playcomplete(wavFiles[8]); 
   }
   if (distance>=42&&distance<47)   {
   Serial.println("45");    
   playcomplete(wavFiles[7]); 
   }
   if (distance>=37&&distance<42)   {
   Serial.println("40");    
   playcomplete(wavFiles[6]); 
   }
   if (distance>=32&&distance<37)   {
   Serial.println("35");    
   playcomplete(wavFiles[5]); 
   }
   if (distance>=27&&distance<32) 
   {
   Serial.println("30");    
   playcomplete(wavFiles[4]); 
   }
   if (distance>=22&&distance<27)   {
   Serial.println("25"); 
   playcomplete(wavFiles[3]); 
   }
   if (distance>=17&&distance<22)   {
   Serial.println("20");    
   playcomplete(wavFiles[2]); 
   }
   if (distance>=12&distance<17)   {
   Serial.println("15");    
   playcomplete(wavFiles[1]); 
   }
   if (distance>=7&&distance<12)   {
   Serial.println("10");    
   playcomplete(wavFiles[0]); 
   }
   
   // playing if distances are less than 10cm  
   if (distance<7)
   {
   Serial.println("less than 10cm");    
   playcomplete(wavFiles[20]);
   }
   */
  delay(1000);
}

/////////////////////////////////////////////////////////
// Plays a full 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 while its playing
  }
  // now its done playing
}

//////////////////////////////////////////////////////////
void playfile(char *name)
{
  // see if the wave object is currently doing something
  if (wave.isplaying) {// already playing something, so stop it!
    wave.stop(); // stop it
  }
  // look in the root directory and open the file
  if (!f.open(root, name))
  {
    putstring("Couldn't open file "); 
    Serial.print(name); 
    return;
  }
  // OK read the file and turn it into a wave object
  if (!wave.create(f))
  {
    putstring_nl("Not a valid WAV"); 
    return;
  }
  // ok time to play! start playback
  wave.play();
}

Back Back to top