Serial servo control [SOLVED]

I'm new to the Arduino world and I'm really stuck on this thing I want to try with a servo. Eventually I want to create a IR-radar but the servo control is the first part of this project.

I'm using the standard Servo code which is listed in the experimenter's guide (see below). I have been able to modify this piece of code so it starts when I press the '1' button in the serial monitor. However I want to be able to change the speed by pressing another button ('2' for slowing it down and '3' for speeding up) but the servo has to keep rotating all the time.

Is the while loop which I use a good idea or do I have to use something else. I'm pretty stuck and won't be able to interupt the while loop with a button press. This is the piece of code I'm using:

#include <Servo.h>

Servo servo;
int positie = 0;
int snelheid = 1;
int readInput = 0;

void setup()
{
  servo.attach(9);
  Serial.begin(9600);
  Serial.println("Test");
}

void loop()
{
  if (Serial.available() > 0)
  {
    readInput = Serial.read();
    
    while(readInput == '1')
    {
      for(positie = 0; positie < 180; positie += snelheid)
      {
        servo.write(positie);
        delay(15);      
      }
      for(positie = 180; positie >= 1; positie -= snelheid)
      {
        servo.write(positie);
        delay(15);
      }
    }
  }
}

Something like this maybe?

void loop()
{
  if (Serial.available() > 0)
  {
    readInput = Serial.read();
    
    if(readInput == '2')
   {
      if(snelheid < 10) snelheid++;
   }

    if(readInput == '3')
   {
      if(snelheid > 1) snelheid--;
   }

    if(readInput == '1')
    {
      for(positie = 0; positie < 180; positie += snelheid)
      {
        servo.write(positie);
        delay(15);      
      }
      for(positie = 180; positie >= 1; positie -= snelheid)
      {
        servo.write(positie);
        delay(15);
      }
    }
  }
}

It only rotates 180 degrees forward, then 180 degrees backward and quits. That's the problem I'm having all the time. It won't keep spinning up and down all the time

Have you looked a the servo "sweep" example in the IDE?

You mean this one? http://www.arduino.cc/en/Tutorial/Sweep

If you look at my code then you can see I've used that part but as soon I'm hooking up serial controls I have problems to keep the void loop() running and running because the servo has to reply on key pressing

If you want it to run all the time, then remove the serial input trigger for the movement. The code shows it will only run once per '1' key press. This will run all the time, but allow you to change the speed with the '2' (faster) and '3' (slower) keys.

void loop()
{
  if (Serial.available() > 0)
  {
      readInput = Serial.read();
    
      if(readInput == '2')
     {
        if(snelheid < 10) snelheid++;
     }

      if(readInput == '3')
     {
        if(snelheid > 1) snelheid--;
     }
  }

   for(positie = 0; positie < 180; positie += snelheid)
   {
     servo.write(positie);
     delay(15);      
   }
   for(positie = 180; positie >= 1; positie -= snelheid)
   {
     servo.write(positie);
     delay(15);
   }
 }

Thank you very much, that is exactly what I'm looking for and now I realise what went wrong as well :slight_smile: