Shop OBEX P1 Docs P2 Docs Learn Events
SR04 — Parallax Forums

SR04

robotboyrobotboy Posts: 30
edited 2012-11-14 13:43 in Robotics
Hi guys, i got 5 Sr04 sensors and cant seem to get them working with the code i wrote. Here is the code:


#include <LiquidCrystal.h>
#include <Servo.h>
#define trigPin 24
#define echoPin 25
#define Reye 22
#define Leye 23
Servo Pan;
Servo Tilt;
Servo Right;
Servo Left;
LiquidCrystal lcd(35, 36, 37, 38, 39, 40);

void setup() {
pinMode(Reye, OUTPUT);
pinMode(Leye, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
Pan.attach(31);
Tilt.attach(32);
Right.attach(33);
Left.attach(34);

}
void loop() {
digitalWrite(Reye, HIGH);
digitalWrite(Leye, HIGH); // turn the LED on (HIGH is the voltage level)
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
Serial.println(voltage);
lcd.setCursor(0, 1);
int duration, distance;
digitalWrite(trigPin, HIGH);
digitalWrite(Reye, HIGH);
digitalWrite(Leye, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;



if (distance >= 200 || distance <= 0){
lcd.print("Wobot :D");
Right.write(180);
Left.write(180);
}
if (distance <= 35){
lcd.print("Wow");
Right.write(0);
Left.write(0);

}

else {
lcd.print(distance);
lcd.print(" cm ");
Right.write(180);
Left.write(0);

}
delay(500);
}



If you see whats wrong that would be great!

Comments

  • Martin_HMartin_H Posts: 4,051
    edited 2012-11-13 07:26
    Hi, first code is easier to read if you use these instructions:

    attachment.php?attachmentid=78421&d=1297987572

    Second, when you say it doesn't work what is the symptom?
  • robotboyrobotboy Posts: 30
    edited 2012-11-13 09:20
    #include <LiquidCrystal.h>
    #include <Servo.h> 
    #define trigPin 24
    #define echoPin 25
    #define Reye 22
    #define Leye 23
    Servo Pan;
    Servo Tilt;
    Servo Right;
    Servo Left;
    LiquidCrystal lcd(35, 36, 37, 38, 39, 40);
    
    void setup() {                
     pinMode(Reye, OUTPUT);
    pinMode(Leye, OUTPUT);
     pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT); 
      Serial.begin(9600); 
      Pan.attach(31);
      Tilt.attach(32);
      Right.attach(33);
      Left.attach(34);
       
    }
    void loop() {
      digitalWrite(Reye, HIGH);
      digitalWrite(Leye, HIGH);  // turn the LED on (HIGH is the voltage level)
      int sensorValue = analogRead(A0);
      float voltage = sensorValue * (5.0 / 1023.0);
      Serial.println(voltage);
       lcd.setCursor(0, 1);
       int duration, distance;
        digitalWrite(trigPin, HIGH);
      digitalWrite(Reye, HIGH);
      digitalWrite(Leye, HIGH);
      delayMicroseconds(1000);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration/2) / 29.1;
    
    
       
     if (distance >= 200 || distance <= 0){
        lcd.print("Wobot         :D");
        Right.write(180); 
        Left.write(180); 
        }
     if (distance <= 35){
        lcd.print("Wow");
        Right.write(0); 
        Left.write(0); 
        
        }
     
      else {
        lcd.print(distance);
        lcd.print(" cm ");
        Right.write(180); 
        Left.write(0); 
       
      }
      delay(500);
    }
    
    

    well all it does is drive forward turning right a little bit slowly as it goes forward (might be my cheap servos).
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-13 09:34
    robotboy wrote: »
    i got 5 Sr04 sensors and cant seem to get them working with the code i wrote.

    In what way isn't it working? Is any value displayed?

    What are expecting that isn't happening?

    I notice you have the line:
    distance = (duration/2) / 29.1;
    

    Where both distance and duration are integers. I don't use Arduino much but I don't think you're allowed to divide an integer by a floating point number.
  • robotboyrobotboy Posts: 30
    edited 2012-11-13 09:47
    Duane Degn wrote: »
    In what way isn't it working? Is any value displayed?

    What are expecting that isn't happening?

    I notice you have the line:
    distance = (duration/2) / 29.1;
    

    Where both distance and duration are integers. I don't use Arduino much but I don't think you're allowed to divide an integer by a floating point number.
    well it doesn't seem to display like it should and when something gets to 35 cm away it would turn.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-13 10:08
    robotboy wrote: »
    well it doesn't seem to display like it should.

    What does it display? What should it display?

    Try changing the line I posted above to:
    distance = (duration/2) / 29;
    

    It might not be as accurate as you'd like but this takes care of the interger and floating point mixture.
  • Martin_HMartin_H Posts: 4,051
    edited 2012-11-13 11:32
    Your delayMicroseconds has an argument of 1000 while the sample code I found online has an argument of 10.
      // Start Ranging
      digitalWrite(TRIGPIN, LOW);
      delayMicroseconds(2);
      digitalWrite(TRIGPIN, HIGH);
      delayMicroseconds(10);
      digitalWrite(TRIGPIN, LOW);
      // Compute distance
      float distance = pulseIn(ECHOPIN, HIGH);
      distance= distance/58;
      Serial.println(distance);
      delay(200);
    

    Duane, C++ usually automatically converts from float to int or vice versa depending upon the destination type and the other argument.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-11-13 18:27
    http://arduino.cc/forum/index.php/topic,106043.0.html

    not really a Parallax-based discussion, this
  • robotboyrobotboy Posts: 30
    edited 2012-11-13 18:41
    Problem solved.... bad servo plug :p
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-11-13 19:12
    robotboy wrote: »
    Problem solved.... bad servo plug :p

    You ought make the change Martin suggested.
  • robotboyrobotboy Posts: 30
    edited 2012-11-14 09:01
    Ya.... wobot is running live using the SR04 http://www.youtube.com/watch?v=O8wR5FWu5vY
  • ercoerco Posts: 20,255
    edited 2012-11-14 09:52
    Go wobot! Nicely done, very Wall-E-esque.
  • robotboyrobotboy Posts: 30
    edited 2012-11-14 13:43
    Thank you!
Sign In or Register to comment.