Hi all,
I'm trying to get a servo arm to swing upon an interrupt (INT0) being tripped. The issue is that it seems like the servo is pulsing back into the arduino (NANO) and tripping the interrupt pin. What happens is the servo moves slightly and then the interrupt is tripped in a never-ending loop. With the servo pin pulled (disconnected) the sketch runs fine (both LED and LCD function as programmed and no false interrupt trips).
What am I doing wrong?
/*
*/
// Servo
#include <Servo.h>
// I2C LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,20,4);
// Interrupt
volatile boolean flag = false; // assign an initial value to the interrupt
// Variables for all
const int led = 5;
const int servoPin = A3;
const int trimPotPin = A2;
int val = 0; // variable to read the value from the analog pin
Servo myservo; // create servo object to control the servo
// --------------------------------------------------------
void setup ()
{
attachInterrupt (0, isr, RISING); // attach interrupt handler
pinMode (led, OUTPUT);
// LCD
lcd.init();
lcd.backlight();
// SERVO
myservo.attach(servoPin);
} // end of setup
//----------------------------------------------------------
void loop ()
{
// Trim POT to move the servo arm into place --------------------------------------
val = analogRead(trimPotPin);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(15);
if (flag)
{
// Do this when hit is detected
digitalWrite(led,HIGH);
lcd.clear();
lcd.print("Detected"); // Print to LCD
myservo.write(90); // swing the servo arm
delay(3000); // Sets the delay for reset
// RESET back to ready state
digitalWrite(led,LOW);
lcd.clear();
myservo.write(0); // return servo arm back to ready position
}
flag = false; // clears the initial "TRUE" value so that we can again wait
// for another interrupt signal
} // end of loop
// --------------------------------------------------------
// Interrupt Service Routine (ISR)
void isr ()
{
flag = true;
} // end of isr