0
Offline
Newbie
Karma: 0
Posts: 24
I love Arduino
|
 |
« on: July 14, 2009, 06:26:30 am » |
Hi, I was new on C programing on Arduino, i wish like to control 4 servo motors angle by using Serial package. Please Guide what the best way to read serial and processing. By the the way i design to send like AB000C000D000E000 A = enable B = 1st servo. C = 2nd D = 3rd E = 4th. and 00 that is 000 value of angle. i try a little abit to read bit by bit but not work perfectly. Thank in Advance 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19032
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: July 14, 2009, 06:34:14 am » |
Don't try to do it all at once - it'll end in tears. Just take one servo and work on controlling that. Make sure you've got a separate power supply for the servos. You'll need to use something like MegaServo, because the standard servo library only supports two servos.
Start off just using the serial monitor to send your commands to the Arduino, so you're not having to debug and develop two lots of software at the same time. When you're happy that the Arduino end of things is working, start on the Processing.
Good luck.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 24
I love Arduino
|
 |
« Reply #2 on: July 14, 2009, 08:59:48 pm » |
Thank
Can anyone guide me the programing code
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19032
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: July 15, 2009, 02:02:14 am » |
Start by looking through the learning section for servo examples, maybe start with a pot and a servo, then start learning about the serial interface, and how to receive and convert numbers.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 24
I love Arduino
|
 |
« Reply #4 on: July 15, 2009, 06:07:16 am » |
I know how to control Servo,
but i don't know how to Convert Number, wait for someone givem some example
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19032
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: July 15, 2009, 06:24:32 am » |
Probably the simplest way is to use the function "atoi". For this, you need to store the ASCII decimal digits as they arrive, in , say a four byte array (assuming three digit numbers), make sure the last character is a zero ('\0') and then call atoi.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #6 on: July 15, 2009, 06:51:42 am » |
Here is some code I use to test the MegaServo library that should give you an idea of how to do what you want: #include <MegaServo.h>
#define NBR_SERVOS 12 // the number of servos, up to 48 for Mega, 12 for other boards #define FIRST_SERVO_PIN 2
MegaServo myServos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boards
void setup() { Serial.begin(9600); for(int i=0; i < NBR_SERVOS; i++) myServos[i].attach(FIRST_SERVO_PIN +i); }
void loop() { static int pos = 0;
if ( Serial.available()) { char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is ch a number? pos = pos * 10 + ch - '0'; // yes, accumulate the value else if(ch >= 'a' && ch <= 'a'+ NBR_SERVOS) // is ch a letter for one of our servos? { myServos[ch - 'a'].write(pos); // yes, save the position in the position array pos = 0; int channel = ch - 'a'; int angle = myServos[channel].read(); int pulseWidth = myServos[channel].readMicroseconds(); Serial.print("Servo on pin "); Serial.print(FIRST_SERVO_PIN + channel, DEC); Serial.print(": angle = "); Serial.print(angle,DEC), Serial.print(", pulse = "); Serial.println(pulseWidth,DEC); } else if (ch == '*') { // position all the servos for(int i=0; i < NBR_SERVOS; i++) myServos[i].write(pos); pos = 0; } else if (ch == '+') { // sweep the servos from 0 to 180 for(int angle = 0;angle < 180; ) { for(int i=0; i < NBR_SERVOS; i++) { myServos[i].write(angle); } angle = myServos[0].read(); Serial.print("Angle = "); Serial.print(angle,DEC), Serial.print(", pulse = "); Serial.println(myServos[0].readMicroseconds(),DEC); ++angle;
delay(20); } pos = 0; } else if (ch == '-') { // sweep the servos from 180 to 0 for(int angle = 180;angle >= 0; ) { for(int i=0; i < NBR_SERVOS; i++) { myServos[i].write(angle); } angle = myServos[0].read(); Serial.print("Angle = "); Serial.print(angle,DEC), Serial.print(", pulse = "); Serial.println(myServos[0].readMicroseconds(),DEC); --angle; delay(20); } pos = 0; } } } the code expects a serial command consisting of a valid value (0 through 180) followed by a letter indicating the servo to write to. The letter 'a' is the first servo, 'b' the second and so on. For example: 90a will write a value of 90 to the first servo 180b will write 180 to the second servo A value followed by a * instead of a letter will be written to all servos The single character + will sweep all the servos from 0 to 180 The single character – will sweep all the servos from 180 to 0
|
|
|
|
« Last Edit: July 15, 2009, 06:59:07 am by mem »
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 52
Arduino rocks
|
 |
« Reply #7 on: December 02, 2010, 01:28:43 pm » |
Hi mem, just a question... what would be the input pin on the 328 diecimilia board... can i use PB0 (pin 14) or must be Rx pin ( pin 2) ???
thanks in advance
GMV
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #8 on: December 02, 2010, 01:36:41 pm » |
The pins in that sketch are output pins. You can use any valid pin including pins 14 through 19
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 52
Arduino rocks
|
 |
« Reply #9 on: December 02, 2010, 01:57:55 pm » |
oh i see, so as an example and for driving 12 servos, looking at your sketch what would be the first ( i presume pin 2 ???) and last output pin that the code will look at ?
thanks
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #10 on: December 02, 2010, 02:31:13 pm » |
For 12 servos, if the first pin is 2 then the last pin is 13
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 52
Arduino rocks
|
 |
« Reply #11 on: December 02, 2010, 03:03:36 pm » |
the problem i have now is that i have reserved pin 8 as an input on my pcb , so is there any inconvenient to use any analog pin as an input for data coming in ?
thanks
|
|
|
|
« Last Edit: December 02, 2010, 03:05:44 pm by gmv »
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #12 on: December 02, 2010, 03:40:40 pm » |
You can create an array to define the pins you want to use. The sketch below uses pins 2 through 7 and 9 through 14 ( 14 is analog pin 0) You can change the values used to initialize the servPins array if you want different pins #include <Servo.h>
#define NBR_SERVOS 12 // the number of servos, up to 48 for Mega, 12 for other boards
Servo myServos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boards
const int servPins[NBR_SERVOS] = {2,3,4,5,6,7,9,10,11,12,13,14};
void setup() { Serial.begin(9600); for(int i=0; i < NBR_SERVOS; i++) myServos[i].attach(servPins[i]); }
void loop() { static int pos = 0;
if ( Serial.available()) { char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is ch a number? pos = pos * 10 + ch - '0'; // yes, accumulate the value else if(ch >= 'a' && ch <= 'a'+ NBR_SERVOS) // is ch a letter for one of our servos? { myServos[ch - 'a'].write(pos); // yes, save the position in the position array pos = 0; int channel = ch - 'a'; int angle = myServos[channel].read(); int pulseWidth = myServos[channel].readMicroseconds(); Serial.print("Servo on pin "); Serial.print(servPins[channel], DEC); Serial.print(": angle = "); Serial.print(angle,DEC), Serial.print(", pulse = "); Serial.println(pulseWidth,DEC); } else if (ch == '*') { // position all the servos for(int i=0; i < NBR_SERVOS; i++) myServos[i].write(pos); pos = 0; } else if (ch == '+') { // sweep the servos from 0 to 180 for(int angle = 0;angle < 180; ) { for(int i=0; i < NBR_SERVOS; i++) { myServos[i].write(angle); } angle = myServos[0].read(); Serial.print("Angle = "); Serial.print(angle,DEC), Serial.print(", pulse = "); Serial.println(myServos[0].readMicroseconds(),DEC); ++angle;
delay(20); } pos = 0; } else if (ch == '-') { // sweep the servos from 180 to 0 for(int angle = 180;angle >= 0; ) { for(int i=0; i < NBR_SERVOS; i++) { myServos[i].write(angle); } angle = myServos[0].read(); Serial.print("Angle = "); Serial.print(angle,DEC), Serial.print(", pulse = "); Serial.println(myServos[0].readMicroseconds(),DEC); --angle; delay(20); } pos = 0; } } }
|
|
|
|
« Last Edit: December 02, 2010, 03:41:08 pm by mem »
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 52
Arduino rocks
|
 |
« Reply #13 on: December 02, 2010, 04:38:58 pm » |
mem, thank again, it looks a lot easier this way, so i can use any analog pin as an output(digital) as well, ok thats nice....
To test this skecth i´ll be using windows Xp hyper terminal and send characters over the arduino to control servos, but in the future i´ll be making another pcb to read any ppm frame (futaba or Multiplex) from the radio trainer port, so i would like to ask you what do you recommend as the best method to read it and send the several servo "pulse width" as characters to the ardu decoder board.
I´ve been searching for info about this for a while and it looks to me that it will take some time till i find the best solution.
thanks
|
|
|
|
« Last Edit: December 02, 2010, 04:40:05 pm by gmv »
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #14 on: December 03, 2010, 04:55:31 am » |
i´ll be making another pcb to read any ppm frame (futaba or Multiplex) from the radio trainer port, so i would like to ask you what do you recommend as the best method to read it and send the several servo "pulse width" as characters to the ardu decoder board. That depends on what you have on your external PCB. I suggest you start another thread to discuss this so we don't hijack this one. Post a link here so we can find your new thread.
|
|
|
|
« Last Edit: December 03, 2010, 04:56:43 am by mem »
|
Logged
|
|
|
|
|
|