delay(500);
{
delay(3);
String a = Serial.readString();
{
if (newquantity == quantity-1)
{
Serial.println("1 PILL WAS TAKEN");
}
else if (newquantity == quantity+1)
{
Serial.println("1 PILL WAS ADDED");
}
else if (newquantity == quantity-2)
{
Serial.println("2 PILLS WERE TAKEN");
}
else if (newquantity == quantity+2)
{
Serial.println("2 PILLS WERE ADDED");
}
else if (newquantity == quantity-3)
{
Serial.println("3 PILLS WERE TAKEN");
}
else if (newquantity == quantity+3)
{
Serial.println("3 PILLS WERE ADDED");
}
So the code above uses load cell to measure the quantity of pills in the container. The if statements show that everytime I get 1 PILL it will say 1 pill were taken and everytime I get 1PILL it will say 1 PILL was added.
It always works in the 1 PILL condition but seldom works on 2 and 3 PILLS condition because I am having a hard time getting 2 or 3 pills in the container. I am wondering is there a way of delaying before reading because the load cell is quite sensitive and i sometimes apply pressure while getting 2 or 3 pills it will read my pressure sometimes 2 pills added and sometime 3 pills added. So i would like an assistance on this one where there is a delay like 5 seconds before reading the data or an ample time for me to get 2 or 3 pills without the system reading it. Thank you in advance. I hope you guys understand what my problem is.
In general using delay when reading serial data is a mistake. You want to read as soon as possible
always, so you don't lose/corrupt incoming serial data. You can always ignore data you are not ready for.
MarkT:
In general using delay when reading serial data is a mistake. You want to read as soon as possible
always, so you don't lose/corrupt incoming serial data. You can always ignore data you are not ready for.
My application on this is when someone take two or three pills gsm module will text to a caregiver that the patient took 2 or 3 pills. So I need something to delay because its hard to get 2 or 3 pills in an instant unlike 1 pill.
The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.
The examples in Serial Input Basics illustrate simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
ymmot17:
Where will i put this in my code? I am sorry I am just a beginner in codes. I just want to read the amount of pills until it is stable
where you have the call
val= scale.read ();
insert the code replacing the calls to analogueRead() with call to scale.read ()
remember the example is in pseudo code so you will have to translate it to C/C++
insert the code replacing the calls to analogueRead() with call to scale.read ()
remember the example is in pseudo code so you will have to translate it to C/C++
What values should I assign to settle time, error, readtime and initial time
the settleTime is the time you require for the signal to settle, initially try 5 seconds and see if it works then try reducing it
error is to allow for the noise in your signal - say 5% of the maximum value scale.read() gives? you may have to experiment with this
replace readTime() and currentTime() with calls to millis()
I’m surprised it works at all. Looks like you’re trying to do equality tests on floats. Of course, since you didn’t post your full code, I can’t see the actual variable definitions. Also, surprised no one has complained yet about your lack of code tags and use of Magic Numbers.