Hello!
I understand how to do the 'blinkwithoutdelay' using currentMillis to set the time, and compare to some interval, and run code. However, I'm stuck on how to do that twice in a row.
I am using Parola to send a string through the LEDs. I can get that working, no problemo. However, I am now trying to hook up a PING Ultrasonic Distance sensor, take a distance reading, and output that distance to the LEDs. The PING sensor will use delayMicroseconds() to send a ping, delay, receive the ping. Here's the original code:
void measureDistance()
{
// set pin as output so we can send a pulse
pinMode(signal, OUTPUT);
digitalWrite(signal, LOW);
delayMicroseconds(5);
digitalWrite(signal, HIGH);
delayMicroseconds(5);
digitalWrite(signal, LOW);
pinMode(signal, INPUT);
pulseduration=pulseIn(signal, HIGH) / 2;
// pulseduration=pulseduration/2;
distance = int(pulseduration/29);
indistance = distance*.39; //<<<<<<<<<<< changed 'indistance' to FLOAT
}
See the 'delayMicroseconds'? How can I get rid of that? When I leave that in, the scrolling on the LEDs delays as well. I was able to get this far- but can't figure out how to do the second delay.
Here's what I have been able to get so far:
const int pingDelay = 5;
int previousMillis = 0;
void measureDistance()
{
unsigned long currentMillis = millis();
// set pin as output so we can send a pulse
pinMode(signal, OUTPUT);
digitalWrite(signal, LOW);
if (currentMillis - previousMillis > pingDelay){
previousMillis = currentMillis;
// delayMicroseconds(5);
digitalWrite(signal, HIGH);
} /// How do I do that again??
delayMicroseconds(5);
digitalWrite(signal, LOW);
pinMode(signal, INPUT);
pulseduration=pulseIn(signal, HIGH) / 2;
// pulseduration=pulseduration/2;
distance = int(pulseduration/29);
indistance = distance*.39;
}
Do I need something like 'currentMillis2' or 'previousMillis2' ? Because I don't think I can just repeat the same code again, since I reset 'previousMillis = currentMillis' right?
I'm pretty sure the code I posted should get you to understand the issue - but if you would like the whole thing/more, just let me know!
Thanks for any ideas, it's a relatively easy idea I'm sure - I'm just having trouble getting my head wrapped around how to do it. :~
(I kind of wish we had something like spoiler tags, so you could provide hints/advice, I could work on it, then just click/hover over the spoiler tag to see the solution).