Go Down

Topic: Read the data when it becomes stable (Read 160 times) previous topic - next topic

phoqueue

Jul 14, 2017, 04:46 pm Last Edit: Jul 14, 2017, 04:54 pm by phoqueue
Hi guys! I am working on a counting scale that can count how many pills are in a container using load cell and send an sms using gsm module when someone took 1 or 2 pills or add 1 or 2 pills but my problem is the code reads the data so fast so I just want some delay or something that it will count when the quantity is stable. Here is the code.

#include <HX711.h>


HX711 scale(A1, A0);
long val = 0;
long quantity = 0;
float count = 0;
long newquantity = 0;

void setup() {
Serial.begin(9600);
delay(2000);
}


void loop() {


   
count = count + 1;
val= scale.read ();
quantity = newquantity;
newquantity = ((((((val - 107400)/305890.0f) * 50))-35.9)/0.555);




delay(1000);
      if (newquantity == quantity - 1)
      {
       Serial.println("1 PILL WAS TAKEN");
      }
      else if (newquantity == quantity + 1)
      {
        Serial.println("1 PILL WAS ADDED");
      }
  if (newquantity == quantity - 2)
      {
       Serial.println("2 PILLS WERE TAKEN");
      }
      else if (newquantity == quantity + 2)
      {
        Serial.println("2 PILLS WERE ADDED");
      }


delay (1000);
}
 


So instead of sms, i am testing it on serial monitor. It works fine but i have to be super fast in getting a pill to make it print accurately and it is hard getting 2 or more pills that fast. Can you please help me what will I add to my code to make it read after the data are stable. I am taking the pills by hand so sometimes a pressure when I am getting a pill is added to the data and it makes it unaccurate so I am wondering what code I must add to make it not read so fast or have a delay before reading the data. Thank you so much in advance. Hope it works on the serial monitor so i can test it using gsm module. Also can you tell me what is the best gsm module. I tried A6 but it is so inconsistent in transmitting and receiving signals so I am wondering what is the best consistent gsm module that works smoothly

Jiggy-Ninja

Define "stable" as precisely as you can. What particular quality are you looking for in the weight signal that determines if the signal is stable vs. unstable?
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

phoqueue

Define "stable" as precisely as you can. What particular quality are you looking for in the weight signal that determines if the signal is stable vs. unstable?
Hmmmm. Since I am getting a pill by hand the force in my hand adds up to the weight and the quantity will be unstable or inaccurate so what I meant for stable actually after getting 1 or 2 pills it will then begin to read the data not when i am currently taking a pill

UKHeliBob

Read the weight data many times over a period of time.  When several consecutive readings do not vary from one another by larger than a margin that you will need to determine, then the reading is stable and decisions about how many pills are present can be made based on the current weight.

Don't bother to do the calculation of pill quantity until the reading is stable.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Jiggy-Ninja

Hmmmm. Since I am getting a pill by hand the force in my hand adds up to the weight and the quantity will be unstable or inaccurate so what I meant for stable actually after getting 1 or 2 pills it will then begin to read the data not when i am currently taking a pill
Your Arduino doesn't know anything about hands or pills. All it sees is the number from the scale.

So imagine yourself seeing just the sequence of weights from the scale. Every time you get a new reading, what sort of comparisons do you have to make against the previous readings to determine if the signal is currently "stable" or "unstable".

This might not be something you can do purely theoretically. You will probably have to do an actual experiment to figure out what the weight signal looks like when you take a pill out of the container. Or when you put one in. Or drop one in. Or stomp on the floor near the container. Or bang the table it's on. Or root around in the container with your finger and not take any pills.

UKHeliBob's suggestion is probably good enough, but signal processing can be complicated. You should make sure of the details of signal so you can decide how big the deadband needs to be and how long it needs to stay in the deadband to be declared "stable".
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

Go Up