I NEED HELP!

I am trying to program a robot to avoid obstacles with a sound sensor. This is my first project using arduino. I have verified and compiled my program it is ok no bugs. I have uploaded my program to my borad, the sensors are working but the two servo sweep motors isn’t working. I have tried the example code for only one servo it worked but when I try for the two motors it doesn’t work. Please examine my code and tell me where I am wrong.

/* Aim: I intend to create a four legged robot using using a HC-SR04 ultrasound sensors and two sweep servo motor as the legs.
*/

#include <NewPing.h>

/* Circuit connection:

  • VCC attached to 5V
  • GND attached to ground
  • TRIG attached to digital pin 3
  • ECHO attached to digital pin 4
    */

const int trigPin = 3;
const int echoPin = 4;

#include <Servo.h> // this will control the legs
Servo servoFL; // servo Fore Limb
Servo servoHL; // servo Hind Limb

int i;
int j;

void setup() {
Serial.begin(9600); // communicate with the pins(serial communication initialized)
servoFL.attach(5);
servoHL.attach(6);
}

void loop() {
/* using the code format for ping.
*/
long duration, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
cm = microsecondsToCentimeters(duration);

Serial.print(cm);
Serial.print(“cm”);
Serial.println();
delay(100); /* I used to print command for two reasons:
*So that I can monitor the distance read by the sensor.
*So that I can check if the sensor is working properly
*/
}

long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

int cm = long (microsecondsToCentimeters);

int CheckSensor() {
if (cm <= 7) { // at this point something is in it path
finditwayout();
}
else {
moveforward();
}
}
void finditwayout() { //move backwards and turn
do {
switch (j) {
case 1:
for (j = 130; j >= 90; j =+ 1) {
servoHL.write(j);
delay(50);
}
break;

case 2:
for (j = 90; j >= 70; j =+ 1) {
servoHL.write(j);
delay(50);
}
break;

case 3:
for (j = 70; j >= 50; j =+ 1) {
servoHL.write(j);
delay(50);
}
break;

case 4:
for (j = 50; j >= 30; j =+ 1) {
servoHL.write(j);
delay(50);
}
break;

case 5:
for (j = 30; j >= 10; j =+ 1) {
servoHL.write(j);
delay(50);
}
}
} while (cm <= 7);

void moveforward() {
left1();
delay(100);
left2();
delay(100);
right1();
delay(100);
right2();
delay(100);
}

void left1() {
for (i = 80; i <= 140; i =+ 1) { // goes from 60 degrees to 120 degrees
// in steps of 1 degree
servoF.write(i); // tell servo to go to position in variable ‘i’
delay(15); // waits 15ms for the servo to reach the position
}
}

void left2() {
for (j = 130; j <= 190; j =+ 1) { // goes from 130 degrees to 190
// in steps of 1 degree
servoH.write(i);
delay(15);
}
}

void right1() {
for (i = 140; i >= 80; i =- 1) { // goes from 120 degrees to 60
servoF.write(i);
delay(15);
}
}

void right2() {
for (j = 190; j >= 130; j =- 1) { // goes from 190 degrees yo 130
servoH.write(i);
delay(15);
}
}

It's usually best to start things off in the simplest way possible. If it works for 1 servomotor, then throw out all unnecessary code and just try to get the second servo moving in exactly the same way the first one moves. Use minimum code to get both motors moving in the same direction.

Sometimes, the issue might be due to your power supply or battery not being able to supply enough power (current) to both motors at the same time.

You probably need to indicate what kind of motors you're using - eg...make/model.

it is ok no bugs.

So, why are you here screaming your head off?

but the two servo sweep motors isn't working.

Sounds like a bug to me.

I have tried the example code for only one servo it worked but when I try for the two motors it doesn't work.

Well, of course not. The example only moves one servo. If you modified it, to make it deal with a second servo, then it is no longer "the example code" so you need to post it PROPERLY.

  /* using the code format for ping. 
   */
   long duration, cm;
   pinMode(trigPin, OUTPUT);
   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(trigPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);

    // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

ALL of this belongs in a function.

Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   delay(100); /* I used to print command for two reasons:
   *So that I can monitor the distance read by the sensor.
   *So that I can check if the sensor is working properly
   */
}

Sending the data to the serial monitor will NOT make the servos move. You actually need to call some more functions to make that happen. And, NO NOT the delay() function.

Um... Actually I do know that the sensors doesn't make the servo motor works. From my code you did notice the comment attached to the print command.Serial.print(cm);

Serial.print("cm");
Serial.println();
delay(100); /* I used to print command for two reasons:
*So that I can monitor the distance read by the sensor.
*So that I can check if the sensor is working properly
*/
}

I just want to know why isn't the servo motor working with my code? Thank you for the hint anyway. I am still new to the forum anyway.

Southpark you very correct. I modified the example code for the sweep motor to control two motors. I think it is the power as you said. But how do I step up the voltage ? Is there any small transformer for a servo motor. I am quite sure it isn't from my connection. Um I hope my codes are correct and I will try to make it simpler next time. Thank you all for helping me.

Naga..... so one servo is controlled by PWM pin 5, while the other servo is controlled by PWM pin 6, right?

So, if you remove the servo connected to pin 6, then the servo at pin 5 works, right?

Then, if you remove the servo at pin 5, and reconnect the other servo to pin 6, then the pin 6 servo works, right?

If each motor works (individually), then let us know what kind of power supply you're using to power those servo motors. That is, what's the power rating of the supply? Show us photos of the power supply...... details are needed.

Naga_4ever:
I just want to know why isn’t the servo motor working with my code?

Your code won’t even compile. That’s why.

Well, one reason, anyway. The other, if you fix the compilation issue, is that you never actually call any code to move the servos. They don’t move because you think they should. They move because you call some functions to make them move.

Put EVERY { on a line BY ITSELF.
Use Tools + Auto Format to properly indent your code.

Okay, I will try all you have said. Makes sense now, at least I know better now. Thank you so much! But PaulS I am still stunned my code compiled successfully. I verified it still no bug. The sensor reads fine, just that my servo doesn't respond to my intended purpose of the program. I will check again.

SouthPark my power source was my pc, then I tried using the adapter for ac supply(12V) and finally tried a dc source(a power-bank)(5v,1.04amps).

I verified it still no bug. The sensor reads fine, just that my servo doesn't respond to my intended purpose of the program.

That sounds suspiciously like a bug to me.

That sounds suspiciously like a bug to me.

To me, a bug is a flaw in a program that causes it to not produce the expected results. The fact that OP never actually tries to move the servos does not indicate a bug in the Servo library to me.

I can't see how loop() can be said to contain a bug, since it is doing EXACTLY what I expect. That it does not do what OP expects means that OP's expectations are wrong. The bug, if any, is on the software that OP is running, not the software OP is trying to write.

Naga... you're welcome. However, just make sure you read our replies properly. I asked you questions about your system ---- particularly about whether or not your motors will individually work (when connected only to pin 5 for one case, and when connected only to pin 6 for a separate case). We can't help you if we can't see or know exactly what your setup is. Otherwise, we'd be just guessing - which is sort of counter-productive.

Okay! My bad. I am really sorry. I forgot to answer to your reply. I am very sorry Southpark. Anyway yes my servo motor works individually in it particular pin and if I switch the pin where they are connected to the other they still work. They don't work when both servo are connected. I thought I told the power source for my Arduino, Please check the previous replies you will find it.

Naga.... ok ..... that sounds like good news then. If it works with individual motors, then that's a good sign indeed. The thing to try now is, power those motors with a power supply that can easily handle the power demand of the whole system. Hopefully that will sort it out.

Yeah! AWESOME! Umm... Advice me what type of power source will be good enough for the system(one sound sensor and two servo motors)?AC or DC

Naga_4ever:
Yeah! AWESOME! Umm... Advice me what type of power source will be good enough for the system(one sound sensor and two servo motors)?AC or DC

Do your servo motors operate with 5 Volt DC? If they do, then you will need to find out how much current each of your servos will be taking in (under maximum operating conditions). This will then allow you to estimate the amount of power that needs to be supplied by your DC power supply. And that will help to work out what kind of power supply you need to obtain. It's usually best to use a power supply that easily delivers the amount of power required by whatever it is powering. That is, handles all situations easily without over-heating or breaking a sweat etc.