Hello, I have two Arduinos, one is effectively sending pulses through a pulled up pin as follows: 3 pulses (5 milliseconds LOW 5miliseconds HIGH),waits 2 seconds--send 20 pulses same timing--wait 2 seconds--- send 7 pulses same timing and repeat everything. So far so good.
Then I put together this code for the receiver on a second arduino:
int inputPin = A1;
int lastState = HIGH;
int pulseCount = 0;
bool isCounting = false; // Flag to check if we are currently counting
void setup()
{
Serial.begin(9600);
pinMode(inputPin, INPUT_PULLUP);
}
void loop()
{
static unsigned long lastPulseTime = 0; // Time of the last pulse
unsigned long currentTime = millis();
int inputState = digitalRead(inputPin);
if (inputState == LOW)
{
if (lastState == HIGH)
{
if (!isCounting) { //isCounting == 0;
pulseCount = 0; // Reset counter when new counting starts
isCounting = true;
Serial.println("Counting started");
}
pulseCount++;
lastPulseTime = currentTime;
}
//lastState = inputState;
}
// Check if the pulse counting should stop
if (isCounting && (currentTime - lastPulseTime > 500)) {
Serial.print("Final pulse count: ");
Serial.println(pulseCount);
isCounting = false; // Stop counting
}
lastState = inputState;
}
Initially i thought that if (isCounting && (currentTime - lastPulseTime > 500)) { would work as a timeout to send the data to the monitor and if we set the time below 200 we shouldn't be able to count the 20 pulses because ideally they take: (5*20)*2 = 200 miliseconds as I verified using wowki simulator and PulseView(pic attached) but I can go as low as 10 miliseconds and the monitor still is showing the right values! so any idea of why is nor dropping the count is highly appreciated.
even more I went to wowki again and I merged both Arduino codes, (I gave up trying to do the json stuff to simulate the inputs so I merged them) and The simulation also shows I can go up to 10 miliseconds for the timeout and still the counts are ok! So any idea is welcome!
One Arduino UNO is sending the "formatted wave" repeatedly. Second Arduino UNO is seeing the wave at DPin-A1. What will second UNO do with the received wave -- I mean what kind of processing?
it is supposed to be part of a bigger sketch. the final outcome it is will be able to perform a task according to the number of received pulses, some kind of poor's man one wire/one way protocol, so for now the only task is to inform the number of received pulses, I mean it works, but it bothers me that i was like why is not working ?and now I'm like why is working? so the Arduino uno sending the pulses is only for debugging the second one.
I know it can be done with interrupts or libraries, but even if I do so I would be like why didn't stopped counting the pulses mmy first sketch?
You are sending 3 groups of pulses each seperated by 2-sec time gap? Do you want to count the arrived number of pulses like: 3, 20, and 7 and save them into seperate variables?
This is not correct, if I understand it right. The difference is between the pulse and previous one, not for the total 20 pulses. So it will be about 10 milliseconds maximum during the pulses train.
That's why it works when you reduce the margin. Actually it should be about 15 or 20 milliseconds, to detect the end of the pulses train.
Not really separate variables, just one and be like ho well my pulseCount is 7, I must to do something, later it will be like ho this time my pulseCount is 20 now I'm doing something else, the 2 seconds gap will be at random I just did that to troubleshoot the receiver but I wanted to implement a timeout to ensure the incoming pulses are finished and the line returned to high as an idle state.
So every time I get a pulse I start the waiting?so what should be the right approach to ensure no more pulses are coming before take actions? start counting from the beginning of the first LOW state I get and be sure not to send trains of pulses bigger than the timeout or leave the code as is, something more obscure and complicated or happily something more simple?
Yup, you are right, now i see it crystal clear, every time I go from HIGH to LOW I reset the time counting, i went to the simulation again and it is starts dropping the count t(actually counting just the first pulse) at some point between 9.5 and 10 milliseconds, so yeah 15 milliseconds work just right, every time. Many thanks to everyone!