Hello!
I have a hc-sr04 sensor mounted on a rotating platform. at first all worked just fine, but at some point I started getting completely wrong readings from the sensor, like:
At -10 degrees nearest object within: 377 cm
At -11 degrees Radar is out of range
At -12 degrees nearest object within: 373 cm
At -13 degrees Radar is out of range
Alert!! at -13 degrees object within 63 cm. Lunching!
At -14 degrees nearest object within: 397 cm
At -15 degrees Radar is out of range
At -16 degrees Radar is out of range
At -17 degrees Radar is out of range
Alert!! at -17 degrees object within 57 cm. Lunching!
At -16 degrees Radar is out of range
At -15 degrees nearest object within: 388 cm
At -14 degrees Radar is out of range
At -13 degrees Radar is out of range
while scanning a plain wall within 375+- cm away.
The platform rotates using a stepper motor, and if the ultrasonic sensor detects an object under 160 cm, a servo motor rotates and releases a rubber band.
because initially everything worked ok, and suddenly I started getting wrong readings, Im not sure what went wrong. (separately, the ultrasonic sensor has no problems).
Thank you very much!
#define ECHO 13 // Pin received echo pulse
#define TRIGGER 10 // Pin sends shot pulse
float distance; // the distance in cms
unsigned long previousMicros = 0; // will sotre last time TRIGGER was updated
long onTime = 10; //microseconds of on-time
long offTime = 2; //microseconds of off-time
unsigned long previousMillis = 0; //will store last time viewTime was updated
int triggerState = LOW; // triggerState used to set it up
float duration;
unsigned long prevPrint = 0; // used to determine wheather to print
#include <AccelStepper.h>
float loc = 0;
float deg = 0;
int lastDeg = -1; //used to make sure not to print twice for same deg
int wires = 4; // Symbolic names for number of pins, here 4 pins
AccelStepper stepper(wires,4,6,5,7);
float degMax = 9; //deg of range each side
float locMax = degMax*(4076/360); // 4076 steps. each deg 11.32 steps
String way = "clkWise";
#include <Servo.h>
int servoPin = 9;
int servoPos = 0;
Servo trigger;
void setup()
{
Serial.begin(9600);
pinMode(ECHO, INPUT);
pinMode(TRIGGER, OUTPUT);
stepper.setMaxSpeed(1.0);
stepper.setSpeed(1.0);
stepper.setAcceleration(5.0);
stepper.setCurrentPosition(0);
trigger.attach(servoPin);
trigger.write(0);
}
void loop()
{
loc = stepper.currentPosition();
deg = loc*0.0883219;
if(way.equals("clkWise") && loc < locMax) {
stepper.move(locMax*2); //clockwise
stepper.run();
}
else if(way.equals("clkWise")&& loc >= locMax){
way = "cntClkWise";
stepper.move(-locMax*2); //counterclockwise
stepper.run();
}
else if(way.equals("cntClkWise") && loc > -locMax) {
stepper.move(-locMax*2); //counterclockwise
stepper.run();
}
else if(way.equals("cntClkWise")&& loc <= -locMax){
way = "clkWise";
stepper.move(locMax*2); //clockwise
stepper.run();
}
// check to see if it's time to change the state of the TRIGGER
unsigned long currentMicros = micros();
if((triggerState == LOW) && (currentMicros - previousMicros >= offTime)) {
triggerState = HIGH; // turn it on
previousMicros = currentMicros; // remember the time
digitalWrite(TRIGGER, triggerState); // update the actual trigger
}
else if((triggerState == HIGH) && (currentMicros - previousMicros >= onTime)) {
triggerState = LOW; // turn it off
previousMicros = currentMicros; // remember the time
digitalWrite(TRIGGER, triggerState); // update the actual trigger
}
duration = pulseIn(ECHO,HIGH);
distance = ((duration*0.034)/2);
if (distance <= 160){
Serial.print("Alert!! at ");
Serial.print(int(deg));
Serial.print(" degrees object within ");
Serial.print(int(distance));
Serial.println(" cm. Lunching!");
Lunching();
}
else if ((currentMicros - prevPrint >= pow(10,6)) && (int(deg) != lastDeg)) { //print only once a second and once for each deg, as long as radar is clear
if ((distance >= 400 || distance <= 2)){
Serial.print("At ");
Serial.print(int(deg));
Serial.println(" degrees Radar is out of range");
}
else {
Serial.print("At ");
Serial.print(int(deg));
Serial.print(" degrees nearest object within: ");
Serial.print(int(distance));
Serial.println(" cm");
}
prevPrint = currentMicros;
lastDeg = int(deg); //update lastDeg
}
}
void Lunching() {
stepper.stop();
servoPos = 70;
trigger.write(servoPos);
delay(1000);
trigger.write(0);
//Waiting for reload to resume scanning
Serial.println("Rubbr band successfully lunched. resume scanning?");
while(Serial.available() == 0) {
}
Serial.println("Resuming");
}