How to delete delay

i want to delete the delay but i dont know how for my rc car project

int ch1; //knipper lichten            stuur
int ch2; //rem ligt                   gas
int ch3; //all knipper lichten        switch 2
int ch4; //lapen groot en normaal     switch 1
int ledpinr = 2;
int ledpinl = 3;
int normaallicht = 4;
int grootlicht = 5;
int achteruitrijlicht = 7;
int remlicht = 6;


void setup() {
  pinMode(13, INPUT);
  pinMode(12, INPUT);
  pinMode(11, INPUT);
  pinMode(10, INPUT);
  pinMode(ledpinr, OUTPUT);
  pinMode(ledpinl, OUTPUT);
  pinMode(normaallicht, OUTPUT);
  pinMode(grootlicht, OUTPUT);
  pinMode(remlicht, OUTPUT);
  pinMode(achteruitrijlicht, OUTPUT);
  Serial.begin(9600);
}

void loop() {
ledkniperlicht();
lampenklijn();
lampengroot();
remlichten();
achteruitrijlichten();
gevaarenlichten();
ch1 = pulseIn(13, HIGH, 25000);
ch2 = pulseIn(10, HIGH, 25000);
ch3 = pulseIn(12, HIGH, 25000);
ch4 = pulseIn(11, HIGH, 25000);
Serial.print(F("ch 1 "));
Serial.println(ch1);
Serial.print(F("ch 2 "));
Serial.println(ch2);
Serial.print(F("ch 3 "));
Serial.println(ch3);
Serial.print(F("ch 4 "));
Serial.println(ch4);
}

void ledkniperlicht() {
   if ((ch1) > 1550) {
        digitalWrite(ledpinr, HIGH);
        delay(500); 
        digitalWrite(ledpinr, LOW);
        delay(200);
   }
   else if ((ch1) < 1450) {
        digitalWrite(ledpinl, HIGH);
        delay(500); 
        digitalWrite(ledpinl, LOW);
        delay(200);
   }
}                                                                                                                                                                                              
void lampenklijn() {
  if ((ch4) > 1100) {
    digitalWrite(normaallicht, HIGH);
  }
  else if ( (ch4) < 1000) {
    digitalWrite(normaallicht, LOW);
  }
}
void lampengroot() {
  if ((ch4) > 1600) {
    digitalWrite(grootlicht, HIGH);
  }
  else if ( (ch4) < 1500) {
    digitalWrite(grootlicht, LOW);
  }
}
void remlichten() {
  if ((ch2) > 1480 && (ch2) < 1520) {
    digitalWrite(remlicht, HIGH);
  }
  else if ((ch2) < 1440) {
    digitalWrite(remlicht, LOW);
  }
  
  else if ((ch2) > 1560) {
    digitalWrite(remlicht, LOW);
  }
}
void achteruitrijlichten() {
  if ((ch2) > 1540) {
    digitalWrite(achteruitrijlicht, HIGH);
  }
  else if ((ch2) < 1530) {
    digitalWrite(achteruitrijlicht, LOW);
  }
}
void gevaarenlichten() {
  if ((ch3) > 1800) {
        digitalWrite(ledpinl, HIGH);
        digitalWrite(ledpinr, HIGH);
        delay(500); 
        digitalWrite(ledpinl, LOW);
        digitalWrite(ledpinr, LOW);
        delay(200);
   }
   else if ((ch3) < 1000) {
        digitalWrite(ledpinl, HIGH);
        digitalWrite(ledpinr, HIGH);
        digitalWrite(normaallicht, HIGH);
        digitalWrite(grootlicht, HIGH);
        digitalWrite(achteruitrijlicht, HIGH);
        digitalWrite(remlicht, HIGH);
        delay(500); 
        digitalWrite(ledpinl, LOW);
        digitalWrite(ledpinr, LOW);
        digitalWrite(normaallicht, LOW);
        digitalWrite(grootlicht, LOW);
        digitalWrite(achteruitrijlicht, LOW);
        digitalWrite(remlicht, LOW);
        delay(200);
   }
}
  1. Locate the line with delay(...); in your code
  2. Place cursor at the end of line
  3. Press Backspace key repeatedly until the line is clear
4 Likes

Do you mean that you want to replace delay() with something that would not block the operation of the program ?

yes

You will need to completely rewrite your sketch in order to do that. Please explain exactly what the sketch should do

it must to do in void ledknipperlicht if ch1 higher than 1550 then ledpinr should be flashing and ch1 lower than 1450 then ledpinl should be flashing

You need to do two things,
i) rewrite your sketch as 'tasks' see my tutorial on Multi-tasking in Arduino
ii) use a 'timer' based on the millis() function instead of delays()
See my tutorial How to write Timers and Delays in Arduino which uses a simple millisDelay class to keep track of what the timers are doing.
multitaskingDiagramSmall

@pasie If what you want to do is to flash an LED when a certain condition is met then that is comparatively easy

When the condition is met set a boolean to true, then later in loop() test the value of the boolean and if it is true blink the LED using millis() for timing

  if (blinking)
  {
    if (currentTime - lastBlinkTime >= blinkInterval)
    {
      digitalWrite(ledPin, !digitalRead(ledPin));
      lastBlinkTime = currentTime;
    }
  }

Another way to get a blinking pattern without delay():

   if ((ch1) > 1550)
  {
    // ON for the first 500 ms of each 700 ms interval
    digitalWrite(ledpinr, (millis() % 700) < 500);
    digitalWrite(ledpinl, LOW);
  }
  else if ((ch1) < 1450)
  {
    // ON for the first 500 ms of each 700 ms interval
    digitalWrite(ledpinl, (millis() % 700) < 500);
    digitalWrite(ledpinr, LOW);
  }
  else
  {
    // Turn off the blinkers if neither is selected
    digitalWrite(ledpinr, LOW);
    digitalWrite(ledpinl, LOW);
  }

The same will work for your other pattern.

1 Like

thx you are the best

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.