loading

Arduino servo random sweep help?

Hello to the ibble world,
I have tried to generate a random sweep for two servo connected to 9 and 10, the problem I have is I don't know enough about the code to figure out where I have gone wrong. Same old story, I have taken the standard code from the examples "sweep" and tried to add a second servo using a random position genorator.
I would appreciate some advice with the following case and if you can help me to understand where I went wrong I should not make the same mistake next time :-)  (probably!)
It seems to generate the fault where I try to use "change(ypos,ynew)" I have defined change but somewhere I have dropped a boo boo.

Thanks in advance.

Kind regards

Darren

// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.


#include <Servo.h>

Servo xservo;  // create servo object to control a servo
Servo yservo;  // a maximum of eight servo objects can be created

int xpos = 0;    // variable to store the servo position
int ypos;
int ynew;

void setup()
{
  xservo.attach(9);  // attaches the servo on pin 9 to the servo object
  yservo.attach(10);
  ypos = random(180);
}
#define change(a,b) if (a>b) a--; else (a<b) a++;

void loop()
{
  for(xpos = 0; xpos < 180; xpos += 1)  // goes from 0 degrees to 180 degrees
  {                                     // in steps of 1 degree
    xservo.write(xpos);                 // tell servo to go to position in variable 'pos'
    delay(10);                          // waits 15ms for the servo to reach the position
  }

  for(xpos = 180; xpos>=1; xpos-=1)      // goes from 180 degrees to 0 degrees
  {                               
    xservo.write(xpos);                  // tell servo to go to position in variable 'pos'
    delay(10);                           // waits 15ms for the servo to reach the position
  }
   {
    ynew = random(180);
    while ((ypos != ynew))
    yservo.write(ypos);
    {
      change(ypos,ynew)
      yservo.write(ypos);
      delay (10);
    }    
}

sort by: active | newest | oldest
Sembot1 year ago

Hai

I love this instructable , thanks for sharing it

I built a mini cnc "http://www.thingiverse.com/thing:1188744"

Now my question is

through a program I want to all the movements and stops random

1: X axis random : left, right and some time even stop random

2: Y axis random : left, right and some time even stop random

3: Z axis random : occasionally up or down

I'm a fairly good homebuilder but a bad programmer

Can you help me please

Kind regards

Maewert program dont works i have a error in the last lines ????

Djandco (author)  Sembot1 year ago
Hi,
I am not much good with the code side of things myself, I just tend to hack away at existing code.

I would have thought Maewert would be able to advise you on this as he did take the time to explain how the stuff actually worked instead of just saying here you go.

Sorry I can't really help
maewert5 years ago
I think your initial problem is in your looping.

The Loop routine is executed over and over:
It starts by moving the X servo from 0 to 180 then back again.
I suspect this works fine.

Then it appears you start to work the Y servo.

// { <== this brace isn't needed, lets get rid of it.
ynew = random(180); // Here you set ynew to a random value
while ((ypos != ynew)) // Here you start a loop
yservo.write(ypos); // this is the entire loop! Your program gets stuck here
{
change(ypos,ynew)
yservo.write(ypos);
delay (10);
}
}


If you get rid of the 'yservo.write(ypos);' after the while loop it should work as you wanted.

Best Wishes....

//Here is the fixed file:
// Sweep
// by BARRAGAN
// This example code is in the public domain.


#include

Servo xservo; // create servo object to control a servo
Servo yservo; // a maximum of eight servo objects can be created

int xpos = 0; // variable to store the servo position
int ypos;
int ynew;

void setup()
{
xservo.attach(9); // attaches the servo on pin 9 to the servo object
yservo.attach(10);
ypos = random(180);
}
#define change(a,b) if (a>b) a--; else (a
void loop()
{
for(xpos = 0; xpos < 180; xpos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
xservo.write(xpos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}

for(xpos = 180; xpos>=1; xpos-=1) // goes from 180 degrees to 0 degrees
{
xservo.write(xpos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
ynew = random(180);
while ((ypos != ynew))
{
change(ypos,ynew)
yservo.write(ypos);
delay (10);
}
}
Djandco (author)  maewert5 years ago
Hay meawert, thanks for the info :-)

You would not beleave how much I am beginning to undserstand thanks to simple explanations that you and others give.

I have even been out and bought two of Simon Monks books to help me along the way. All I need to do now is find the time to read them.

I ended up using one very simple code to random both servos

#include

Servo xservo;
Servo yservo;

int xpos;
int ypos;

void setup()
{
xservo.attach(9);
yservo.attach(10);
}

void loop()
{
{
ypos = random(180); //generate random value for y-servo
yservo.write(ypos); //y-servo moves to new position
delay(20);
}
{
xpos = random(180); //generate random value for x-servo
xservo.write(xpos); //x-servo moves to new position
delay(20);

}
}

This gives a random value to both which seems does just fine.

Beleive it or not this is to put a laser onto to entertain my dogs in the yard :-)
I am also trying to build a pneumatic ram to throw the ball for them while I am at work.
We have plans!

Kind regards

Darren
Libahunt5 years ago
I am a beginner in Arduino too and I have no servos to test this code myself, so excuse me, if I can't give clear explanations about everything, but I have some experience in simple kind of programming and I think I can point out suspicious parts in the code.

First
#define change(a,b) if (a>b) a--; else (a<b) a++;
That does not look at all like the syntax in reference http://www.arduino.cc/en/Reference/Define. Throw this line out for a start, whatever it is meant to be probably can be achieved with more beginner-friendly methods than define and change.

Second
{
    ynew = random(180);
    while ((ypos != ynew))
    yservo.write(ypos);
    {
      change(ypos,ynew)
      yservo.write(ypos);
      delay (10);
    }   
If you want the servo to go straight to new position without the 1 degree steps, then remove outer curly brackets from this part. No loop or condition - no brackets. If it is supposed to go to new position in steps then it needs a bit more complicated code than the sweep to indicate which is bigger, current or new position, and increasing OR decreasing it accordingly.
Here:
while ((ypos != ynew))
     yservo.write(ypos);
     {
       change(ypos,ynew)
       yservo.write(ypos);
       delay (10);
}
I guess you instead meant something like that:
if(ypos != ynew) {          //if current and new positions differ
  ypos = ynew;              //then change ypos value to new
  yservo.write(ypos);     //and move servo to that position
  delay (10);
}
(I think the test, if they differ, is not absolutely necessary in this case. You could just generate new value and move servo to this, if it is the same as previous nothing happens. But you can keep it just for coding practice.)


I did not understand exactly but guess you want one servo to do a slow sweep and other move randomly at the same time? I they are attched to each other then looks like a uneven zig-zag. Then I would suggest something like that instead:

#include <Servo.h>

Servo xservo;
Servo yservo;

int xpos;
int ypos;

void setup()
{
  xservo.attach(9);
  yservo.attach(10);
}

void loop()
{
  for(xpos = 0; xpos < 180; xpos += 1)
  {                                    
    xservo.write(xpos);         //x-servo goes 1 degree further
    ypos = random(180);     //generate random value for y-servo
    yservo.write(ypos);     //y-servo moves to new position
    delay(10);                      
  }

  for(xpos = 180; xpos>=1; xpos-=1)     
  {                              
    xservo.write(xpos);         //x-servo goes 1 degree backwards
    ypos = random(180);     //generate random value for y-servo
    yservo.write(ypos);     //y-servo moves to new position                
    delay(10);                          
  }
Djandco (author)  Libahunt5 years ago
Thank you for replying liba_hunt, I will have a look at this tomorrow and give it a go.

I tend to learn by doing as when i read things they don't seem to make sense. The problem when you lear this way it is mainly learn by mistakes!

So, one day I might even sit down and read the book while I have a code to work on.

I think I tried to over complicate things, and your code is a lot clearer and makes sense.

Thank you again

Kind regards

Darren
You're welcome :)

I just started to think that delay 10 millisecons might not be enouch time for the y-servo to make the whole many-degrees move, so maybe try a longer delay there.
Djandco (author)  Libahunt5 years ago
I changed it to 20 ms and it seemed to work fine :-)

The main problem I have is the servos i bought off eBay. every time they run the shut down the power supply.
they may be cheap but they don't work!

So, two Futaba servo's later and it works fine :-)

Thanks again

Kind reagrds

Darren