Blinking LED without delay

I am trying to make an LED blink without using the delay function. I copied some code from the internet but it doesn't work for the LED that I want. See the code below. On line 150 the light doesn't blink when it's supposed to, instead it stays off. It does however work for the LED on line 158, so I'm not sure what the problem is. If I don't make the lights blink and instead have them turn solid on and off, they work as expected. Thanks.

int ledBlink = HIGH;  // ledState used to set the LED
bool tested = true; 
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;  // will store last time LED was updated

// constants won't change:
const long interval = 250;  // interval at which to blink (milliseconds)

void test() {
  digitalWrite(7,HIGH);
  digitalWrite(2,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);
  digitalWrite(12,HIGH);
  for (int i=0; i <=510;i++ ){
    int j = 0;
    if (i <= 255){
      j=i;
      if (i==255){
        delay(2000);
      }
    } else {
      j = 510 - i; 
    }
    analogWrite(3,j);
    analogWrite(6,j);
    analogWrite(5,j);
    delay(10);
  }  
  digitalWrite(7,LOW);
  digitalWrite(2,LOW);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(12,LOW);
  delay(1500);
  tested = true;
  digitalWrite(7,HIGH);
}
String receive_buffer;
void setup() {
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  digitalWrite(7,HIGH);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(12, OUTPUT); 
  pinMode(13,INPUT);
  test();
  }
int lastReceive = 0;
//int cpuAlarm = 0;
void loop() {
  unsigned long 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 (ledBlink == LOW) {
      ledBlink = HIGH;
    } else {
      ledBlink = LOW;
    }}

  String results[5];
  String message;
  int cpuUsage = 0;
  int cpuTemp = 0;
  int gpuUsage = 0;
  int gpuTemp = 0;
  int ramUsage = 0;
  
  bool messageDecoded;
  int j = 0;
  int k = 0;
  static unsigned long x = 0;
  static byte i=0;
  if (Serial.available() > 0)
  {
    message = Serial.readString();;
    j = 0;
    k = 0;
    messageDecoded = false;
    digitalWrite(2,LOW);
    
    if (messageDecoded == false)
    {
      lastReceive = 0;
      while (k < message.length())
      {
          if(message.charAt(k) == '|')
          {
            j++;
            k++;
          }
          else
          {
            results[j] = results[j] + message.charAt(k);
            k++;
          }
      }

    messageDecoded = true;
    cpuUsage = results[0].toInt();
    cpuTemp = results[1].toInt();
    gpuUsage = results[2].toInt();
    gpuTemp = results[3].toInt();
    ramUsage = results[4].toInt();
    analogWrite(3,2.55 * cpuTemp);
    analogWrite(5,gpuTemp*2.55);
    analogWrite(6,ramUsage*2.55);
    if (cpuUsage > 95)
      {
      digitalWrite(8,HIGH);
      } else 
      {
        digitalWrite(8,LOW);
      }   
    if (gpuUsage > 95)
      {
      digitalWrite(9,HIGH);
      } else 
      {
        digitalWrite(9,LOW);
      }   
    if (cpuTemp > 85)
      {
      digitalWrite(10,ledBlink);
      //cpuAlarm++;
      } else 
      {
        digitalWrite(10,LOW);
      //cpuAlarm = 0;
      }
      /*if (cpuAlarm > 3){
        digitalWrite(13,HIGH);
        delay(500);
        digitalWrite(13,LOW);
      } */
    if (gpuTemp > 20)
      {
        digitalWrite(12,ledBlink); //This light doesn't blink, instead it stays off. If I replace ledBlink with HIGH, it turns on when expected. 
      } else
      {
        digitalWrite(12,LOW);
      }        
    
    }
  } else if (lastReceive>5000) {
    digitalWrite(2,ledBlink); //This works as expected: the LED blinks. 
  } else {
    lastReceive++;
  }

}

In your IDE go to file/examples/digital "blink without delay" Start there.

Try post #21 of how to delay using milli() for Flow Chart approach and post #44 @J-M-L for FSM approach

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