And here is the code
L&P
PY
*/
//*********************************************
/* I use sleep for delay to save batter */
//**********************************************
#include <avr/sleep.h>
#include <avr/wdt.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define echoPin 7 // Echo Pin
#define trigPin 6 // Trigger Pin
#define LEDPin 13 // Onboard LED
#define to_note ((Pulsemax-Pulsemin)/(sizeof(notes)/2))
#define to_note_div (((Pulsemax-Pulsemin)/(sizeof(notes)/2))/6)
char f_wdt=0;
/*************** THe frequency of the piano notes from HI to LOW */
unsigned int notes[]={4978,4699,4435,4186,3951,3729,3520,3322,3136,2960,2794,2637,2489,2349,2217,2093,1976,1865,1760,1661,1568,1480,1397,1319,1245,1175,1109,1047,988,
932,880,831,784,740,698,659,622,587,554,523,494,466,440,392,415,370,349,330,311,294,277,262,247,233,220,208,196,185,175,165,156,147,139,131,123,117,110,104,98,
93,87,82,78,73,69,65,62,58,55,52,49,46,44,41,39,37,35,33,31};
int maximumRange = 430; // Maximum range needed in centimeter
int minimumRange = 4; // Minimum range needed
long Pulsemax = 25026; //430*58.2 how much time need for 430 cm
long Pulsemin =233; // 4*58.2
//int to_note,to_note_6;
long duration; // Duration used to calculate distance
void setupUltra();
void setup_WDT(){
// CPU Sleep Modes
// SM2 SM1 SM0 Sleep Mode
// 0 0 0 Idle
// 0 0 1 ADC Noise Reduction
// 0 1 0 Power-down
// 0 1 1 Power-save
// 1 0 0 Reserved
// 1 0 1 Reserved
// 1 1 0 Standby(1)
cbi( SMCR,SE ); // sleep enable, power down mode
cbi( SMCR,SM0 ); // power down mode
sbi( SMCR,SM1 ); // power down mode
cbi( SMCR,SM2 ); // power down mode
// setup_watchdog(4); // 1/2 second
}
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
f_wdt=0;
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
//****************************************************************
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
byte bb;
// int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
// ww=bb;
// Serial.println(ww);
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCSR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCSR = bb;
WDTCSR |= _BV(WDIE);
}
//****************************************************************
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
f_wdt=1; // set global flag
}
void setupUltra() {
// Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
digitalWrite(LEDPin, LOW);
// to_note=(Pulsemax-Pulsemin)/(sizeof(notes)/2); //
// to_note_6=to_note/6;
}
void setup() {
// iterate over the notes of the melody:
int x;
// Serial.begin(9600);
setupUltra();
setup_WDT();
}
#define full_note 80
//#define haft_note 80
void loop() {
// no need to repeat the melody.
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
int thedelay;
long theitem_rem,theitem;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH,Pulsemax+Pulsemin);// a little more than the furthest point
//Serial.println(duration);
// distance = duration/58.2;
thedelay=4;
if( duration<Pulsemax && duration>Pulsemin )
{
duration-=Pulsemin;
theitem=duration/to_note;
theitem_rem=duration%to_note;
// Serial.println(theitem);
if(notes[theitem]-notes[theitem+1]<3)
{tone(8, notes[theitem],full_note); delay(full_note);}
else
switch(theitem_rem/to_note_div)
{
case 0:tone(8, notes[theitem],full_note); delay(full_note);
break;
case 1:
case 2:
duration=(notes[theitem]-notes[theitem+1])/3;
tone(8, notes[theitem]-duration,full_note); delay(full_note);
break;
case 3:
case 4:
duration=((notes[theitem]-notes[theitem+1])*2)/3;
tone(8, notes[theitem]-duration,full_note); delay(full_note);
break;
default: tone(8, notes[theitem+1],full_note); delay(full_note);
break;
}
if(theitem>=24)thedelay=4; //256ms NORMAL
else
if(theitem>=20)
{// this is for the case somthing nearer up front about 1m
setup_watchdog(2); //1/2 second
system_sleep();
thedelay=3;
}
else
if(theitem>=16)thedelay=3; //256ms NORMAL
else if(theitem>=12)
{// SUCH A THE CHAIR FRONT
setup_watchdog(1); //1/2 second
system_sleep();
thedelay=2; //256ms}
}
else if(theitem>=7)//ARM Length
{//within arm length
// setup_watchdog(1); //1/2 second
// system_sleep();
thedelay=2; //256ms
}
else // verr very close
thedelay=1;//128ms Very close about to hit
// setup_watchdog(theitem);
//system_sleep();// 128 ms second
}
else thedelay=4;
if(theitem>(sizeof(notes)/2)-1) thedelay=5;
setup_watchdog(thedelay); //1/2 second
system_sleep();
}