SR04
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 ");
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!
#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 ");
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
Second, when you say it doesn't work what is the symptom?
well all it does is drive forward turning right a little bit slowly as it goes forward (might be my cheap servos).
In what way isn't it working? Is any value displayed?
What are expecting that isn't happening?
I notice you have the line:
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.
What does it display? What should it display?
Try changing the line I posted above to:
It might not be as accurate as you'd like but this takes care of the interger and floating point mixture.
Duane, C++ usually automatically converts from float to int or vice versa depending upon the destination type and the other argument.
not really a Parallax-based discussion, this
You ought make the change Martin suggested.