Go Down

Topic: Serial servo control [SOLVED] (Read 655 times) previous topic - next topic

Dec 25, 2011, 03:50 pm Last Edit: Dec 26, 2011, 01:17 pm by pimmes111 Reason: 1
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:

Code: [Select]

#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);
     }
   }
 }
}

SurferTim

Something like this maybe?
Code: [Select]
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

zoomkat

Have you looked a the servo "sweep" example in the IDE?
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

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

SurferTim

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.
Code: [Select]
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 :)

Go Up