Go Down

Topic: Ping Robo code error (Read 3754 times) previous topic - next topic

hugher256

Apr 26, 2012, 06:42 pm Last Edit: Apr 27, 2012, 07:10 pm by AWOL Reason: 1
My code for Ping robot.
There sumthing wrong in my code i cannot fix.
Please see LeftServo.refresh();




Code: [Select]
#include <Servo.h>     // includes the servo libs
Servo LeftServo;       // name the servo
Servo RightServo;      // name the servo
int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0;
int val2 = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin = 13; // LED connected to digital pin 13
int lightSense = 2;
int headLight = 8;

void setup(){
  pinMode(6,OUTPUT);
  pinMode(5,OUTPUT);
  LeftServo.attach(6); // Attaches the Left Servo to PWM pin 6 on the Arduino
  RightServo.attach(5); // Attaches the Right Servo to PWM pin 5 on the Arduino
  pinMode(headLight, OUTPUT);
  pinMode(lightSense, INPUT);
  beginSerial(9600);                  // Sets the baud rate to 9600
  pinMode(ledPin, OUTPUT);            // Sets the digital pin as output
}

void moveServoLeftTo(int angle, int duration){
 // command the servo to move to the given angle for the given number of milliseconds  
  LeftServo.write(angle);
  for( ; duration > 0; duration -= 20){  // loop for the given number of milliseconds by subtracting 20ms each iteration
LeftServo.refresh();             // call the service routine to move the servo
delay(20);          // wait 20 milliseconds  between each refresh
  }
}
void moveServoRightTo(int angle, int duration){
 // command the servo to move to the given angle for the given number of milliseconds  
  RightServo.write(angle);
  for( ; duration > 0; duration -= 20){  // loop for the given number of milliseconds by subtracting 20ms each iteration
RightServo.refresh();            // call the service routine to move the servo
delay(20);          // wait 20 milliseconds  between each refresh
}  

}
void loop() {

// Ping))) functions: Doing more than I need it to, I think, but code

timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT);   // Switch signalpin to output

 digitalWrite(ultraSoundSignal, LOW);  // Send low pulse
 delayMicroseconds(2);                 // Wait for 2 microseconds
 digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
 delayMicroseconds(5);                 // Wait for 5 microseconds
 digitalWrite(ultraSoundSignal, LOW);  // Holdoff

 pinMode(ultraSoundSignal, INPUT);     // Switch signalpin to input
 val = digitalRead(ultraSoundSignal);  // Append signal value to val
 while(val == LOW) {                   // Loop until pin reads a high value
   val = digitalRead(ultraSoundSignal);
}

 while(val == HIGH) {                  // Loop until pin reads a high value
 val = digitalRead(ultraSoundSignal);
 timecount = timecount +1;           // Count echo pulse time
}

ultrasoundValue = timecount;          // Append echo pulse time to ultrasoundValue

if(timecount > 0){
 digitalWrite(ledPin, HIGH);
}

//delay(1);

// Servo functions: The basics

 if(ultrasoundValue > 500){
moveServoLeftTo(45,50);   // move the left servo to 45 degrees for 500 milliseconds
moveServoRightTo(180,50);  // moveRightTo function is left as an excercise ;)
 }
 
 if(ultrasoundValue < 500){
     moveServoLeftTo(180,40);
     moveServoRightTo(180,40);

 }
   
    if(ultrasoundValue < 100){
     moveServoLeftTo(180,100);
     moveServoRightTo(45,100);
     //moveServoLeftTo(45,50);
     //moveServoRightTo(180,50);

 }
// Headlight - Turns on when photoresistor is triggered  
// Doesn't seem to work on the same circuit... not sure why yet.
/*  val2 = digitalRead(lightSense);  // read input value
 if (val == HIGH) {            // check if the input is HIGH
   digitalWrite(headLight, LOW);  // turn LED OFF
 } else {
   digitalWrite(headLight, HIGH); // turn LED ON
 */
   
}


Moderator edit: code tags corrected.


PaulS

{code} is supposed to be [ code ] (without the spaces). Ditto for {/code}.

Quote
There sumthing wrong in my code i cannot fix.

What would that something be? Aside from the fact that that is NOT how to read a ping sensor.

dxw00d

Which servo library are you using? I don't recognise Servo.refresh().

hugher256

Sorry i dont know about the code as i copied it from sumwhere.
And also i dont know about library used.
if someone can provide me with the code for ping robot the help will be really really appreciated.

dxw00d

I suggest you start with learning how to read a ping))) sensor - http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor
Then look at the Servo library - http://arduino.cc/en/Reference/Servo

hugher256

Sorry after this also i cannot under stand.
One question-
How to make robot rotate 45 degree?
i have two servos.
OR
Make one servo stop to make other servo rotate?

dxw00d

Are you using normal servos, or continuous-rotation servos?

hugher256

continuous-rotation servos

dxw00d

Ok. They will be stopped when you set them to somewhere around 90 degrees. Settings higher or lower than the stop position will make them rotate in one direction or the other.

To make your robot rotate 45 degrees, assuming differential drive, you'll need to start the servos in opposite directions, and stop them after the robot has rotated as far as you want. If you have no wheel encoders, or any other way of getting rotational feedback, you'll have to experiment with the amount of time to run the servos.

hugher256

dxw00d ,please explain this with a code.

dxw00d

Have you looked at the servo example sketches? Attach one of your servos, and try the 'Sweep' sketch. That should help you understand how the code works.

I was wrong earlier. To spin on the spot, both servos need to rotate in the same direction.


hugher256

This is my code. My servos are woking correctly, in opposite direction. But my pig sensor does not light up. I connected it to 5v port on Arduino. Any help will be really appreciated.

#include <Servo.h>

Servo ls;
Servo rs;
int val = 0;
int timecount = 0;
int ultrasoundValue = 0;
int signal =8;
int distance;
unsigned long pulseduration=0;


void setup(){
      pinMode(9,OUTPUT);
   pinMode(10,OUTPUT);
   ls.attach(9);
   rs.attach(10);
   pinMode(signal, OUTPUT);
Serial.begin(96500);
}
void measureDistance()
{
   // set pin as output so we can send a pulse
pinMode(signal, OUTPUT);
// set output to LOW
digitalWrite(signal, LOW);
delayMicroseconds(2);

// now send the 5uS pulse out to activate Ping)))
digitalWrite(signal, HIGH);
delayMicroseconds(5);
digitalWrite(signal, LOW);

// now we need to change the digital pin
// to input to read the incoming pulse
pinMode(signal, INPUT);
   val = digitalRead(signal);  // Append signal value to val
  while(val == LOW) {                   // Loop until pin reads a high value
    val = digitalRead(signal);
}

  while(val == HIGH) {                  // Loop until pin reads a high value
  val = digitalRead(signal);
  timecount = timecount +1;           // Count echo pulse time
}

ultrasoundValue = timecount;          // Append echo pulse time to ultrasoundValue

if(timecount > 0){
}


}
void loop()
{
// get the raw measurement data from Ping)))
measureDistance();

// divide the pulse length by half
pulseduration=pulseduration/2;

// now convert to centimetres. We're metric here people...
distance = int(pulseduration/29);

if( signal > 500){
   ls.write(0);
}

if( signal > 450){
   rs.write(0);
}

}

HazardsMind

#13
Apr 16, 2013, 08:50 pm Last Edit: Apr 16, 2013, 08:58 pm by HazardsMind Reason: 1
I have a few minor questions.

1) Why is your ping signal set as an output?
2) Why is this set the way it is. "Serial.begin(96500);"
3) How can "val = digitalRead(signal); " read anything if it is set as an output?
4) Why?
Quote
while(val == LOW) {                   // Loop until pin reads a high value
    val = digitalRead(signal);
}

5) This is doing nothing, if(timecount > 0){ }
6) Where is pulseduration?
7) This will give you problems,
Quote
if( signal > 500){
   ls.write(0);
}

if( signal > 450){
   rs.write(0);
}

8 ) Why is this not in [ code ] [/ code] tags?

Last question, your pig sensor lights up?
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PaulS

Quote
But my pig sensor does not light up.

Do you have a link to this pig sensor? I wonder if there is a cow sensor, too.

Go Up