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;
}
}