Pages: [1]   Go Down
Author Topic: code differences  (Read 519 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to write code for a quad project (first) I am working on and am trying to figure out the best approach.I am using a SSC32 with a Mega and have but a basic understanding of how to go about it  but with the vast amount of info online it tends to get out of had fast. So I guess really I found to pieces of code online and looks like they for the most part do the same thing but was wondering if one was a better way of doing it then the other or if it does not matter at all? Below are the two examples and I understand that one is using just one servo and the other is using 3.

Example 1.

void setup() {
Serial.begin(9600);
}

void loop() {
move(1, 2000, 100);
move(1, 1000, 100);
}

void move(int servo, int position, int time) {
Serial.print("#");
Serial.print(servo);
Serial.print(" P");
Serial.print(position);
Serial.print(" T");
Serial.println(time);
delay(time);
}


Example 2.

void setup() {
Serial.begin(115200);                             
}
void loop() {
 
  Serial.println("#0 P750 #4 P750 #11 P750 T500");         
  delay(1000);                                                                     
  Serial.println("#0 P2200 #4 P1500 #11 P2200 T500");
  delay(1000);
}


Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 70
Posts: 2172
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Example 2 uses hard coded values, maybe a test case of what you'd expect of the first code modified for 3 servos.
By itself it is useless. Whereas example 1 allows a function to be called with different values.

Neither code does really anything apart from write text to the serial output.

What is it you want to achieve,
move servos?
print output to serial?
something else?
Logged


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

I am trying to move servo's and I am able to do that but just trying to figure what way is the better way.I have a quad that I am trying to make walk and the first thing I want to be able to do is just have it walk straight.Currently I have 3 servos per leg and it has been slow going so I am looking for any advice or tips.
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The two examples may produce similar output but they do it in different ways.

Example 1 uses a function to output the data.  The function can be used from anywhere in the program and different values can be passed to it so it can be used to achieve different results at different times and could, for instance, be called from a for loop to produce a series of actions.

Example 2 uses hard coded values which will be the same every time.  Each movement would need to be coded explicitly and could not be changed by changing a variable, such as in a for loop.

Using a function that takes parameters is, therefore, the preferred way to code this.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

well what would example 1 look like if it was 3 servos?
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is my interpretation of how commands are sent to the servos.
Code:
void loop() {
move(1, 2000, 100);  //servo 1 to position 2000 in time 100
move(2, 1000, 150);  //servo 2 to position 1000 in time 150
move(3, 1500, 220);  //servo 3 to position 1500 in time 220
}
I assume that there is a device at the other end of the serial link that is receiving the commands, interpreting them and passing them on to the servos.  Have you got such a device ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

Yeah I have 12 servos connected to the ssc32 servo controller on a quad I am trying to build. The whole thing is a little daunting because my programming skill's are non existent, I do have a basic understanding but thats about it. So the code you posted is all that would need to be added for 3 servos and would work for 12, say for example?
Logged

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

I think I might have deleted some of my comments here smiley-sad
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The principle will work for as many servos as the receiving device can support but whichever method you use bear in mind that the calls to the function will happen in a sequence and not all at the same time.  It is up to the receiving device to interpret and act on the commands appropriately. 
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: