So i want to detect items using us sensor and so i have used the function sonar sensor to determine the distance. But when checked on the serial monitor it always keeps showing 2335,2336,2337 etc. Only if i cover the sensor i get 5,6,9,etc. Can somebody tell me why?Thanks in Advance.
It's probably a problem with the code you didn't post, or the wiring you didn't post.
oh sorry i forgot here it is
#include <SoftwareSerial.h>
long duration2, distance2, BackSensor, FrontSensor;
char data = 0;
int sensor = 2;
int state1 = LOW;
int val = 0;
int enA = 9;
int enB = 10;
int motor1Pin1 = 22;
int motor1Pin2 = 23;
int motor2Pin1 = 24;
int motor2Pin2 = 25;
int state2;
int trigPin1 = 5;
int echoPin1 = 6;
int var1;
int myvar;
void Forward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, HIGH);
}
void Reverse() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
void Left() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void Right() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void StopWheel() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void Turn()
{
var1 = false;
Right();
Forward();
Left();
Forward();
Left();
Forward();
Right();
Forward();
var1 = true;
}
void setup()
{
pinMode(sensor, INPUT);
pinMode(motor1Pin1, OUTPUT), pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT), pinMode(motor2Pin2, OUTPUT);
pinMode(enA, OUTPUT), pinMode (enB, OUTPUT);
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
}
void SonarSensor(int trigPin, int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration2 = pulseIn(echoPin, HIGH);
distance2 = (duration2 / 2) / 29.1;
}
void loop() {
if(Serial.available())
{
char a=Serial.read();
}
SonarSensor(trigPin1, echoPin1);
FrontSensor = distance2;
Serial.println(distance2);
while (distance2 > 2||(a=='f') {
Forward();
}
if (distance2 == 2) {
StopWheel();
}
val = digitalRead(sensor);
if (val == HIGH)
{
Turn();
}
if (state1 == LOW) {
myvar = true;
}
if (myvar == true) {
Serial.println("Motion detected!");
state1 = HIGH;
}
else {
while (distance2 > 5) {
Forward();
}
}
Serial.println(distance2);
if (state1 == HIGH) {
Serial.println("Motion stopped!");
state1 = LOW; // update variable state1 to LOW
}
}
Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.
That code doesn't even compile.
but it does. and i have auto formatted it down below
#include <SoftwareSerial.h>
long duration2, distance2, BackSensor, FrontSensor;
char data = 0;
int sensor = 2;
int state1 = LOW;
int val = 0;
int enA = 9;
int enB = 10;
int motor1Pin1 = 22;
int motor1Pin2 = 23;
int motor2Pin1 = 24;
int motor2Pin2 = 25;
int state2;
int trigPin1 = 5;
int echoPin1 = 6;
int var1;
int myvar;
void Forward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, HIGH);
}
void Reverse() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
void Left() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void Right() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void StopWheel() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void Turn()
{
var1 = false;
Right();
Forward();
Left();
Forward();
Left();
Forward();
Right();
Forward();
var1 = true;
}
void setup()
{
pinMode(sensor, INPUT);
pinMode(motor1Pin1, OUTPUT), pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT), pinMode(motor2Pin2, OUTPUT);
pinMode(enA, OUTPUT), pinMode (enB, OUTPUT);
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
}
void SonarSensor(int trigPin, int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration2 = pulseIn(echoPin, HIGH);
distance2 = (duration2 / 2) / 29.1;
}
void loop() {
if (Serial.available())
{
char a = Serial.read();
}
SonarSensor(trigPin1, echoPin1);
FrontSensor = distance2;
Serial.println(distance2);
while (distance2 > 2 || (a == 'f') {
Forward();
}
if (distance2 == 2) {
StopWheel();
}
val = digitalRead(sensor);
if (val == HIGH)
{
Turn();
}
if (state1 == LOW) {
myvar = true;
}
if (myvar == true) {
Serial.println("Motion detected!");
state1 = HIGH;
}
else {
while (distance2 > 5) {
Forward();
}
}
Serial.println(distance2);
if (state1 == HIGH) {
Serial.println("Motion stopped!");
state1 = LOW; // update variable state1 to LOW
}
}
But it doesn't. Try again and you'll see that it definitely doesn't compile. Now that it's formatted, a quick look at the indentation of the code will make it clear what the problem is.
while (distance2 > 5) {
Forward();
}
How can this loop ever end? distance2 is never updated inside this loop.
Faruthecoder:
but it does. and i have auto formatted it down belowwhile (distance2 > 2 || (a == 'f') {
Forward();
}
That 'if' will not compile. It is missing a ')'.
When you fix that, the same line gets "'a' was not declared in this scope" because 'a' exists only in the 'if' statement:
if (Serial.available())
{
char a = Serial.read();
}
If you fix that by changing it to:
char a;
if (Serial.available())
{
a = Serial.read();
}
At least THEN it will compile. Then you have the problem that 'a' is filled with garbage each time the function runs and unless a character has arrived, a won't be set: "warning: 'a' may be used uninitialized in this function".
And then there is:
while (distance2 > 5)
{
Forward();
}
Since the Forward() function doesn't change 'distance2', this will be an infinite loop if 'distance2' is greater than 5.
So i had already started a thread on this and got a lot of replys. But i realised i had not posted a lot of things like code and schematic etc. So i am re-posting everything and i hope you all will please help me.
#include <SoftwareSerial.h>
long duration2, distance2, BackSensor, FrontSensor;
char data = 0;
int sensor = 2;
int state1 = LOW;
int val = 0;
int enA = 9;
int enB = 10;
int motor1Pin1 = 22;
int motor1Pin2 = 23;
int motor2Pin1 = 24;
int motor2Pin2 = 25;
int state2;
int trigPin1 = 5;
int echoPin1 = 6;
int var1;
int myvar;
void Forward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, HIGH);
}
void Reverse() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
void Left() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void Right() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void StopWheel() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void Turn()
{
var1 = false;
Right();
Forward();
Left();
Forward();
Left();
Forward();
Right();
Forward();
var1 = true;
}
void setup()
{
pinMode(sensor, INPUT);
pinMode(motor1Pin1, OUTPUT), pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT), pinMode(motor2Pin2, OUTPUT);
pinMode(enA, OUTPUT), pinMode (enB, OUTPUT);
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
}
void SonarSensor(int trigPin, int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration2 = pulseIn(echoPin, HIGH);
distance2 = (duration2 / 2) / 29.1;
}
void loop() {
SonarSensor(trigPin1, echoPin1);
FrontSensor = distance2;
Serial.println(distance2);
while (distance2 > 2) {
Forward();
}
if (distance2 == 2) {
StopWheel();
}
val = digitalRead(sensor);
if (val == HIGH)
{
Turn();
}
}
@Faruthecoder, do not cross-post. Threads merged.
Two simple questions:-
-
What is the function of enA and enB?
-
What does your sketch do with enA and enB?
Ian
Thanks for the code. But this is not an adequate description of the problem...
please help me.
What does your code currently do? What do you want it to do different?