Go Down

Topic: moving sculpture controled by 2 sonic sensors arduino not reading both (Read 1 time) previous topic - next topic

DaztheGuardian

I have an arduino uno moving 2 sensors to random degrees that stop moving when something comes in to the HC-SR04 Ultrasonic Sensor distance less than 50 centimeters. The problem is that both sonic sensors show correct distances but only the second distance measurement is making the the servos stop. It is like the arduino only checks the if statement to the second sonic sensor. I have checked to sensor individually and all connections so i think it is the code. here is the code.
Code: [Select]
#define trigPin1 12
#define echoPin1 13
#define trigPin2 2
#define echoPin2 4
#include <Servo.h>

long randNumber1;
long randNumber2;
int servoPin1=3;
int servoPin2=5;
Servo Servo1;
Servo Servo2;
int long duration1, duration2, distance, pos=0,i;
void setup(){
  Serial.begin(9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  Servo1.attach(servoPin1);
  Servo2.attach(servoPin2);
  randomSeed(analogRead(0));
}
void loop(){

 
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance = (duration1/2) / 29.1;
   Serial.print(distance);
   Serial.println("distance1 cm");
    digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
 distance = (duration2/2) / 29.1;
   Serial.print(distance);
   Serial.println("distance2 cm");
  randNumber1 = random(5, 160);
  Serial.println(randNumber1);
  randNumber2 = random(5, 160);
  Serial.println(randNumber2);
 if(distance <50)
  {
Servo1.write(0);
delayMicroseconds(10);
Servo2.write(0);
delayMicroseconds(10);
Serial.println(0);
 delay(750);
 }

else
{
Servo1.write(randNumber1);
delayMicroseconds(10);
Servo2.write(randNumber2);
delay(750);
}
 


}

claudegu

Hi,

You should use distance1 and distance2 variables insteed of only "distance" because the second part erase the first one.

Then you just have to ask if distance1 or distance2 is below 50cm

Stone age was not finished due to a lack of stones

claudegu

Stone age was not finished due to a lack of stones

DaztheGuardian

i was out of town. Im going to try it in about an hour. When i get to the studio. 

DaztheGuardian

if i put the distance 1 and distance 2 in it does not  move the servos. It acts like the measurements are less than 50 when they are not i have checked the sensors and they are working  so i dont know what i did
Code: [Select]
#define trigPin1 12
#define echoPin1 13
#define trigPin2 2
#define echoPin2 4
#include <Servo.h>

long randNumber1;
long randNumber2;
int servoPin1=3;
int servoPin2=5;
Servo Servo1;
Servo Servo2;

void setup(){
  Serial.begin(9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  Servo1.attach(servoPin1);
  Servo2.attach(servoPin2);
  randomSeed(analogRead(0));
}
void loop(){

 int long duration1, duration2, distance1, distance2, pos=0,i;
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;
   Serial.print(distance1);
   Serial.println("distance1 cm");
    digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2/2) / 29.1;
   Serial.print(distance2);
   Serial.println("distance2 cm");
  randNumber1 = random(5, 175);
  Serial.println(randNumber1);
  randNumber2 = random(5, 175);
  Serial.println(randNumber2);
if(distance1 or distance2 <50)
  {
Servo1.write(0);
Servo2.write(0);
Serial.println(0);
 delay(1000);
 }

else
{
Servo1.write(randNumber1);
Servo2.write(randNumber2);
delay(1000);
}
 


}



claudegu

In Arduino language, OR is   ||

:)

so you should write :  


if(distance1 <50 || distance2 <50)


Stone age was not finished due to a lack of stones

DaztheGuardian

You have saved my sanity and my art piece.  You don't know how close i was to destroying it. Thank you

claudegu

 :smiley-grin:

Have a nice day and I wish you a lot of success with your art piece.
Stone age was not finished due to a lack of stones

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy