Optimization of blinking code for car with warning lights

I want to short up the following working code for a blinking module of a car:

void blinker()
{ 
  uint32_t gpio_1, gpio_2;
  leftb.getValue(&gpio_1);
  rightb.getValue(&gpio_2);   
    if ( gpio_1 == LOW|| gpio_2 == LOW||warning_marker == 1) {    //if blinker switch in left or right blinker position
    uint16_t currentMillis = millis(); 
    if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW){
      ledState = HIGH;
    }
    else {
      ledState = LOW;
     }
    }
    // set the output for left or right blinker
      if ( gpio_1 == LOW ){
      sendCommand("t14.bco=0");
      delay(10);
    }
      if (gpio_2 == LOW){
      sendCommand("t13.bco=0");
      delay(10);
    }
      if (warning_marker == 1 && gpio_1 == HIGH && gpio_2 == HIGH){
      if (ledState == LOW){
        sendCommand("bt1.pic1=7");
        delay(10);
      }
      else{
        sendCommand("bt1.pic1=6");
        delay(10);
      }
    }
    else
    {
      sendCommand("bt1.pic1=6");
      delay(10); 
    }
    if (warning_marker == 0 && gpio_1== LOW || (warning_marker == 1 && gpio_2 == HIGH)){
      blinkerPin = leftblinkerPin;
     if(gpio_1 == LOW){
       digitalWrite(rightblinkerPin, HIGH); 
      }
      if (ledState == LOW){
       sendCommand("t13.bco=64512");
       delay(10);
       }
      else {
       sendCommand("t13.bco=0"); 
       delay(10); 
      }
      digitalWrite(blinkerPin, ledState);
     }
    if (warning_marker == 0 && gpio_2 == LOW || (warning_marker == 1 && gpio_1 == HIGH)){
      blinkerPin = rightblinkerPin;
     if(gpio_2 == LOW){
       digitalWrite(leftblinkerPin, HIGH); 
      }
      if (ledState == LOW){
       sendCommand("t14.bco=64512");
       delay(10);
       }
      else {
       sendCommand("t14.bco=0"); 
       delay(10); 
      }
      digitalWrite(blinkerPin, ledState);
      }   
 }
  else {
    if(ledState == LOW){ 
    digitalWrite(rightblinkerPin, HIGH);
    sendCommand("t14.bco=0");
    delay(10);
    digitalWrite(leftblinkerPin, HIGH); 
    sendCommand("t13.bco=0");
    delay(10);
    }     
  }
}

The blinker are displayed on a Nextion display as well. The following modes shall be given:

blinker left and right warning blinker which is only active if blinker left or right were not chosen.

do you understand the code?

Hi,

Can you post the whole code, and what model Arduino are you using?

Tom... :)

    uint16_t currentMillis = millis();   ////// ERROR: Should be 'uint32_t'
    if (currentMillis - previousMillis >= interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;    //////  "previousMillis += interval;" will give a better average blink rate

You can take advantage of LOW==0==false and HIGH==1==true to simplify some code:

void setup() {}
void loop() {}

boolean ledState = false;
boolean warningMarker = false;
uint32_t previousMillis;   // TurnSignalTimer
boolean PreviousLeftSignal = false, PreviousRightSignal = false;
unsigned long interval = 500;  // TurnSignalInterval

void blinker()
{
  //  It's too bad that ".getValue()" can't just return a value like a normal function
  uint32_t gpio_1, gpio_2;
  leftb.getValue(&gpio_1);
  rightb.getValue(&gpio_2);

  // Blink a light if the turn signal if set that way or hazard flashers are on
  boolean leftSignal = !gpio_1 || warningMarker;
  boolean rightSignal =  !gpio_2 || warningMarker;

  // Is a signal just turning on or off?
  if ((leftSignal != PreviousLeftSignal) || (rightSignal != PreviousRightSignal)) {
    previousMillis = millis();  // First blink starts now
    ledState = true;
    // Turn lights on (or off) to start first blink
    SetLeftSignal(leftSignal);
    SetRightSignal(rightSignal);
  }
  PreviousLeftSignal = leftSignal;
  PreviousRightSignal = rightSignal;

  uint32_t currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    // if the LED is off turn it on and vice-versa:
    ledState = !ledState;

    SetLeftSignal(leftSignal && ledState);
    SetRightSignal(rightSignal && ledState);
  }
}

// Update the state of the left turn signal
void SetLeftSignal(boolean state) {
  static boolean previousState = false;

  if (state != previousState) {
    sendCommand("t14.bco=0");  // Address left blinker
    if (state)
      sendCommand("bt1.pic1=6");   // Turn on
    else
      sendCommand("bt1.pic1=7");   // Turn off
  }
  previousState = state;
}


// Update the state of the left turn signal
void SetRightSignal(boolean state) {
  static boolean previousState = false;

  if (state != previousState) {
    sendCommand("t13.bco=0");  // Address right blinker
    if (state)
      sendCommand("bt1.pic1=6");   // Turn on
    else
      sendCommand("bt1.pic1=7");   // Turn off
  }
  previousState = state;
}

Thank you JohnWasser.
I have attached the picture of the HMI panel where left and right blinker are shown by black bars which turn to orange with the blinking interval. In the case of your code the hazard flash lights button is blinking if activated. Now, blinking of the left and right blinker on the panel are missing and the blink relays are not turned on and off.

Screen 00000.jpg

I want to short up the following working code

Why? The amount of space on the paper that the printed code occupies has no relationship to the amount of time that the code takes to execute, or the correctness of the code.

The general rule of thumb is “If it works, quit f**king with it.”.

It would be easier to help if the arguments to sendCommand() were documented. I recommend you start by declaring ‘const char *’ variables for each of the strings you pass to sendCommand(). The variable names should say what the command does.