Pages: [1]   Go Down
Author Topic: SERVO INTERRUPT  (Read 950 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

Here's my code:
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);
}
Logged

Belgium
Online Online
Edison Member
*
Karma: 68
Posts: 1926
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
#define DELAY 220
void TimedAction()
{ static long LastActionTime=0;

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

Best regards
Jantje

Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50883
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?

Quote
Here is my code
That can't be all of your code.
Logged

Pages: [1]   Go Up
Jump to: