Ultrasonic sensor coding help

Hi made a project on IR SENSOR
Now thinking to replace the same project with ultrasonic sensor.
is thier any way to retain this project and replace it with ultrasonic sensor?

#include <ShiftRegister74HC595.h>
// create a global shift register object
// parameters: <number of shift registers> (data pin, clock pin, latch pin)
ShiftRegister74HC595<2> sr(14, 16, 15);

#define sensorPin1 5        //ir sensor 1
#define sensorPin2 7        //ir sensor 2
#define sensorPin3 9        //ir sensor 3
#define sensorPin4 11       //ir sensor 4


#define TOUPON 1
#define TOUPOFF 2
#define TODOWNON 3
#define TODOWNOFF 4
#define NONE -1
#define button 12             //Push button on D12
int state = 0;                //integer to hold current state
int old = 0;                  //integer to hold last state
int buttonpoll = 0;           //integer to hold button state
int anim_state = NONE;
int last_anim_state = -1;
unsigned long last_millis = 0;
unsigned long last_count = 0;
int sensorState1 = 0;
int sensorState2 = 0;
int sensorState3 = 0;
int sensorState4 = 0;
int count=0;
int counter =0;
int time1=200;


void setup()
{
  Serial.begin(9600);
  Serial.println("FUN Started");
  Serial.print(count);
  pinMode (sensorPin1,INPUT);
  pinMode (sensorPin2, INPUT);
  pinMode (sensorPin3,INPUT);
  pinMode (sensorPin4,INPUT);
  pinMode(button, INPUT); 
  
 
}

void loop(){
  peopleCount();
  animate_state();
  buttonRead();
  switchDecision();
}

void buttonRead() 
 {//debouncing routine to read button
  buttonpoll = digitalRead(button);      //poll the state of button
  if (buttonpoll == 1) {                 //check if it has been pressed
    delay(50);                           //wait 50ms
    buttonpoll = digitalRead(button);    //poll button again
    if (buttonpoll == 0) {               //if it is 0 considered one press
      state = old + 1;    
    }
  }
   else {                                //if button has not been pressed
    delay(100);                         // wait 100ms
   }
 }


void peopleCount(){
 
 
  sensorState1 = digitalRead(sensorPin1);
  sensorState2 = digitalRead(sensorPin2);
  sensorState3 = digitalRead(sensorPin3);
  sensorState4 = digitalRead(sensorPin4);
  int lower = digitalRead(sensorPin1);
  int upper = digitalRead(sensorPin4);
 
  
  if (sensorState1 == LOW && (millis() - last_count > timex)) { //counts people IN
    count++;
    Serial.println(count);
    last_count = millis();
    
  }
 if (sensorState4 == LOW && (millis() - last_count > timex)) { //counts people IN
    count++;
    Serial.println(count);
   last_count = millis();
  }
  if (sensorState2 == LOW && (millis() - last_count > timex)) { //counts people OUT
    count--;
    Serial.println(count);
    last_count = millis();
  }
 if (sensorState3 == LOW && (millis() - last_count > timex) ) { //counts people OUT
    count--;
    Serial.println(count);
    last_count = millis();
  }
  if (count == 0 && sensorState2 == LOW)
 
  {
   anim_state = TODOWNOFF;
  }
 
  if (count == 0 && sensorState3 == LOW)
 
  {
     anim_state = TOUPOFF;
 
  }
 
  if (count == 1  &&  sensorState1 == LOW)
  {
    anim_state = TOUPON;
  }
  if (count == 1 &&  sensorState4 == LOW)
  {
    anim_state = TODOWNON;
  }
  if (count < 0)
  {
    count = 0;
  }
}

void  animate_state(){
  if (anim_state == TOUPON){
  //last_millis = millis();
  if (anim_state != last_anim_state) { // state just changed, lets set the counter to 0
    counter = 0;
    sr.set(counter, HIGH);
    Serial.println("starting toupon ");
    last_millis = millis();
  } else if ( millis() > last_millis + time1 ) {   // continue the animation if the time interval is up
    counter++;
    sr.set(counter, HIGH); 
    Serial.print(" lights on ");
    Serial.println(counter);
    last_millis = millis();
  }
  if ( counter >= 15 ) anim_state = NONE;
  
}
  if (anim_state == TOUPOFF){
  //last_millis = millis();
  if (anim_state != last_anim_state) { // state just changed, lets set the counter to 0
    counter = 0;
    sr.set(counter, LOW);
    Serial.println("starting toupOff");
    last_millis = millis();
  } else if ( millis() > last_millis + time1 ) {   // continue the animation if the time interval is up
    counter++;
    sr.set(counter, LOW); 
    Serial.print(" lights off ");
    Serial.println(counter);
    last_millis = millis();
  }
  if ( counter >=15) anim_state = NONE;
  
}

if (anim_state == TODOWNON){
  //last_millis = millis();
  if (anim_state != last_anim_state) { // state just changed, lets set the counter to 0
    counter = 15;
    sr.set(counter, HIGH);
    Serial.println("starting todownon ");
    last_millis = millis();
  } else if ( millis() > last_millis + time1 ) {   // continue the animation if the time interval is up
    counter--;
    sr.set(counter, HIGH); 
    Serial.print(" lights on ");
    Serial.println(counter);
    last_millis = millis();
  }
  if ( counter == 0 ) anim_state = NONE;
  
}

if (anim_state == TODOWNOFF){
  //last_millis = millis();
  if (anim_state != last_anim_state) { // state just changed, lets set the counter to 0
    counter = 15;
    sr.set(counter, LOW);
    Serial.println("starting todownon ");
    last_millis = millis();
  } else if ( millis() > last_millis + time1 ) {   // continue the animation if the time interval is up
    counter--;
    sr.set(counter, LOW); 
    Serial.print(" lights off ");
    Serial.println(counter);
    last_millis = millis();
  }
  if ( counter == 0 ) anim_state = NONE;
  
}
last_anim_state = anim_state;
}
void switchDecision() {
 

   switch(state){
    
    case 1:
    Serial.print("all on");
      sr.setAllHigh();
      old = state;
      break;
      
    case 2:
     Serial.print("sensors on");
    peopleCount();
    animate_state();
     
    default:
     Serial.print("all off");
       sr.setAllLow();
      old = 0;
      break;
   }
}

an IR sensor says "YES (or "NO) there is something radiating enough heat in front of me"

The ultrasonic sensor is likely to give you a distance to the first obstacle

So you'll need to find a way to transform the distance into "YES" or "NO"

1 Like

Yeah... Thats what even I was think of...lets see how it goes

Rather than trying to build ultrasonic rangers into your existing code, can I suggest that you breadboard a couple of sensors, and see if they'll do what you want?
A common mistake with ultrasonic rangers is to trigger two at once, and expect them to discriminate their own returns.

ir sensors works perfectly but as it cant be used outdoors..soo thinkin about ultrasonic could work...
but it feels hard to code for ultrasonic sensor rather than ir sensors.
soo what do you mean by Trigger two at once. which one do you recommend ir or ultrasonic?

Rangers typically have two signal pins, one called trigger or trig (an input signal), and the other called echo (an output signal).
When you toggle trigger, it causes the ranger to start a measurement cycle, and echo goes high until the receiver detects the return of the transmitted pulse train, when it goes low.

Thus, range is proportional to the pulse width on the echo pin.

If you trigger two sensors at the same time, the sensors could confuse the pulse train from the other ranger, and return a false reading, depending on how you have arranged the devices.

I recommend the solution that works for your application.

yeah i get your point and that i have even fixed with ir sensor. as if 1st senor is detected ..all sensors dont detect for next 3 seconds. which could prevent from false detections...soo can i use the same with ultrasonic sensor?

well all your IR sensors are live at the same time, always. the fact that you ignore the others for 3s is unrelated.

That won't work like that with ultrasonic sensor. You have to tell each sensor to run a check, wait for the result to come, and then - after a bit of time so that all bouncing ultrasounds are gone - trigger the next one

(that is unless the sensors are physically in different locations where ultrasounds from another uint cannot be received)

yeah get your point but i want to use it in stairs soo i dont think any other stuffs will cause issue

This depends on how much ultrasonic sound can bounce back in a directed reflection.
And how much ultrasonic sound is reflected back depends on the environment you use the sensors.

Did you google for "arduino ultrasonic sensor code" you should find dozens of example-codes that show the principle how to "drive" ultrasonic sensors.

best regards Stefan