Servo Motors with Bluetooth

Hi, I am fairly new to Arduino and have not been able to find an answer to this problem.

Basically, I have modified the Servo Sweep example sketch so that I can control a servo motor from an android device, through bluetooth. I have it set up so that when I type in the value '0', '1', '2' or '3', the servo motor will move to a specified angle. I was wondering if it is possible to use an alternative to 'delay' so that I can interrupt the case '0'. At the moment I have to wait for the servo motor to finish moving before I can press another value.

Any help would be greatly appreciated, thanks. :slight_smile:

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600); //begins serial communication
} 
  
void loop() 
{ 
  int pos;
  if (Serial.available()){
    delay(100);
    while(Serial.available()>0){
      pos=Serial.read();     //reads the value sent from Visual Basic  
      if(pos=='0'){
   myservo.write(45);  
   delay(1000);          
   myservo.write(0);  
   delay(1000);          
   myservo.write(90); 
   delay(1000);          
   myservo.write(135); 
   delay(1000);          
   myservo.write(180); 
   delay(1000);          
   myservo.write(90);  
   delay(1000);}          
      else if(pos=='1')
        myservo.write(-90);  //rotates the servo 90 degrees (right)
      else if(pos=='2')
        myservo.write(180);  //rotates the servo 180 degrees (Left)
      else if(pos=='3')
        myservo.write(-180); //rotates the servo 180 degrees (right)     
    }
  } 
}

The demo several things at a time illustrates how to manage timing using millis()

...R

Thanks for pointing me in the right direction Robin2. I've learnt a lot from that link :slight_smile: