// when I used 2 time timer1.attachinterrupt it will not continue to keep running the loop
#include <TimerOne.h>
// value 14 is will not be used
#define speakerPinLeft 14 //11 is the correct one
#define trigPinLeft 9 //9 is the correct one
#define echoPinLeft 2 //2 is the correct one but if 2 and 3 pin on then result fail
#define speakerPinRight 11
#define trigPinRight 13 // 13 is the correct one
#define echoPinRight 3 // 3 is the correct one
#define ledyellowleft 14 //12 is the correct one
#define ledredleft 14 //10 is the correct one
#define ledyellowright 12
#define ledredright 10
int iLeft, iRight;
//For Left Counter
unsigned long SEC=1000; //1sec = 10^6microsecond
long pstart=0;
long pstop=0;
//For Right Counter
unsigned long SEC2=1000; //1sec = 10^6microsecond
long pstart2=0;
long pstop2=0;
void setup() {
Serial.begin (9600);
pinMode(trigPinLeft, OUTPUT_FAST);
pinMode(echoPinLeft, INPUT_FAST);
pinMode(trigPinRight, OUTPUT_FAST); // ori OUTPUT_FAST
pinMode(echoPinRight, INPUT_FAST); // INPUT_FAST
pinMode(ledyellowleft, OUTPUT);
pinMode(ledredleft, OUTPUT);
pinMode(ledyellowright, OUTPUT);
pinMode(ledredright, OUTPUT);
Timer1.initialize(SEC); // ori location
//you need at least 15us + ping time. it didnt work below 1000us for me for below code
}
void pingUltra()
{
digitalWrite(trigPinLeft, LOW);
delayMicroseconds(2);
digitalWrite(trigPinLeft, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinLeft, LOW);
// NOT checking for LOW. it seemed to hang here if i used it.
pstart=micros();
while(digitalRead(echoPinLeft)==HIGH){}
pstop=micros();
iLeft = (pstop-pstart)/29/2; //testing only
}
void pingUltra2()
{
digitalWrite(trigPinRight, LOW);
delayMicroseconds(2);
digitalWrite(trigPinRight, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinRight, LOW);
// NOT checking for LOW. it seemed to hang here if i used it.
pstart2=micros();
while(digitalRead(echoPinRight)==HIGH){}
pstop2=micros();
iRight = (pstop2-pstart2)/29/2; //testing only
}
void loop() {
Timer1.attachInterrupt( pingUltra ,SEC); // ori location
Timer1.attachInterrupt( pingUltra2 ,SEC); // ori location
Serial.print (iLeft);
Serial.print (" CM left ");
Serial.print (iRight);
Serial.println (" CM right");
delay (500);
}