Convert Polling program to Interrupt

Hello! I'd like to be able to convert my program which receives data from PC(C#) to arduino and then actuates the servo motor according to the data from the PC.

I'm confused as to how am i going to program it using interrupts since i can't find a guide where in I can serially receive data and interrupt the servo program. I have seen program that uses external interrupts but i need to 'interrupt' the program when, let's say, i receive another 'data position' from the PC.

Can anyone give me tips as to how i can accomplish this? Thanks!

//Code for adjusting the angle of the servo motor

#include <Servo.h>
#include <stdlib.h>


Servo myservo;

int pos = 0;
int pin = 9;
int del = 0;
int value = 0;

void setup()
{
  
  Serial.begin(9600);
  myservo.attach(9);
  myservo.write(30);
}

void loop()
{   
 if(Serial.available())
 { 
   
   char c = Serial.read();
   /*******************************************************************************************/
   
   if(c >= '0' && c <= '4')
   {
    value = (c - '0') * 6;
    myservo.write(value);
    delay(del);
   }
   else if(c >= '5' && c <= '9')
   {
     value = ((c - '0') + 1) * 6;
     myservo.write(value);
     delay(del);
   }
   if(c == '+')
   {
    myservo.write(30);
    delay(del); 
   }
   if(c == '!')
   {
    myservo.detach();
   }
   /*******************************************************************************************/

 }
}

What do you want to interrupt? Your example will run as fast as possible (as long as "del" == 0) .

Hi Msquare, for example i send i 'data position' to the arduino. Let's say it is 60 degrees. If i send another 'data position' which is 0 degrees while it is moving to the 60 degrees position, i need to be able to halt that first data position and execute the second position which is the 0 degrees.

Because what happens in my original code is that it waits the first data position to be executed before it executes the second data position. In my project, i have to be precise in timing and position that's why it was recommended to me to use Interrupts. Although, i'm getting confused on how to implement it using interrupts because there are 'a lot' of different ways to program it.

An interrupt only switches to an interrupt service routine. When that is finished the interrupted program continues. I do not think you want interrupts. You want to write your servo routine using the blink without delay timing method from the example program in the UzdE of the same name.

The standard Servo library - which is what your example uses - only takes a moment to do a servo.write(). It simply sets the pin to pulse so the Servo goes to the specified position. There is no "wait". The servo motor/electronics will do the motion.

If you send "04040" as one line to sketch what does the Servo do? I think it jitters a little and then ends up at the "0" position, and did not have time to go to the "4" position. The jitter is due to that it starts moving to the "4" but then is overriden by the new position "0".

If you do use the delay(del) with any other value the 0 then we do have a problem. In fact removing the delay() call as it is not used is a good thing. If you do use it (del>0) then you do BLOCK and we have a problem, like the one you describe.

Thank guys. I realized i don't really need to delay the movement of the servo motor. Thanks a lot!

Hello again! Is there anyway to prevent it from jittering if i attempt to do this?

void loop() 
{                             
    myservo.write(0);       
    myservo.write(180);  
}

Thanks!

Is there anyway to prevent it from jittering if i attempt to do this?

You've written your code to deliberately jitter, and you ask if there's a way of preventing it doing so?

No, not really. The servo gives no feedback on what position it has reached, so you do not know when (if ever) it has reached the position. So here you would need a delay() or write code with millis() so the "delay" does not block other code (if there is other code). You simply "guess" it will take 325mS(or whatever) to make the move.

Of course if you know that you want to go to 180, there is no need to go to 0 first, then it wont do the "false start" in the wrong direction.

You may want to use some expensive digital servo which does give feedback (do not know if such a thing exists - or which Arduno library can talk to it - but it wont be the standard Servo)

You could provide the feedback yourself. All you need to know is if the motor is still turning so attach something to the servo you can measure, say a pot of a rotary encoder. You are not interested in the position of count you get off this just if the motor is still turning. Maybe even a current monitor on the servo's current will give you an easy turning / not turning indication. Just a seriese resistor and a comparator to set the trigger point.