Distance Sensors Aren't Working Together

Two distance sensors are operating, call them FrontSensor and StairSensor. The program should Serial.print(1) when FrontSensor detects an object that is between 3 cm and 70 cm away.

StairSensor is more complex. It detects the distance from the object, then subtracts the value of FrontSensor from this distance. This new value is called temp. If temp is within 4cm and 50 cm, the program should Serial.print(5).

SonarSenor is the function that calculates the distance of the object from the distance sensor, and stores it in a variable called “distance.”

The problem with the code below is that I don’t want to be sending 1’s when temp is sending 5’s. When temp is within the given range, I want ONLY 5’s to be sent.

I.e. FrontSensor is working so 1’s are being printed, but then temp suddenly comes into range; now the 1’s should stop and only the 5’s should be printed.

const int lower_limit=3;
const int upper_limit=70;

long duration, distance, FrontSensor, LeftSensor, StairSensor,temp;
SonarSensor(trigPin1, echoPin1);
FrontSensor = distance;
SonarSensor(trigPin3, echoPin3);
StairSensor = distance;

temp=StairSensor-FrontSensor;


if (temp>4 && temp<50)
    {
        mySerial.print(5);
        delay(10);
    }

    else{
    if (FrontSensor>lower_limit && FrontSensor<upper_limit)
      {
        mySerial.print(1);
        delay(10);
      }
    }

Hi,
I think we need your complete code.

Tom.. :slight_smile:

I tried to include only the necessary parts, but if it helps, here you go:

I’m using xBee’s to make 2 arduinos communicate. This is the TX. The RX simply receives a value and runs a switch statement that plays a recording based on which number is received.

The overall project is to play messages based on which distance sensor detects an object inb it’s range.

//left sensor does staircase detection
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11); // RX, TX

#define trigPin1 2 // for front sensor
#define echoPin1 3
#define trigPin2 4 //for Left Sensor
#define echoPin2 5
#define trigPin3 6 //for staircase detection
#define echoPin3 7
#define switch_mode 8
#define vibrator1 A0 //front sensor
#define vibrator2 A1 //left sensor
#define vibrator3 A2 //relative staircase deteciton

int toggle_switch;

const int smallvibration=50;
const int bigvibration=80;

const int lower_limit=3;
const int middle_limit=25;
const int upper_limit=70;

long duration, distance, FrontSensor, LeftSensor, StairSensor,temp;
void setup()
{
  mySerial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(vibrator1, OUTPUT);
  pinMode(vibrator2, OUTPUT);
  pinMode(vibrator3, OUTPUT);
  pinMode(switch_mode,INPUT_PULLUP);
}

void loop() {

toggle_switch=1; // for testing purposes
//toggle_switch = digitalRead(switch_mode);
  
SonarSensor(trigPin1, echoPin1);
FrontSensor = distance;

if(toggle_switch == LOW)
  {
    if(FrontSensor>lower_limit && FrontSensor<middle_limit)
    {analogWrite(vibrator1,bigvibration);}
    
    else if (FrontSensor>middle_limit && FrontSensor<upper_limit)
    {analogWrite(vibrator1,smallvibration);}
    
    else
    {analogWrite(vibrator1,0);}
  }

else if (toggle_switch == HIGH)
  {
    if (FrontSensor>lower_limit && FrontSensor<upper_limit)
      {
        mySerial.print(1);
        delay(10);
      }
  }
  
SonarSensor(trigPin2, echoPin2);
LeftSensor = distance;

if(toggle_switch == LOW)
  {
    if(LeftSensor>lower_limit && LeftSensor<middle_limit)
    {analogWrite(vibrator2,bigvibration);}
    
    else if (LeftSensor>=middle_limit && LeftSensor<upper_limit)
    {analogWrite(vibrator2,smallvibration);}
    
    else
    {analogWrite(vibrator2,0);}
  }

else if (toggle_switch == HIGH)
  {
    if (LeftSensor>lower_limit && LeftSensor<upper_limit)
      {
        mySerial.print(4); // says "left"
        delay(10);
      }
  }
  
SonarSensor(trigPin3, echoPin3);
StairSensor = distance;

temp=StairSensor-FrontSensor;

if(toggle_switch == LOW)
  {
    if (temp>4 && temp<50)
    {analogWrite(vibrator3,smallvibration);}
    
    else
    {analogWrite(vibrator3,0);}
  }

else if (toggle_switch == HIGH)
{
  if (temp>4 && temp<50)
    {
        mySerial.print(5); //says "staircase up"
        delay(10);
    }
}

Serial.print(FrontSensor); //print distance
Serial.print("-");
Serial.print(LeftSensor);
Serial.print("-");
Serial.print(StairSensor);
}

void SonarSensor(int trigPin,int echoPin)  //calcualte distance
{analogWrite(trigPin, 0);
delayMicroseconds(2);
analogWrite(trigPin, 255); 
delayMicroseconds(10);
analogWrite(trigPin, 0);
duration = pulseIn(echoPin, HIGH); 
distance = (duration/2) / 29.1;
delay(50);}

FrontSensor, LeftSensor, and StairSensor are dumb names for variables that contain distances. What were you thinking?

SonarSensor() should return a value, rather than mess with global variables.

I.e. FrontSensor is working so 1's are being printed, but then temp suddenly comes into range; now the 1's should stop and only the 5's should be printed.

Based on the code you have written, I would NOT expect the 1's to stop.

You should read all three sensors, and then decide what to do based on the state of the toggle switch pin ONE TIME, not three times.

Thank you for the criticism, I'll take it in good faith.

It's obvious you haven't read the explanation I've given about this problem, because you've casually commented on everything EXCEPT the topic of this post.

PaulS:
Based on the code you have written, I would NOT expect the 1's to stop.

Yes, they don't stop, that's why I made this post.

You're obviously better at Arduino than I am, this problem is trivial to you. Could you please suggest something useful, to solve the problem outlined in my first message?

Could you please suggest something useful, to solve the problem outlined in my first message?

I already did. Read all three sensors. Have ONE if statement that cares about the state of the toggle_switch value, not three.

Giving variables meaningful names helps make sure that you do not use the values incorrectly.

Will this stop printing 1’s when the 5’s start printing?

For anyone who just visited this post, I’m only concerned with stopping the 1’s from being printed when the 5’s are printing.

You can ignore the ‘if’ statement that is printing 4’s; it is independent of others.

else if (toggle_switch == HIGH)
  {
    if (temp>4 && temp<50)
    {
        mySerial.print(5);
        delay(10);
    }
    
    else if (FrontSensor>lower_limit && FrontSensor<upper_limit)
      {
        mySerial.print(1); 
        delay(10);
      }


    if (LeftSensor>lower_limit && LeftSensor<upper_limit)
      {
        mySerial.print(4);
        delay(10);
      }
  }

Yes, that should work. The code prints nothing, 1, or 5 on any given pass.