hi everybody!
I am currently working on a robot-tank. I am controlling it by an app for android. my problem is the following; I am trying to add a proximity sensor, so when ever it crashes it will stop the motors, but i want it mounted in a servo (thats where my problem is at the momet) I have added few lines to my original code allowing the robot to have a servo going left to right constantly, when i first turn it on it seemed to work, the servo was going left to right constantly but when i tried to controlled by my phone it would not move until i removed the servo from it, but even when i removed the servo the controlling side had a delay that i did not experience before.
i am attaching the code to see if you guys can help me. thanks in advance
#include <Servo.h>
Servo servoloco; // create servo object to control a servo
char puertoserie; // variable to receive data from the serial port
int motor_izquierda[] = {3, 5};
int motor_derecha[] = {6, 9};
int val = 0; // variable to store the value coming from the sensor
void setup() {
servoloco.attach(11); // attaches the servo on pin 11 to the servo object v
Serial.begin(9600); // start serial communication at 9600bps
int i;
for(i = 0; i < 2; i++){
pinMode(motor_izquierda[i], OUTPUT);
pinMode(motor_derecha[i], OUTPUT);
}
}
void loop() {
if( Serial.available() ) // if data is available to read
{
puertoserie = Serial.read(); // read it and store it in 'val'
}
if( puertoserie == 'w' )
{
go_forward_both_motors_on();
}
if( puertoserie == 's' )
{
go_backwards_both_motors_on();
}
if( puertoserie == 'd' )
{
go_right();
}
if( puertoserie == 'a' )
{
go_left();
}
if( puertoserie == 'q' )
{
stop_motors_both_motors_off();
}
if ( puertoserie == 'o' )
{
}
if ( puertoserie == 'z' )
{
stop_motors_both_motors_off();
}
{
servoloco.write(0); // sets the servo position according to the scaled value
delay(100); // waits for the servo to get there
servoloco.write(170); // sets the servo position according to the scaled value
delay(100); // waits for the servo to get there
}
}
void stop_motors_both_motors_off(){
digitalWrite(motor_izquierda[0], LOW);
digitalWrite(motor_izquierda[1], LOW);
digitalWrite(motor_derecha[0], LOW);
digitalWrite(motor_derecha[1], LOW);
}
void go_left(){
digitalWrite(motor_izquierda[0], HIGH);
digitalWrite(motor_izquierda[1], LOW);
digitalWrite(motor_derecha[0], HIGH);
digitalWrite(motor_derecha[1], LOW);
}
void go_right(){
digitalWrite(motor_izquierda[0], LOW);
digitalWrite(motor_izquierda[1], HIGH);
digitalWrite(motor_derecha[0], LOW);
digitalWrite(motor_derecha[1], HIGH);
}
void go_forward_both_motors_on(){
digitalWrite(motor_izquierda[0], LOW);
digitalWrite(motor_izquierda[1], HIGH);
digitalWrite(motor_derecha[0], HIGH);
digitalWrite(motor_derecha[1], LOW);
}
void go_backwards_both_motors_on(){
digitalWrite(motor_izquierda[0], HIGH);
digitalWrite(motor_izquierda[1], LOW);
digitalWrite(motor_derecha[0], LOW);
digitalWrite(motor_derecha[1], HIGH);
}
I wonder whether you may have got a conflict in the pins you're using. I suggest you list the hardware pins you are using and double check that the sketch is using them correctly. Remember to include any pins you're using for serial I/O.
Hi,
The fact that it was working, then wasn't and that adding and removing devices makes it work for a short while suggests that its a power problem.
If your using batteries, try a fresh set. If you are using large servos, motors and sheilds you will almost certainly need hobby quality batteries to take your project further.
hi, I have change all the batteries as you said and it works much better. do you know if i use the mega2560 it will perform better? now my next step is to add the srf05 sensor, so when an object is detected it should skip it or turn of the motors off so it doesn't crash. any idea of how to do that? also I have change the delay of the servo from 1000 to 600
Glad to hear the batteries did the job. If you've got a lot of servos etc powered from an Arduino you're stretching what it's capable of - the rule of thumb I've read is two small servos only. You've got 40mA per IO pin whether it's a Uno or a mega. But after that you can power your devices directly and control them from the Arduino which might also free you up to using a 2nd or larger battery for the robot circuits and get you back some reliability.
For how to do the object avoidance - search for examples of using the HC-SR04 which is the previous ultrasonic range sensor and 100% compatible with the 05. There 's even a recent library linked here Arduino Playground - SR04 which I expect should work with yours, though I've not tried it personally.
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
Servo Current mA
Futaba S3003 55
HITEC HS-322HD 75
ACE S1903 90
Bluebird BMS410 250
I'm a little interested in how you measured the servo currents. The way the S3003
and HS322 "analog" servos work is they use a short current pulse to the motor
every 20-msec, triggered by the signal pulse.
With no load, the current pulse is approx 2-msec long, and with increasing torque
loading, the current pulse lengthens up to 6-8 msec or so. So it's a form of
PWM, in fact. You should be able to measure this if you put a small R in the
ground lead and measure it using an oscilloscope.
__|^|___ or __|^^^^|__
That's if the servo isn't moving. If it's moving, then you end up with something
more like
@Poli:
You have agood start. I would suggest you immediately start to modularize your
code, so maybe take the serial checks and move to a separate subroutine such
as the following, and then call it from the main loop. Then it's more readable
when you start to add more functionality. For example,
void loop()
{
Remote();
....
//other stuff
....
}
void Remote()
{
if( Serial.available() ) // if data is available to read
{
puertoserie = Serial.read(); // read it and store it in 'val'
}
if( puertoserie == 'w' )
{
go_forward_both_motors_on();
}
....
}
For a good reference on building small robots, the following book is my
favorite, and has lots of info on hardware, sensors, and especially how
to write robot programs in C language. Very cheap, if you lived in the US.
The basic coding scheme is something called "subsumption", and there is info
on the following links from a guy who used it to build his robots. See the
software section on the first page.