Tuesday, 3 January 2017

Stock counting with ultrasonic sensors

This is very a very simple way you can count many things. But in this example you can see that it updates the stocks every 2 seconds and which ever unit gets less stock or gets empty it will uptade it within 2 seconds.
Ad

How does it work?
Well, it measure the distance between items and itself and by doing this it can find out if there is something there or not and finds out how many items are there.

Automatic stock counting with ultrasonic sensors
int trigPin1 = 5;
int echoPin1 = 6;

int trigPin2 = 7;
int echoPin2 = 8;


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

void sensor1(){ // 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;

   

if (distance1 <=14){
Serial.println("3 Leogs left in unit 1");
    }
else if (distance1 <=17) {
Serial.println("2 Legos left in unit 1");
  }

else if (distance1 <=21) {
Serial.println("1 Lego left in unit 1");
  }
 
else if (distance1 <=24) {
Serial.println("Nothing left in unit 1");
  } 
else  {
//ggg
  }
}
//
void sensor2(){ // This function is for first sensor.
  int duration2, distance2;
  digitalWrite (trigPin2, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin2, LOW);
  duration2 = pulseIn (echoPin2, HIGH);
  distance2 = (duration2/2) / 29.1;

   

if (distance2 <=13){
Serial.println("3 Leogs left in unit 2");
    }
else if (distance2 <=16) {
Serial.println("2 Legos left in unit 2");
  }

else if (distance2 <=19) {
Serial.println("1 Lego left in unit 2");
  }
 
else if (distance2 <=23) {
Serial.println("Nothing left in unit 2");
  } 
else  {
//ggg
  }
}

void loop() {
Serial.println("\n");
sensor1();
sensor2();
delay(2000);
}

Note:
You might need to measure the distance manually and put it in the sketch so that it can work with your stock shelve. This is just an idea, you'll have to try it out so that cand work accoding to your need.
Ad

* You can change the delay time to what ever time you want or you can even make it on command like the sketch below.

On command stock counting with ultrasonic sensors
int trigPin1 = 5;
int echoPin1 = 6;

int trigPin2 = 7;
int echoPin2 = 8;


void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
}
String str;

void u1(){ // 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;

   

if (distance1 <=14){
Serial.println("3 Leogs left in unit 1");
    }
else if (distance1 <=17) {
Serial.println("2 Legos left in unit 1");
  }

else if (distance1 <=21) {
Serial.println("1 Lego left in unit 1");
  } 
  
else if (distance1 <=24) {
Serial.println("Nothing left in unit 1");
  }  
else  {
//ggg
  } 
}
//
void u2(){ // This function is for first sensor.
  int duration2, distance2;
  digitalWrite (trigPin2, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin2, LOW);
  duration2 = pulseIn (echoPin2, HIGH);
  distance2 = (duration2/2) / 29.1;

    

if (distance2 <=13){
Serial.println("3 Leogs left in unit 2");
    }
else if (distance2 <=16) {
Serial.println("2 Legos left in unit 2");
  }

else if (distance2 <=19) {
Serial.println("1 Lego left in unit 2");
  } 
  
else if (distance2 <=23) {
Serial.println("Nothing left in unit 2");
  }  
else  {
//ggg
  }
}

void loop() {
if(Serial.available())
{
str = Serial.readStringUntil('\n');

if(str == "u1"){
u1();
  }
 
  if(str == "u2"){
u2();
  }
}
}

For this sketch you just need to send u1 command to the get result from the unit one and u2 to get the result of unit two.

That's all for this post I hope it helps you and you can see the sketch in action below.

Ad



No comments:

Post a Comment