SERVO INTERRUPT

Hello everyone...

I'm trying to control 2 servo, one will keep sweeping automatically while the others will change position when i push my transmitter...
I know this need to use interrupt but i cant really understand how to write the program...
I try to write it out & hope someone can point out my mistake...
Thank you very much :slight_smile:

Here's my code:

#include <Servo.h>
 
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;
int pos = 0;    // variable to store the servo position 
volatile long transmitterValue; // to store the transmitter position/value

void setup() 
{   
  myservo1.attach(10);   
  myservo2.attach(11); 
  attachInterrupt(0, servoPosition, CHANGE); //interrupt function to control the servo position when receive signal
  Serial.begin(57600); // to link the board & send the data to computer
  Serial.println("Done with setup");
}

void checkTransmitter()
{
  transmitterValue = ( pulseIn(2, HIGH, 100000) / 10 ); // Read RC channel from the interrupt pin
}

void servoPosition ()   // interrupt function
{ 
  if (  transmitterValue < 130 )
  {
    myservo2.write(30);
  }  
  
  else if ( transmitterValue > 150 )
  {
    myservo2.write(150);
  }

  else
  {
    myservo2.write(90);
  }
}

void sweepingServo()
{ 
    for(pos > 30; pos < 150; pos += 1)  
    {                                  
      myservo1.write(pos);             
      delay(20);      
    } 
    
    for(pos < 150; pos > 30; pos -= 1)     
    {                                
      myservo1.write(pos);              
      delay(20);     
    }
}

void loop ()
{
  checkTransmitter();
  sweepingServo();
  servoPosition ();
  Serial.print("data:");
  Serial.println(transmitterValue);
  delay(200);
}

There is no need for interrupts for what you want to do.
What I would do is as follows:
delete all the calls to delay (bad practice if you want some real time activity) and replace them with time conditional firing. Like in the code fragment below.

#define DELAY 220
void TimedAction()
{ static long LastActionTime=0;

  if( (LastActionTime + DELAY) > millis() )
  {
    LastActionTime=millis();
   //do something time based     
   }
}

Best regards
Jantje

I did that too... But when i want to change my degree, for example from 180 degree to 160 degree, my servo just stop working and i still cant figure out why is this happen.

Here is my code:

if ((timerVal + 50) <= millis())  // Timer loop
  {    
     myservo1.write(pos);     // tell servo to go to position in variable 'pos' 
     pos = pos + count;       // increment or decrement value
     
     if (pos == 180)         // If servo reaches this position, change direction
     {                
       count = -10;          // decrement in directon
     } 
     if (pos == 0) 
     {                 
       count = +10;          // increment in directon
     } 
     timerVal = millis();    // Reset the timer
  }

By the way i really need the interrupt program, hope someone will guide me. Thanks :slight_smile:

By the way i really need the interrupt program, hope someone will guide me.

Why?

What is triggering the interrupt that fires the interrupt service routine?

Here is my code

That can't be all of your code.