I am struggling with a task where by I need to detect a potential collision using an ultrasonic sensor. The sensing system should flash an LED from 1m to 51cm and light the LED solid from 50cm to 5cm without slowing down the collision system. The sensor used is a parralax PING))) sensor. Below is the programming for the task but any advice on where i have gone wrong and solutions would be great. Thanks
Why not start by defining the problem with specifications including a flow chart and schematic. Define what is the expected outcome. Purchase the Arduino cookbook and read it, this will give you some basics. also use the online tutorials and videos available, there are many good ones on this web site. At this point you will be able to define the problem and may have already solved it.
Have fun blinking a led with delay() at the same time you use the ping with delay().
There is an Arduino Tutorial Example named BlinkWithoutDelay that you might want to play with. It has some errors that won't manifest in less than 24 hours but the overall lesson is still good.
holdingpattern:
Have a look at the code below, which:
Uses the newping library (install from library manager) which doesn't use pulseIn and is less blocky. (Not sure if it's totally unblocky, never looked)
Puts parts of code in functions all called from loop() which keeps loop() nice and clean and facilitates debugging
Uses blink without delay technique to call sonar regularly but delay()-lessly
Uses blink without delay to blink the alertLed when appropriate
Uses blink without delay to blink the builtin led all the time as a sort of proof of life. This is especially useful when you want to prove that code's not blocky.
Karma to you for the effort and that you took the trouble to learn millis timing!
Did you also get the state machine lesson? Between the two you can handle most automation code.
Some things you might change:
The #defines for pin numbers, the compiler will use ints. Use const byte for pin numbers and save 1 byte each.
When you are doing calculations you want your variables to either be the same type or cast the ones that aren't as that type.
This is what "don't do mixed math calculations" is about.
With time calculations, use unsigned integer (byte, word, unsigned long, unsigned long long) type or cast to that.
if ( millis() - previousMillisPulse >= (unsigned long) pulseLedInterval ) // casting that int to UL
You can time 65.535 seconds using only the low 16 bits of millis() return and Arduino word vars (uint16_t). This is fine where intervals are short like 60 seconds or less and of course your code does not block!
The BWD example as is will bug just shy of 25 days when the signed interval can only hold half of what the unsigned start time variable does.
Crank up your serial baud rate. At 9600 it takes > 1 ms for 1 char to be sent and the output buffer only holds 64 chars.
It becomes a problem when a print tries to overfill the buffer and then execution is stuck on that print statement until enough chars are sent out to let the print finish. The slower the baud rate, the longer the blocking and the greater the chance of having an overfill. 115200 baud is a nice rate, it gives the Arduino over 1300 cycles between chars coming in or going out.