Go Down

Topic: I need code to run to modified 360 degrees servo motors. (Read 24482 times) previous topic - next topic

biotech

Hi everyone!

I am getting started with Arduino and so far so good. My first goal was to modify a servo motor to make it turn 360 degrees. Just today I connected it to the Adafruit motor shield and the Arduino board. With the code I got I was able to see the motor running non stop. To me that was kind of a giant leap!

Can someone please give me a simple code to run two modified servo motors as robot wheels. Maybe they can start turning forwards then stop, and start turning backwards. Something simple like that.

The other thing I need please is info about how to access the servo motors libraries. By now I only found in those libraries the knob and sweep routines but sure enough there is much more than that.

Any help will be more than welcome. Thanks!

zoomkat

Some test code where two us servo positions can be sent from the serial monitor to control two servos.

Code: [Select]

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test

#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control
  myservo2.attach(7);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
     
      // expect a string like 07002100 containing the two servo positions     
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters
     
      Serial.println(servo1);  //print to serial monitor to see parsed results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
           
      myservo1.writeMicroseconds(n1); //set servo position
      myservo2.writeMicroseconds(n2);
    readString="";
  }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

biotech

Hi Zoomkat,

Thank you for the code. I have tried it with only one modification. I set the servos in ports 9 and 10 as I am using the Adafruit motor shield.

I connected only one motor and and it still turns continuosly. Am I doing something wrong or maybe the modification in the electronics of the servos is not right?



Jantje

Th code states
Code: [Select]
// expect a string like 07002100 containing the two servo positions
So
1) start the serial monitor at baud rate 9600
2) send a string like 0100000 if you connected your servo to pin 6(not sure how you changed the code)
3) send a string like 1000000 if you connected your servo to pin 6(not sure how you changed the code)
4) see what the servo does and deduce a new value
5) redo 4 till the motor doesn't move anymore

Best regards
Jantje



Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

zoomkat

Quote
2) send a string like 0100000 if you connected your servo to pin 6(not sure how you changed the code)


Not the best starting point for testing. Most continous rotation servos have a neutral stop rotation value of ~1500us. usually full rotation in one direction is ~1400us, and ~1600us for the other direction. I'd start with a value like 15001500 and vary the values by +-10us to see how the rotation speed changes. Newer factory made continous rotation servos may have much broader speed change bands, but ~1500us should still be the neutral stoped value.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

biotech

Hi guys,

Thanks for the help.

I didn't know about the serial monitor feature. Right now I have connected the servo to the Adafruit and then the Arduino board to the computer via USB. I am trying to send the values you mentioned via the serial monitor, buy I get the error message "port 6 not found".

As mentioned before, due to the Adafruit motor shield I modified the code to ports 9 and 10. The problem is that when I try to change the port from 6 to 9 or 10 at the serial monitor it doesn´t allow me to do it.

This is the code I am using with the modifications:

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test

#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(9);  //the pin for the servo control
  myservo2.attach(10);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
     
      // expect a string like 07002100 containing the two servo positions     
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters
     
      Serial.println(servo1);  //print to serial monitor to see parsed results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
           
      myservo1.writeMicroseconds(n1); //set servo position
      myservo2.writeMicroseconds(n2);
    readString="";
  }
}

Any idea on how can I use the serial monitor feature if I am using a motor shield?

Thanks!


zoomkat

Quote
Any idea on how can I use the serial monitor feature if I am using a motor shield?


I suggest you remove the motor shield and then do some servo testing.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Jantje

biotech
My advice: go through the arduino samples. You are lacking quite some basic knowledge. Going through the samples will help you understand what is going on in this world.

Zoomcat
The adafruit motorshield only makes it easy to connect a servo. Basically the 2 times 3 pins (+ gnd pwm) are put nicely next to each other so you can plug in 2 servo's using the standard servo connector. it is a "extra feature" of the motorshield which is very often misunderstood as a real feature of the shield.
If you have the shield lying around and need to work with servo's it is convenient.

Best regards
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

biotech

Jantje/Zoomkat

Believe me Jantje that I have tried the Arduino samples for servos severaly times. But they are ment for 180 degrees servos.

I got the motor shield to make my life easier in my first attempt to modify a servo. However, so far it looks impossible to calibrate a servo using the shield. Is this right?

So as Zoomkat mentioned, I think that the best option is to connect the servo motor directly to the Arduino. But here I have yet another question. This is a modified servo, so shall I connect it to the Arduino as a normal servo or as a dc motor?

By the way, I am using a 9g microservo. May it be possible that microservos are more tricky than normal servos for calibration?

Thanks for the valuable support.

dxw00d

Whether or not you use the shield won't make any difference. The shield just gives convenient connection points to Arduino pins. You should drive them as servos, not dc motors.

Can you describe the steps you took to modify the servos?

Jantje


I didn't know about the serial monitor feature. ...

Believe me Jantje that I have tried the Arduino samples for servos severaly times. But they are ment for 180 degrees servos.


My advice: go through the arduino samples. You are lacking quite some basic knowledge. Going through the samples will help you understand what is going on in this world.

I stick to my advice, and I mean all the samples, not only the servo samples. You should do at least do the basic samples and blink without delay. Play around with the samples, change some stuff and see whether it does what you intended. It is a step backwards to better mover forwards.

Best regards
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

biotech

Jantje

You are right on that. I skept all those basic samples. Maybe it´s time to have a go on them as well. Thanks for the advise.

dxw00d

This are the steps I followed to modify the servo:
1- Remove the mechanical stop on the gear.
2- Desoldered the potentiomenter.
3- Solder in place two resistors.
4- Connected the servo to the shield and it runs continuosly as a DC.
4. Tried to calibrate the servo using the serial monitor feature but it doesn't seem to work with the servo connected to the Adafruit motor shield. The reason being is that serial monitor doesn´t allow me to change from channel 6 to channel 9 or 10 to do de calibration.

At the moment I am thinking to forget the motor shield and connect the servo directly to the Arduino. However, as I am not good with electronics it is going to take ma a while until I get the connectors for doing this.

Please if you see anything wrong with the steps I just mentioned let me know. Thanks!


AWOL

Quote
3- Solder in place two resistors.

What were the values, and how did you wire them?

zoomkat

Quote
2- Desoldered the potentiomenter.
3- Solder in place two resistors.


This may be where you have issues. I have unmodified 9G servos and they work with the code I posted. I also have standard servos I've modified like below and they also work with the posted code. If there is much mismatch between your resistors, you may not be able to get the servo response you want.

http://www.lynxmotion.net/viewtopic.php?f=31&t=6388
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

biotech

Zoomkat,

Are you telling me that you can make 9g servos work as normal DCs without modifying the electronics? Please let my know in case I got it wrong.

Regarding the desoldering of the pot and soldering of the resistors, I will try to do it all over again and see what happens. However, as far as I know I did it well.

Tomorrow I will get conectors for the servos so I can attach them directly to the Arduino (without the motor shield). That may be the first step to understanding what's going on.

Thanks!


Go Up