How to make LEDs blink in my code

Good day

My project is a complete temperature monitor using %x LM35 sensors and displaying the info on 16x2 lcd.

My goal is to make the LEDs blink non stop when my temp has reached max settings.

My code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int CPUPin = 0;       // CPU temp pin
int GPUPin = 1;       // GPU temp pin
int MBDPin = 2;      // Motherbourd temp pin
int HDD1Pin = 3;     // Harddrive 1 temp pin
int HDD2Pin = 4;     // Harddrive 2 temp pin

int CPULED = 6;      // CPULED pin
int GPULED = 7;      // GPULED pin
int MBDLED = 8;     // Motherboard LED pin
int HDD1LED = 9     // Harddrive 1 LED pin
int HDD2LED = 10;  // Harddrive 2 LED pin

int hotCPU = 60;    // CPU max temp
int hotGPU = 85;    // GPU max temp
int hotMBD = 40;    // Motherboard max temp 
int hotHDD1 = 35;  // Harddrive 1 max temp
int hotHDD2 = 35;  // Harddrive 2 max temp

void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);

  pinMode(CPULED, OUTPUT);
  pinMode(GPULED, OUTPUT);
  pinMode(MBDLED, OUTPUT);
  pinMode(HDD1LED, OUTPUT);
  pinMode(HDD2LED, OUTPUT);
}

void loop() {
  {{{{
          // put your main code here, to run repeatedly:
          int value = analogRead(CPUPin);
          lcd.setCursor(0, 1);
          float milivolts = (value / 1024.0) * 5000;
          float celsius = milivolts / 10;
          lcd.clear();
          lcd.print("     C P U:");
          lcd.setCursor(3, 1);
          lcd.print(   celsius);
          lcd.print("  \337C");
          lcd.setCursor(0, 1);
          if (celsius > hotCPU) 
          digitalWrite(CPULED, HIGH);
          else digitalWrite(CPULED, LOW);
          delay(1000);
          
        }

        {
          lcd.clear();

          int value = analogRead(GPUPin);
          float milivolts = (value / 1024.0) * 5000;
          float celsius = milivolts / 10;
          lcd.setCursor(0, 1);
          lcd.clear();
          lcd.print(" GRAPHICS CARD:");
          lcd.setCursor(3, 1);
          lcd.print(   celsius);
          lcd.print("  \337C");
          lcd.setCursor(0, 1);
           if (celsius > hotGPU) 
          digitalWrite(GPULED, HIGH);
          else digitalWrite(GPULED, LOW);
          delay(1000);
        }

        {
          lcd.clear();
          int value = analogRead(MBDPin);
          float milivolts = (value / 1024.0) * 5000;
          float celsius = milivolts / 10;
          lcd.setCursor(0, 1);
          lcd.clear();
          lcd.print("  MOTHERBOARD:");
          lcd.setCursor(3, 1);
          lcd.print(   celsius);
          lcd.print("  \337C");
          lcd.setCursor(0, 1);
           if (celsius > hotMBD) 
          digitalWrite(MBDLED, HIGH);
          else digitalWrite(MBDLED, LOW);
          delay(1000);
        }

        {
          lcd.clear();

          int value = analogRead(HDD1Pin);
          float milivolts = (value / 1024.0) * 5000;
          float celsius = milivolts / 10;
          lcd.setCursor(0, 1);
          lcd.clear();
          lcd.print("500GB HARDDRIVE:");
          lcd.setCursor(3, 1);
          lcd.print(   celsius);
         lcd.print("  \337C");
          lcd.setCursor(0, 1);
          if (celsius > hotHDD1) 
          digitalWrite(HDD1LED, HIGH);
          else digitalWrite(HDD1LED, LOW);
          delay(1000);
        }

        {
          lcd.clear();

          int value = analogRead(HDD2Pin);
          float milivolts = (value / 1024.0) * 5000;
          float celsius = milivolts / 10;
          lcd.setCursor(0, 1);
          lcd.clear();
          lcd.print(" 2TB HARDDRIVE:");
          lcd.setCursor(3, 1);
          lcd.print(  celsius);
          lcd.print("  \337C");
          lcd.setCursor(0, 1);
          if (celsius > hotHDD2) 
          digitalWrite(HDD2LED, HIGH);
          else digitalWrite(HDD2LED, LOW);
          delay(1000);

        }
      }
  }
  }
}
 {
  {{{{

Pretty pointless.

There are blinking LEDs in the demo Several Things at a Time

...R

I'd personally dedicate a timer to it.

I am assuming this is an UNO or an ATMEGA328 based device...but you could alter the code/registers for other ATMEL based ICs.

/* Dedicate Timer2 (8 bit) to toggle an LED.
 * Firstly, we use the 8 bit timer in normal mode. 
 * The timer overflows and calls the ISR(TIMER2_OVF_vect) which incremements
 * "overflows" by 1. 
 * 
 * At 16Mhz divided by 64, an overflow occurs every ~1 millisecond.
 * 
 * The ISR also checks the value of overflows, and if it is greater than a pre-defined value
 * it "flips" the LED state to make it strobe. Set the strobe speed (milliseconds) with
 * "led_period".
 * 
 * If you want the led on, simply set "warning_led_state" to a 1.
 * If you want the led off, simple set "warning_led_state" to a 0.
 * 
 *  
 */

volatile long overflows=0;
volatile long led_period = 100;
volatile boolean led_status = false;
volatile boolean warning_led_state = 0;

#define ledpin 13


void setup() {

pinMode(ledpin,OUTPUT);

  //==================SETTING UP TIMER 2==================//

  TCCR2A = 0;
  TCCR2B = 0;
  TCCR2B |= (1<<CS22); // Divide system clock by 64...
  TIMSK2 =0;
  TIMSK2 |= (1<<TOIE2); // Enable the overflow interrupt
  SREG |= 0b10000000; // Enable Interrupts


}

void loop() {
warning_led_state = 1;
delay(4000);
warning_led_state = 0;
delay(4000);


}

ISR(TIMER2_OVF_vect){
  overflows++;

  if (overflows >= led_period && warning_led_state == 1){
    digitalWrite(ledpin,!led_status);
    led_status = !led_status;
    overflows = 0;
  }
  else if (warning_led_state == 0){
        digitalWrite(ledpin,LOW);
        overflows = 0;
  }
}

This frees up your main() loop if you end up needing delays and whatnot in it.

Hi thank you but i am new to this and want to know how i will add this to my code

Daniel122:
Hi thank you but i am new to this and want to know how i will add this to my code

Look at the thread that Robin linked. Study it, understand it and write your sketch from scratch.

Daniel122:
Hi thank you but i am new to this and want to know how i will add this to my code

Have you carefully studied my link in Reply #2?

I there are aspects that you don't understand then please tell me and I will try to help.

...R