Hello everyone, i am trying to make a code where an ultrasonic sensor will give input to a motor and an LED strip and they are supposed to run together, i cant seem to make them run simultaneously, it kept shutting off the LED strip and only the motor will run.
In the posted code no LED strip is controlled but only one LED.
I guess your basic problem is the use of delay(). Take a look at the example BlinkWithoutDelay to see how you can avoid using delay() to start and stop some actions by a time frame.
when the distance < 10 i want the motor and led to run simultaneously, the motor would spin and the LED will blink repeatedly. I didnt use distance >= 10 because i did not know of its function.
When using millis() to control the timing behavior you must eliminate all delay() calls. You shouldn't introduce new features (fading the LED) before you got the base code running!
there are two things to change
1.
static unsigned long currentmillis;
static unsigned long previousMillis;
and blink WITHOUT delay. means without ANY delay
ALL delays must be replaced by non-blocking timing
your
delay(5000)
makes your code
stop executing for 5 seconds
this delay must be replaced by non-blocking timing based in millis()
though this poor blink without delay example does not cover how to code keeping your motor switched on for five seconds
@the community:
such questions pop up every week new
there should be a tutorial that shows how to use non-blocking timing in various ways.
going beyond blink
one-shot timer
different on/off times
introducing to the only loop is void loop() & code everything conditional on flags
Start again! You should open a new sketch for each component of you project and get each one working individually before you combine them. Doing everything together in one sketch will lead to problems. Complexity is just a combination of simple things. Best to work on simple things rather than the combination