Pages: [1] 2 3   Go Down
Author Topic: Ping Robo code error  (Read 2835 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My code for Ping robot.
There sumthing wrong in my code i cannot fix.
Please see LeftServo.refresh();




Code:
#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.

« Last Edit: April 27, 2012, 12:10:49 pm by AWOL » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

{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.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

continuous-rotation servos
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

dxw00d ,please explain this with a code.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank dxw00d.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
 }

}
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 86
Posts: 3445
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: April 16, 2013, 01:58:02 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2 3   Go Up
Jump to: