1

I am currently working on an alarm system using multiple sensors and one arduino board and I am getting readings from 2 of the sensors but one gives a constant zero and a constant buzzer sound even without sensing the distance...I have followed the schematics of the original project but nothing works. Here is the code


int ledred1 = 2;
int ledgreen1 = 3;
int ledorange1 = 4;
int ledred2 = 5;
int ledgreen2 = 6;
int ledorange2 = 7;
int ledred3 = 8;
int ledgreen3 = 9;
int ledorange3 = 10;

int sound = 250;

int trigPin1 = 11;
int echoPin1 = 12;

int trigPin2 = 13;
int echoPin2 = 30;

int trigPin3 = 31;
int echoPin3 = 32;

#define buzzer1 33
#define buzzer2 34
#define buzzer3 35


void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
 
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
 
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

 
  pinMode(ledred1, OUTPUT);
  pinMode(ledred2, OUTPUT);
  pinMode(ledorange1, OUTPUT);
  pinMode(ledgreen1, OUTPUT);
  pinMode(ledgreen2, OUTPUT);
  pinMode(ledred3, OUTPUT);
  pinMode(ledgreen3, OUTPUT);
  pinMode(ledorange2, OUTPUT);
  pinMode(ledorange3, OUTPUT);

  
}
void firstsensor(){ // This function is for first sensor. 
  int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

      Serial.print("1st Sensor: ");
      Serial.print(distance1); 
      Serial.print("cm    ");

  if (distance1 <=200 && distance1>=100) {  // Change the number for long or short distances.
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,HIGH);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
    
    sound=250;

  } else {
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
     }   


  if (distance1 <100&& distance1>=50) {  // Change the number for long or short distances.
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,HIGH);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
    
    sound=275;

  } else {
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
      }   
        if (distance1 <50&& distance1>=0) {  // Change the number for long or short distances.
    digitalWrite (ledred1, HIGH);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
    
    sound=300;

  } else {
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
      } 
      if(distance1>200 || distance1<0){
    Serial.println("Out of Range");
    noTone(buzzer1);
  }
  else{
    Serial.print(distance1);
    Serial.println("cm");
    tone(buzzer1,sound);

  }  
}
void secondsensor(){ // This function is for second sensor.
    int duration2, distance2;
    digitalWrite (trigPin2, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin2, LOW);
    duration2 = pulseIn (echoPin2, HIGH);
    distance2 = (duration2/2) / 29.1;
 
      Serial.print("2nd Sensor: ");
      Serial.print(distance2); 
      Serial.print("cm    ");
  
     if (distance2 <=200&& distance2>=100) {  // Change the number for long or short distances.
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, HIGH);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
    
    sound=250;

  } else {
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
     }   


  if (distance2 <100&& distance2>=50) {  // Change the number for long or short distances.
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,HIGH);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
    
    sound=275;

  } else {
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
      }   
        if (distance2 <50&& distance2>=0) {  // Change the number for long or short distances.
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, HIGH);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
    
    sound=300;

  } else {
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
      } 
      if(distance2>200 || distance2<0){
    Serial.println("Out of Range");
    noTone(buzzer2);
  }
  else{
    Serial.print(distance2);
    Serial.println("cm");
    tone(buzzer2,sound);

  }  
}
void thirdsensor(){ // This function is for third sensor.
    int duration3, distance3;
    digitalWrite (trigPin3, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin3, LOW);
    duration3 = pulseIn (echoPin3, HIGH);
    distance3 = (duration3/2) / 29.1;

      Serial.print("3rd Sensor: ");  
      Serial.print(distance3); 
      Serial.print("cm");
  
     if (distance3 <=200&& distance3>=100) {  // Change the number for long or short distances.
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, HIGH);
    
    sound=250;

  } else {
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
     }   


  if (distance3 <100&& distance3>=50) {  // Change the number for long or short distances.
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,HIGH);
    digitalWrite (ledgreen3, LOW);
    
    sound=275;

  } else {
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
      }   
        if (distance3 <50&& distance3>=0) {  // Change the number for long or short distances.
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
    
    sound=300;

  } else {
    digitalWrite (ledred1, LOW);
    digitalWrite (ledgreen1,LOW);
    digitalWrite (ledorange1,LOW);
    digitalWrite (ledred2, LOW);
    digitalWrite (ledorange2,LOW);
    digitalWrite (ledgreen2, LOW);
    digitalWrite (ledred3, LOW);
    digitalWrite (ledorange3,LOW);
    digitalWrite (ledgreen3, LOW);
      } 
      if(distance3>200 || distance3<0){
    Serial.println("Out of Range");
    noTone(buzzer3);
  }
  else{
    Serial.print(distance3);
    Serial.println("cm");
    tone(buzzer3,sound);

  }  
}

void loop() {
Serial.println("\n");
firstsensor();
delay(100);
secondsensor();
delay(100);
thirdsensor();
delay(100);
}

This is what I get in the serial monitorenter image description here This is the snapshot of the pin configurationenter image description here

6
  • Did you try exchanging the first module with one of the others? Tripple-check all your connections? Test it out with a simple example sketch all on its own? – Mat Nov 23 '20 at 12:10
  • I would suggest that you want to really simplify your program by making a single function that can take parameters to work with different pin combinations rather than duplicating code like that. – Majenko Nov 23 '20 at 13:46
  • please review your code line by line and learn what each line does .... it appears that you have not thought about program flow at all .... i am saying this because your sensor1 code manipulates led2 and led3 ... same with sensor2 and sensor3 ... also the distance checking code turns off leds that were previously turned on .... you cannot just copy and paste code without being aware of what it does – jsotola Nov 23 '20 at 19:09
  • @Mat I have rebuilt the connections and tested using a simple sketch..It works.. I guess the problem is with the code – Wayne 254 Nov 24 '20 at 5:35
  • @Majenko I am currently trying to simplify the code – Wayne 254 Nov 24 '20 at 5:35
1

This is NOT an answer to the question, but I did some (major) refactoring which make it much more clear what is happening.

I think you made some mistake within the (refactored) method Sensor::UpdateState.

File <.ino>:

#include "Sensors.h"

Sensors _sensors;

void setup() {
  Serial.begin (9600);
  _sensors.Init();  
}


void loop()
{
  _sensors.Process();
}

File Sensors.h:

#ifndef SENSORS_H
#define SENSORS_H

#include "Arduino.h"
#include "Sensor.h"

class Sensors
{
  public:
    Sensors();

    void Init();
    void Process();

  private:
    static const int NR_OF_SENSORS = 3;

    Sensor _sensors[NR_OF_SENSORS];
};

#endif // SENSORS_H

File Sensors.cpp:

#include "Sensors.h"

Sensors::Sensors()
:
  //              index trigPin echoPin redLed orangeLed greenLed buzzer)
  _sensors( { Sensor(1 ,  11   ,  12   ,  2       3     ,   4    ,  33),
              Sensor(2 ,  13   ,  30   ,  5       6     ,   7    ,  34),
              Sensor(3 ,  31   ,  32   ,  8       9     ,   10   ,  35) })
{
}

void Sensors::Init()
{
  for (uint8_t index = 0; index < NR_OF_SENSORS; index++)
  {
    _sensors[index].Init();
  }
}


void Sensors::Process()
{
  for (uint8_t index = 0; index < NR_OF_SENSORS; index++)
  {
    _sensors[index].Process();
  }
}

File Sensor.h:

#ifndef SENSOR_H
#define SENSOR_H

#include "Arduino.h"

class Sensor
{
  public:
    enum EState 
    {
      LOW_OUT_OF_RANGE, 
      WAY_TOO_CLOSE,
      TOO_CLOSE, 
      OK, 
      HIGH_OUT_OF_RANGE
    };
    
    Sensor(uint8_t index, uint8_t trigPin, uint8_t echoPin, uint8_t redLed, uint8_t orangeLed, uint8_t greenLed, uint8_t buzzer);

    uint8_t Index    (void) { return _index;     };
    uint8_t TrigPin  (void) { return _trigPin;   };
    uint8_t EchoPin  (void) { return _echoPin;   };
    uint8_t RedLed   (void) { return _redLed;    };
    uint8_t OrangeLed(void) { return _orangeLed; };
    uint8_t GreenLed (void) { return _greenLed;  };
    uint8_t Buzzer   (void) { return _buzzer;    };
     
    void Init();
    void Process();

  private:
    uint8_t _index;
    uint8_t _trigPin;
    uint8_t _echoPin;
    uint8_t _redLed;
    uint8_t _orangeLed;
    uint8_t _greenLed;
    uint8_t _buzzer;
    EState _state;
    uint8_t _distance;
    
    void Measure();
    void UpdateState();
    void ProcessState();
    void Show(uint16_t sound, uint8_t led);
    void PrintDistance(void);
};

#endif // SENSOR_H

File Sensor.cpp:

#include "Sensor.h"

Sensor::Sensor(uint8_t index, uint8_t trigPin, uint8_t echoPin, uint8_t redLed, uint8_t orangeLed, uint8_t greenLed, uint8_t buzzer)
:
  _index(index),
  _trigPin(trigPin),
  _echoPin(echoPin),
  _redLed(redLed),
  _orangeLed(orangeLed),
  _greenLed(greenLed),
  _buzzer(buzzer),
  _state(OK),
  _distance(0)
{
}


void Sensor::Init()
{
  pinMode(TrigPin()  , OUTPUT);
  pinMode(EchoPin()  , INPUT);
  pinMode(RedLed()   , OUTPUT);
  pinMode(OrangeLed(), OUTPUT);
  pinMode(GreenLed() , OUTPUT);
}


void Sensor::Process()
{
  Measure();
  UpdateState();
  ProcessState();
  PrintDistance();
  delay(100);
}


void Sensor::Measure()
{
  digitalWrite(TrigPin(), HIGH);
  delayMicroseconds (10);
  digitalWrite(TrigPin(), LOW);
  uint16_t duration = pulseIn (EchoPin(), HIGH);
  _distance = (duration / 2) / 29.1;
}


void Sensor::UpdateState()
{
  if (_distance < 0)
  {
    _state = LOW_OUT_OF_RANGE;
  }
  else if (_distance <= 50)
  {
    _state = WAY_TOO_CLOSE;
  }
  else if (_distance <= 100)
  {
    _state = TOO_CLOSE;
  }
  else if (_distance <= 200)
  {
    _state = OK;
  }
  else 
  {
    _state = HIGH_OUT_OF_RANGE;
  }
}


void Sensor::ProcessState()
{
  digitalWrite(RedLed(), LOW);
  digitalWrite(OrangeLed(), LOW);
  digitalWrite(GreenLed(), LOW);
  
  switch (_state)
  {
  case LOW_OUT_OF_RANGE:
    noTone(Buzzer());  
    break;
    
  case WAY_TOO_CLOSE:
    Show(300, RedLed());
    break;
    
  case TOO_CLOSE:
    Show(275, OrangeLed());
    break;
    
  case OK:
    Show(250, GreenLed());
    break;
  
  case HIGH_OUT_OF_RANGE:
    noTone(Buzzer());    break;
    break;
  
  default:
    // Error
    break;
  }
}


void Sensor::Show(uint16_t sound, uint8_t led)
{
  tone(Buzzer(), sound);
  digitalWrite(led, HIGH);
}


void Sensor::PrintDistance()
{
  Serial.print("Sensor ");
  Serial.print(Index());
  Serial.print(": ");
  Serial.print(_distance); 
  Serial.print("cm    ");
  
  if ((_state == LOW_OUT_OF_RANGE) || (_state == HIGH_OUT_OF_RANGE))
  {
    Serial.print(" (Out of range)");
  }
  
  Serial.println("");
}
1
  • Thank you for this..i am going through it to see how I can model the code to suit the one I have written – Wayne 254 Nov 24 '20 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.