HI!
Im using Arduino Mega2560. I have sensor connected to my arduino board.
I need to read the sensor and check that I read request value for 200ms,
I wrote the following code. It works well, but I can't use while because it is blocking other things.
I couldn't have any Idea how to use this technique without while.
Please if can someone help me!!!
my code is:
The function must be out of loop.
the loop read commands from Serial port and proccess the commands, If i get specific command then I need this check.
my loop:
void loop() {
if (COMM_SERIAL.available()) {
processCommInput(COMM_SERIAL.read());
}
}
void processCommInput (unsigned char c) {
if (COMMAND_START_CHAR == c) {
commandBufferIndex = 0;
commandCollecting = 1;
}
} else {
commandBuffer[commandBufferIndex++] = c;
if (COMMAND_BUFFER_SIZE == commandBufferIndex) {
processCommandBuffer();
commandCollecting = 0;
}
}
proccessCommandBuffer() proccess incomming command and check sensors accoring to the command.
So you want to read a stream of values from serial, ensuring that the same value is available for a number of millisecond, while simultaneously doing something else? Please supply more details on what it is you're trying to create.
My loop read commands from serial. then proccess the command and do something according to the command id:
'L'- lighting led on .
'D'- check if sensor is on for 1000ms.
whenevey I got the command I need to do the request proccess.
my problem is with the code above: It blocks the code and in those millisecond the arduino is not reading the other commands.
I need to read the sensor for X ms and get the same value, not iin the loop.
It's separate logic, different question. It's useful to phrase "has been something for a period of time" to "has never not been something for a period of time". The way you do that is like:
if (now - timestamp > period)
{
code you do when sensor has been active for 1000
}
if (sensor is not active)
{
time stamp = now
}
but when you awaiting low level on sensor pin it is anyway nothing other executed.
or do you accept that readings will not whole 1000ms but sometimes in this period?