I've spent far too many hours trying to get these codes to work.. Can someone please offer some assistant on the Interrupt part?
"'ISR' does not name a type" is my error.
Thanks in advance!
#include <Servo.h>
#define trigPin 7
#define echoPin 6
Servo servo;
int pos = 0;
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.attach(8);
cli();// stop interrupts
TCCR1A = 0;// set TCCR1A register to 0
TCCR1B = 0;
TCNT1 = 0; //initialize counter value to 0
//set timer count for 1khz increments
OCR1A = 15624;// = (1610^6) / (10008) - 1
//turn on CTC mode
TCCR1B |= (1 << WGM12);
//set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
//enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
//END TIMER SETUP
}
ISR{TIMER1_COMPA_vect){
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 8)
{
Serial.println("the distance is less than 5");
servo.detach();
}
else
{
servo.attach(8);
}
}
void loop()
{
for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
{
servo.write(pos);
delay(15);
} // waits 15ms for the servo to reach the position
// waits 15ms for the servo to reach the position
for (pos = 180; pos >= 0; pos -= 1)
{ // goes from 180 degrees to 0 degrees
servo.write(pos);
delay(15);
// tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
}
}