Go Down

Topic: [HELP] Move servo upon interrupt tripped. (Read 2869 times) previous topic - next topic

velocity101

Jan 17, 2014, 01:44 am Last Edit: Jan 17, 2014, 02:14 am by velocity101 Reason: 1
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?


Code: [Select]
/*

*/

// 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




johnwasser

Could it be that your interrupt pin is left floating and is picking up interference from the motor?  If the interrupt is coming from a switch connecting to +5V you need a pull-down resistor on the pin to keep it at Ground when the switch is open. Unconnected is not the same as 0V/Ground.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

velocity101


Could it be that your interrupt pin is left floating and is picking up interference from the motor?  If the interrupt is coming from a switch connecting to +5V you need a pull-down resistor on the pin to keep it at Ground when the switch is open. Unconnected is not the same as 0V/Ground.


Hi John,

I thought that might be the case as well and already have a 10k pull down resistor on INT0 to ground.  Still does it.  I put a multimeter on that pin and without the servo attached to its pin INTO reads what it's supposed to "0" without any fluctuations.  Once I hook up the servo to its control pin, INT0 swings up and down wildly...  I may have to resort to using a separate NANO to control just the servo and isolate it--accepting a TTL signal from the 1st NANO to trigger the servo actuation sequence... not very elegant and wasteful of two microcontrollers when one should have done.

From what you see, my sketch looks correct?

zoomkat

If you are trying to power the servo from the arduino, then you may be shorting out/resetting the arduino power when the servo tries to move.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

velocity101


If you are trying to power the servo from the arduino, then you may be shorting out/resetting the arduino power when the servo tries to move.


Yup. Realized that but I'm actually powering it from a bench regulated DC variable power supply.  It can handle up to 5 amps and the circuit hardly draws more than 0.5 amps.

Go Up