Blink led no working serial available

I made a remote car, the code below works well but the problem is that the LED blinker doesn't work and goes silent when it's on. What is wrong?

int in1 = 2;
int in2 = 3;
int in3 = 4;
int in4 = 5;

int lightl = 8;
int lightr = 10;
int seinl = 9;
int seinr = 11;

unsigned long previousMillis = 0;    // will store last time LED was updated
const long interval = 500;           // interval at which to blink (milliseconds)
int ledState = HIGH;                  // ledState used to set the LED
boolean state = false;

unsigned long previousMillis2 = 0;    // will store last time LED was updated
const long interval2 = 500;           // interval at which to blink (milliseconds)
int ledState2 = HIGH;      
boolean state2 = false;

void setup() {
  Serial.begin(9600);

  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
  pinMode(lightl, OUTPUT);
  pinMode(lightr, OUTPUT);
  pinMode(seinl, OUTPUT);
  pinMode(seinr, OUTPUT);

  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  
  digitalWrite(lightl, HIGH);
  digitalWrite(lightr, HIGH);
  digitalWrite(seinl, HIGH);
  digitalWrite(seinr, HIGH);
}

void loop() {
  if (Serial.available()) {
    int serialinput = Serial.read();
    switch (serialinput) {
      // Your existing case statements...
      case 'a':
        //Serial.print("forward");
        digitalWrite(in1, HIGH);  //HIGH
        digitalWrite(in2, LOW);   //LOW
        digitalWrite(in3, HIGH);  //HIGH
        digitalWrite(in4, LOW);   //LOW
        break;
      case 'b':
        //Serial.print("rev");
        digitalWrite(in1, LOW);   //LOW
        digitalWrite(in2, HIGH);  //HIGH
        digitalWrite(in3, LOW);   //LOW
        digitalWrite(in4, HIGH);  //HIGH
        break;
      case 'c':
        //Serial.print("left");
        digitalWrite(in1, LOW);   //LOW
        digitalWrite(in2, HIGH);  //HIGH
        digitalWrite(in3, LOW);   //LOW
        digitalWrite(in4, LOW);   //LOW
        break;
      case 'd':
        //Serial.print("right");
        digitalWrite(in1, LOW);   //LOW
        digitalWrite(in2, LOW);   //LOW
        digitalWrite(in3, LOW);   //LOW
        digitalWrite(in4, HIGH);  //HIGH
        break;
      case 'e':
        //Serial.print("stop");
        digitalWrite(in1, LOW);
        digitalWrite(in2, LOW);
        digitalWrite(in3, LOW);
        digitalWrite(in4, LOW);
        break;
      case 'f':
        digitalWrite(lightl, HIGH);
        digitalWrite(lightr, HIGH);
        break;
      case 'g':
        digitalWrite(lightl, LOW);
        digitalWrite(lightr, LOW);
        break;
      case 'h':
        digitalWrite(seinl, HIGH);
        digitalWrite(seinr, HIGH);
        break;
      case 'i':
        digitalWrite(seinl, LOW);
        digitalWrite(seinr, HIGH);
        state = true;
        break;
      case 'j':
        digitalWrite(seinr, HIGH);
        digitalWrite(seinl, HIGH);
        break;
      case 'k':
        digitalWrite(seinr, LOW);
        digitalWrite(seinl, HIGH);
        state2 = true;
        break;
    }
    Serial.println(serialinput);
    
    if (state == true) {
      Blink();
    } else {
      digitalWrite(seinl, HIGH); // check to make sure it's off incase it was still on
    }

    if (state2 == true) {
      Blink2();
    } else {
      digitalWrite(seinr, HIGH); // check to make sure it's off incase it was still on
    }
  }
}

void Blink() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == HIGH) {
      ledState = LOW;
    } else {
      ledState = HIGH;
    }
    digitalWrite(seinr, ledState);
  }
}

void Blink2() {
  unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >= interval2) {
    previousMillis2 = currentMillis2;
    if (ledState2 == HIGH) {
      ledState2 = LOW;
    } else {
      ledState2 = HIGH;
    }
    digitalWrite(seinr, ledState2);
  }
}

In order to work, this needs to happen on every loop. As it is you have it inside the if that checks for Serial.available. So it can only time out and turn the led off if you send more serial data.

Hello bimosora

Check the logical correct arrange of {} brackets.

So I have to put it outside if, okay

Put them outside

if (Serial.available()) { ... }

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