Pages: [1]   Go Down
Author Topic: Serial servo control [SOLVED]  (Read 591 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
      }
    }
  }
}
« Last Edit: December 26, 2011, 07:17:16 am by pimmes111 » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 147
Posts: 6038
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like this maybe?
Code:
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);
      }
    }
  }
}
Logged

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

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
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9631
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

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

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
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 147
Posts: 6038
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
   }
 }
Logged

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

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

Pages: [1]   Go Up
Jump to: