Go Down

Topic: I need help with Servo Code (Read 1 time) previous topic - next topic

PeterH

My advice, for what it's worth, is that every 'if', 'else', 'for', 'while' etc should always be followed by a compound statement, even if you only plan to have a single statement in it. In other words, put a '{' and '}' pair, each on separate lines please, and indented to the same amount. This avoids the type of bug that PaulS was hinting at when he asked his leading question.

PaulS

Quote
My advice, for what it's worth, is that every 'if', 'else', 'for', 'while' etc should always be followed by a compound statement, even if you only plan to have a single statement in it. In other words, put a '{' and '}' pair, each on separate lines please, and indented to the same amount.

Agreed. At work, whenever I type if(someCondition), I put the open and close curly braces in without even thinking about what to do in the block. It's just become automatic. Then, I go back and put the code in the block. Visual Studio automatically does the indenting and outdenting so, I rarely have to deal with mismatched indent levels, except when copying and pasting a block of code. When I do that, the first thing I do after pasting is correct the indent level to match the braces.

Saves untold amounts of grief.
The art of getting good answers lies in asking good questions.

Spaceman

Well, because of all who have helped in this topic, I got it!  Here is what it looks like:

Code: [Select]

//Written with alot of help by Spaceman

#include <Servo.h>

Servo servo01;
int pos = 0;

void setup ()
{
  Serial.begin(9600);
  servo01.attach(9);
}


void loop()
{
  randomSeed(analogRead(0));
  int OddEven = random(1, 3);
  int randNum = random(0, 180);
 
  if (OddEven ==1)
  {
    Serial.print("position_01");
    Serial.println(randNum);
    //delay(250); uncomment if you want to read slowly
    servo01.write(randNum);
      {
        for(pos = randNum; pos < 180; pos +=1)
          {
            servo01.write(pos);
            Serial.print("position_01_Next");
            Serial.println(pos);
            //delay(100); uncomment if you want to read slowly
          }
      }
  }     
  else
  { 
    Serial.print("position_02");
    Serial.println(randNum);
    //delay(250); uncomment if you want to read slowly
    servo01.write(randNum);
      {
        for(pos = randNum; pos>=1; pos -=1)
          {
            servo01.write(pos);
            Serial.print("position_02_Next");
            Serial.println(pos);
            //delay(100); uncomment if you want to read slowly
          }
      }
  }
}



My advice, for what it's worth, is that every 'if', 'else', 'for', 'while' etc should always be followed by a compound statement, even if you only plan to have a single statement in it. In other words, put a '{' and '}' pair, each on separate lines please, and indented to the same amount. This avoids the type of bug that PaulS was hinting at when he asked his leading question.


Not knowing much about programming, this is what made the difference.  I'll follow this practice from now on!

Now to figure out how to apply this to 4 servos working at the same time.  I saw somewhere in the forum about sketches.  Does anyone have any hints?

AWOL

The sketch is what you just posted.
Did you mean libraries, or even simply arrays?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

PaulS

#19
Jan 29, 2012, 06:46 pm Last Edit: Jan 29, 2012, 06:48 pm by AWOL Reason: 1
Quote
I saw somewhere in the forum about sketches.

Sketches are everywhere on the forum. Perhaps you meant arrays.

Code: [Select]
Servo myServos[4];

myServo[0].attach(somePin);
myServo[1].attach(anotherPin);

for(int n=0; n<4; n++)
{
 int pos = random(0, 180);
 myServo[n].write(pos);
}


Moderator edit: Code tags added, subscript corrected.
The art of getting good answers lies in asking good questions.

Spaceman


The sketch is what you just posted.
Did you mean libraries, or even simply arrays?


Frankly, I don't know what I mean.  I will look up libraries and arrays and let you know.  From a layman's point, I just meant multiple instructions for multiple pieces of equipment (4 servos) in the same (now I know what sketch is...) sketch.

AWOL

In that case, arrays and for loops.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Spaceman

Code: [Select]
Servo myServos[4];

myServo[0].attach(somePin);
myServo[1].attach(anotherPin);

for(int n=0; n<4; n++)
{
 int pos = random(0, 180);
 myServo[n].write(pos);
}


I can see the logic behind this, but don't know how to implement.  Can you clue me in?

PaulS

I don't know how much more help I can give you. There are three blocks of code, there. The first shows hoe to define an array of Servo objects.

The second shows how to call a method on an object in an array, individually.

The third shows how to use an index to iterate through the objects in the array.

How you adapt them to your code depends on how you want the servos to behave.
The art of getting good answers lies in asking good questions.

Spaceman

Thanks for the info,  I'll tinker around!

Go Up