Go Down

Topic: Project with HC-SRO4 and two SM-S4303R servos and one SG90 bugging (Read 1 time) previous topic - next topic

Crochar

Hello, first time here, hope I do it right.

I'm trying to make this robot that has to move forward with the two SM-S4303R servos (continuous rotation), when a wall is detected (with the HC-SRO4 ultrasonic sensor) stop, move the head (with the SG90 servo wich has the ultrasonic sensor attached) to both sides, storing the distance it detects on variables and then turning to either side depending on the data detected.
I didn't connect anything with any resistance because I looked it up on the internet and it didn't say to.
The servers are connected like this: one cable to arduino's 5V, another cable to GND and the last one to the pin that controls it.

The problems started when trying to make the continuous rotation servos stop when a wall was detected, they would simply continue  going forward. I started printing the sensor's value to assure that it wasn't its roblem but everything was ok.  I "solved" the problem by making the servos go slow, as slow as i could. Sometimes i got errors like "the code can't be uploaded, board not connected" or things like that (i can't renmember the code errors exactly sorry). When the head servo was connected it started freaking out too trebbling and doing kind of random movements sometimes. I got it working by puttig some delays but new problems appeared and the code got kind of messy (you'll see it).

Oh, sometimes i get this error too
Code: [Select]
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00 avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00 avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00 avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00 avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00 avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00 avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00 avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00 avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00 avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00 Ha ocurrido un error mientras se enviaba el sketch

I got it kind of working, it moved forward, stopped when detecting a wall, looked to the sides and turned but suddenly, when i uploaded the code again it started freaking out again, acting like it was allways detecting a wall and therefore not stopping looking at the sides, sometimes moving both servos backwards (something i NEVER programmed, it just happens randomly) and other weird stuff, specially at the head servo.

I can provide any information needed so just ask.
I am sorry for the messy code but I re-did it so many times I am afraid of changing anything.
The servos appear to move in contrary directions but it's supposed to be programmed like that because the way they are placed in the robot. To move forward one has to move in the opposite direction because it's placed backwards.
The values on the continuous rotation sensor are not 100 and 80 because if I programmed them like that the moved at thifferent, wich makes no sense but I "calibrated" them so they moved at similar speeds.
Some variables are in Catalan so ask me if you want to know what they are.

At the start I was using a Uno board, now I'm using a MEGA board but it's the same.

Another error that happened is that i uploaded a code with just the "detect distance" code some servos moved although they were'nt even in the code.

I hope I did a good explanation, thank you all.

I hope

GrooveFlotilla

Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What have you got powering your project?
You cannot power servos from the Mega 5V pin, it cannot supply the current needed drive the servo motor.

Do you have a DMM?

Can you please tell us your electronics, programming, Arduino, hardware experience?

Did you write your code in stages, getting the input devices to work and then the output devices, then when they both work, combining them?

Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

Crochar

Hi,

Yes, I'm powering the servos with the 5V pins form the Arduino board but firstly it was working so I doubt that's the error.
Yes I have a DMM.
Actually I'm studying secondary school and I programmed quite a few things using, bluetooth, servos and all kind of sensors.
Yes, when I firstly got the eroorrs I re-wrote the whol code stage by stage and the servos were working. It all started bugging when I did a simple change on the value of a delay, which shouldn't affect the servos at all. For me it just seems like it all bugs out for no reason.

I ran the code again and now the robot simply acts af it was detecting allways a wall, moving the head to the sides and then the head bugs out after having looked to the left, it spasms.

Crochar

The forum doesen't let me upload the image, it says "Your attachment has failed security checks and cannot be uploaded. Please consult the forum administrator."

TomGeorge

Hi,
Upload jpg or png, keep the file size below 1M.

Tom.... :)


OPs Code.


Code: [Select]


// Echo







#include <Servo.h>





int pinSensorTrigger = 9;

int pinSensorEcho = 10;



int pinServoUlls = 11; //eyes

int pinServoDret = 4; //right servo

int pinServoEsquerre = 3; // left servo





Servo servoDret;

Servo servoEsquerre;

Servo servoUlls;





long duracio; // duration

int distancia; // distance

int distanciaDreta; // right distance

int distanciaEsquerra; // left distance



int estat = 0; // 0 = Move, 1 = Detect side distances , 2 = Turn









void setup() {

  

  pinMode(pinSensorTrigger, OUTPUT);

  pinMode(pinSensorEcho, INPUT);

  

  servoDret.attach(pinServoDret);

  servoEsquerre.attach(pinServoEsquerre);

  servoUlls.attach(pinServoUlls);

  

  Serial.begin(9600);

}



void loop() {



  if (estat == 0){

    

  servoUlls.write(70);

  

  servoDret.write(125);

  servoEsquerre.write(70);

  }



  if (estat == 1){

    

    servoDret.write(111);

    servoEsquerre.write(85);

    servoUlls.write(0);

    delay (800);



    digitalWrite(pinSensorTrigger, LOW);

    delayMicroseconds(2);

    digitalWrite(pinSensorTrigger, HIGH);

    delayMicroseconds(10);

    digitalWrite(pinSensorTrigger, LOW);

    

    duracio = pulseIn(pinSensorEcho, HIGH);

    distanciaDreta = duracio*0.034/2;



    delay(250);

    servoUlls.write(145);

    delay(800);



    digitalWrite(pinSensorTrigger, LOW);

    delayMicroseconds(2);

    digitalWrite(pinSensorTrigger, HIGH);

    delayMicroseconds(10);

    digitalWrite(pinSensorTrigger, LOW);

    

    duracio = pulseIn(pinSensorEcho, HIGH);

    distanciaEsquerra = duracio*0.034/2;



    delay(250);

    

    estat = 2;

  }

  if (estat == 2){

    if (distanciaDreta < distanciaEsquerra){  //Turn left

      servoDret.write(125);

      servoEsquerre.write(115);

      delay(300);

      estat = 0;

      

    }else{                                   //Turn right

      servoDret.write(85);

      servoEsquerre.write(70);

      delay(300);

      estat = 0;

    }

  }

  

    digitalWrite(pinSensorTrigger, LOW);

    delayMicroseconds(2);

    digitalWrite(pinSensorTrigger, HIGH);

    delayMicroseconds(10);

    digitalWrite(pinSensorTrigger, LOW);

    

    duracio = pulseIn(pinSensorEcho, HIGH);

    distancia = duracio*0.034/2;

    Serial.println(distancia);



    delay(600); // I repeat the code with a delay because some detection errors happened here



    digitalWrite(pinSensorTrigger, LOW);

    delayMicroseconds(2);

    digitalWrite(pinSensorTrigger, HIGH);

    delayMicroseconds(10);

    digitalWrite(pinSensorTrigger, LOW);

    

    duracio = pulseIn(pinSensorEcho, HIGH);

    distancia = duracio*0.034/2;

    Serial.println(distancia);





  if (distancia < 15){

    estat = 1;

    Serial.println("Less than 15");

  }

}




Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

Hi,
Can I suggest you use the NewPing library to get readings from your ultrasonic unit?

https://playground.arduino.cc/Code/NewPing

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

vinceherman

Yes, I'm powering the servos with the 5V pins form the Arduino board but firstly it was working so I doubt that's the error.
No.  Don't do that.  It might work on the bench with unloaded servos, but as soon as you make them actually do work, it overheats the regulator and things go bad.

Go Up