Can't get LED to flash (like an indicator / blinker) in this sketch

Hi Guys,
Relatively new to Arduino but have used the advice from the board many times.
I am however stuck with my current sketch and need some advice please. This is an install in a 1/76 scale bus where bluetooth application sets either the destination (written to the OLED screen) or activates one of the lighting features (headlights, blinkers etc).
The issue - IND_On turns on the blinkers but they are solid, not flashing as required. I can use Delay but this causes a block and I can't do anything else while that action runs. Can Millis be used to complete this request?
My code in it's entirety is below. Any help much appreciated:

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

unsigned long previousMillis = 0;
int ledState = LOW; //initial state of LED

long rememberTime = 0;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// 'Harry_Potter_logo_text (1)', 128x32px
const unsigned char myBitmap [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x0e, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x78, 0x00, 0x20, 0x0e, 0x70, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x1e, 0x30, 0x08, 0x78, 0x0e, 0x70, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x31, 0x1f, 0x7c, 0x0e, 0x70, 0x3e, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x37, 0x9f, 0xec, 0x0e, 0x70, 0x3f, 0xc6, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x37, 0xcf, 0xe8, 0x0e, 0x73, 0x9f, 0xeb, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x32, 0xef, 0x60, 0xef, 0xf7, 0xdd, 0xdb, 0xec, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x30, 0xec, 0x7c, 0xcf, 0xff, 0xdd, 0xd9, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x3f, 0xec, 0x6c, 0xcf, 0xee, 0xd9, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x3f, 0xec, 0x6e, 0x8f, 0x0e, 0xd9, 0xd8, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x4e, 0x77, 0xec, 0x6f, 0x8f, 0x0e, 0xd9, 0xd8, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x2f, 0xf7, 0xec, 0x6f, 0x0f, 0x0e, 0xd9, 0xd8, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x1f, 0x37, 0xec, 0xe7, 0x07, 0x0e, 0xd9, 0xdb, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x33, 0xfe, 0x06, 0x0e, 0x06, 0xd9, 0xce, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x33, 0xc0, 0x06, 0x0e, 0x03, 0xd9, 0xc6, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x30, 0x0e, 0x0c, 0x0e, 0x00, 0x09, 0xc0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x30, 0x0c, 0x0c, 0x0e, 0x00, 0x04, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x30, 0x0f, 0xf8, 0x0e, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x30, 0x03, 0xf8, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0e, 0x70, 0x00, 0x20, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x78, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

String readString;

int state = 0;

void setup() {

  Serial.begin(9600); // Default communication rate of the Bluetooth module
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);

  pinMode(11, OUTPUT); // define LEDpin as output
  digitalWrite(11, LOW); // set initial state

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //or 0x3C
  display.clearDisplay(); //for Clearing the display
  display.drawBitmap(40, 0, myBitmap, 128, 32, WHITE); // display.drawBitmap(x position, y position, bitmap data, bitmap width, bitmap height, color)
  display.display();
}

void loop()

{
  while (Serial.available()) {
    delay(50);
    char c = Serial.read();
    readString += c;
  }
  if (readString.length() > 0) {
    Serial.println(readString);
    if (readString == "Dest_1") {
      display.clearDisplay();
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(40, 8);
      // Display static text
      display.println("Desination 1");
      display.display();
    }

    if (readString == "Dest_2") {
      display.clearDisplay();
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(30, 4);
      // Display static text
      display.println("Aldershot");
      display.display();

      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(24, 12);
      // Display static text
      display.println("via Old Dean");
      display.display();

      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(110, 5);
      // Display static text
      display.println("1");
      display.display();
    }

    if (readString == "Dest_3") {
      display.clearDisplay();
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(24, 8);
      // Display static text
      display.println("Destination 3");
      display.display();

      display.setTextSize(2);
      display.setTextColor(WHITE);
      display.setCursor(98, 4);
      // Display static text
      display.println("99");
      display.display();
    }

    if (readString == "Dest_4") {
      display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //or 0x3C
      display.clearDisplay(); //for Clearing the display
      display.drawBitmap(40, 0, myBitmap, 128, 32, WHITE); // display.drawBitmap(x position, y position, bitmap data, bitmap width, bitmap height, color)
      display.display();
    }

    if (readString == "HL_On") {
      digitalWrite(12, HIGH);
    }

    if (readString == "HL_Off") {
      digitalWrite(12, LOW);
    }

    if (readString == "IND_On")
    {
      digitalWrite(11, (millis() % 500) < 250 ? HIGH : LOW);
    }
    if (readString == "IND_Off") {
      digitalWrite(11, LOW);
    }
  }
  readString = "";
}

But not the advice to use code tags.

Use the message to set a flag that indicates whether or not the blinkers should be blinking, not to try to do the blinking itself, because the message will only compare once.

Then use the blink-without-delay example to conditionally blink the LEDs every iteration of loop ()

1 Like

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

You could have edited the code tags into the original post - it's the same code, isn't it?

Do yourself, and us, a favour, and use the IDE's auto format tool before posting code.

1 Like

Thank you :slight_smile:
I've not used flags at all before, any chance of an example please?
I'm reading through the blink-without-delay code now so seeing how I integrate that :+1:

A flag is just a variable, in this case Boolean, that says that something should or should not be happening.
So, if you receive the message that the blinkers should be on, you set the flag to true
If you receive the message that the blinkers should be off, you set the flag to false.
Each time through loop(), you test the flag to see if it is true, and if so, call the blink-without-delay code for the blinkers.
Please don't name the flag just "flag".

1 Like