Motor and LED strip wont blink simultaneously

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.

Below are the codes i have so far.

long distance;
long duration;
int red = 10;
int brightness = 0;
int motor1pin1 = 2;
int motor1pin2 = 3;
int fadeAmount = 5;
const int trigPin = 12;
const int echoPin = 11;

void setup() {
  Serial.begin(9600);
  Serial.println(F("Starting"));
  pinMode(red, OUTPUT);
  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  ultra();
  top_open();
  Serial.print("Distance:");
  Serial.println(distance);
}

void top_open() {
  if(distance < 10) {
    digitalWrite(motor1pin1, HIGH);
    digitalWrite(red, HIGH);
    delay(30);
    digitalWrite(motor1pin2, LOW);
    digitalWrite(red, LOW);
    delay(30);
    delay(10000);       
  }
  else {
    analogWrite(red, brightness);
    brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
    digitalWrite(motor1pin1, LOW);
    digitalWrite(motor1pin2, LOW);   
  }
  
  delay(20);
}

void ultra(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
}

Any feedback is appreciated.

In the code below you are only turning on the LED for 30 ms which means you can't see it. What do you want the LED to do? Stay on? Blink?

  if(distance < 10) {
    digitalWrite(motor1pin1, HIGH);
    digitalWrite(red, HIGH);
    delay(30);
    digitalWrite(motor1pin2, LOW);
    digitalWrite(red, LOW);
    delay(30);
    delay(10000);       
  }

Can you be very specific what you want to do when the distance < 10 vs distance >= 10?

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.

I did try to use BlinkWithoutDelay example, but for some reason the LED wont run and only the ultrasonic sensor and motor run.


long distance;
long duration;
const int led =  10;
int brightness = 0;
int motor1pin1 = 2;
int motor1pin2 = 3;
int fadeAmount = 5;
const int trigPin = 12;
const int echoPin = 11;
int ledState = LOW;           
unsigned long previousMillis = 0;        
const long interval = 100;
          
void setup() {
  Serial.begin(9600);
  Serial.println(F("Starting"));
  pinMode(led, OUTPUT);
  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void ultra(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
}

void loop() {
  ultra();
  top_open();
  Serial.print("Distance:");
  Serial.println(distance);
}

void top_open() {
  if(distance < 10) {
    unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(led, ledState);
    digitalWrite(motor1pin1, HIGH);
    digitalWrite(motor1pin2, LOW);
    delay(5000); 
          
  }
  else {
    analogWrite(led, brightness);
    brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
    digitalWrite(motor1pin1, LOW);
    digitalWrite(motor1pin2, LOW);   
  }
  
  delay(20);
  }
}

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

best regards
Stefan

Hi, aren't you using any motor driver IC/module?

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