making an led blink with millis

Hi,

I am trying to make a bell controller as in making a bell ring a set pattern. Ever time I run this code to make the led blink and also make the bell ring the lcd screen freezes. I have read that millis will do what I want but I am trying to work it out.

Here is my code that make the led blink and the bell ring.

void bell(){
  digitalWrite(bellPin, HIGH);
  delay(x);
  digitalWrite(bellPin, LOW);
}

void bellx3(){
  bell();
  delay(y);
  bell();
  delay(y);
  bell();
  delay(z);
}

void bellx9(){ // REPEAT THIS 3 TIMES FOR 9 BELLS
  bell();
  delay(y);
  bell();
  delay(y);
  bell();
  delay(y);
}

void angelus() {//set 1 of 3 rings
  bellx3();
  bellx3();
  bellx3();
  bellx9();
  bellx9();
  bellx9();
}

I know its the delay part is causing the lcd to freezes. I would be great full if someone could help me.

I know its the delay part is causing the lcd to freezes.

How do you know that? Normally, LCD displays just maintain their display data while any processing goes on... if you have addtional code that is supposed to change the LCD while the bell is ringing, you should follow the forum guidelines and post your entire sketch. Thanks for using code tags, though...

You need to post your whole code. You mention an LED, a bell and an LCD but we can't really guess as to how the whole thing comes together.

I am sorry I did not post the full code as I was waiting for that pc to do a windows update and seem to be taking a long time. I have the full code now.

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C   lcd(0x27, 16, 2);
RTC_DS3231          rtc;

int bellPin = 10;
//int bellLED = 16;


// Time for hammer to hit bell
int x = 300; // hammer on pules
int y = 2000; // hammer off and wait
int z = 3000; // wait between sets

char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char monthsOfTheYear[12][12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

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

 // initialize digital pin bellLED as an output.
  pinMode(bellPin, OUTPUT);
//  pinMode(bellLED, OUTPUT);

  setupLcd();
  setupRtc();

  // Use this to set the correct time on the RTC module
  // setTime(2021, 3, 18, 22, 58, 0);
}

void loop () {
  DateTime now = rtc.now();

  // Date row
  lcd.setCursor(1, 0);
  lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
  lcd.print(" ");
  lcd.print(now.day() < 10 ? "0" + String(now.day()) : now.day());
  lcd.print(" ");
  lcd.print(monthsOfTheYear[now.month() - 1]);
  lcd.print(" ");
  lcd.print(now.year());

  // Time row
  lcd.setCursor(4, 1);
  lcd.print(now.hour() < 10 ? "0" + String(now.hour()) : now.hour());
  lcd.print(":");
  lcd.print(now.minute() < 10 ? "0" + String(now.minute()) : now.minute());
  lcd.print(":");
  lcd.print(now.second() < 10 ? "0" + String(now.second()) : now.second());

  delay(100);

  if (now.hour() == 12 && now.minute() == 00 && now.second() == 00){
     angelus();
  }
  else if (now.hour() == 18 && now.minute() == 00 && now.second() == 00){
    angelus();

  }
}

void bell(){
  digitalWrite(bellPin, HIGH);
  delay(x);
  digitalWrite(bellPin, LOW);
}

void bellx3(){
  bell();
  delay(y);
  bell();
  delay(y);
  bell();
  delay(z);
}

void bellx9(){ // REPEAT THIS 3 TIMES FOR 9 BELLS
  bell();
  delay(y);
  bell();
  delay(y);
  bell();
  delay(y);
}

void angelus() {//set 1 of 3 rings
  bellx3();
  bellx3();
  bellx3();
  bellx9();
  bellx9();
  bellx9();
}

void setupLcd() {
  // put your setup code here, to run once:
  lcd.init(); // initialize the lcd
  // Print a message to the LCD.
  lcd.backlight();
  //lcd.print("Booting...");
}

void setupRtc() {
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
}

void setTime(int y, int m, int d, int h, int i, int s) {
  Serial.println("Setting time");
  rtc.adjust(DateTime(y, m, d, h, i, s));
}

I am using the same pin as the led is connected to at the moment to make the bell ring via an optocoupler.

Where did you get this syntax from ?

lcd.print(now.hour() < 10 . . .

larryd:
Where did you get this syntax from ?

lcd.print(now.hour() < 10 . . .

I did find some info on the net about showing the time and some trying and I have the clock working fine.

The main problem is when the bell /led flashes the LCD freezes ie time stops and when the bell stops the time jumps and runs fine.

Try this non-blocking code for blinking a number of times. Set blinkCounter >0 to the number of times you want to blink

//https://forum.arduino.cc/index.php?topic=733312.0
// RepeatingMillisDelay.ino

#include <millisDelay.h>
// from SafeString library from Arduino library manager
// see https://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html


int led = 13;
// Pin 13 has an LED connected on most Arduino boards.
bool ledOn = false; // keep track of the led state

millisDelay ledDelay;
unsigned long ledDelay_ms = 500;

millisDelay restartLedBlink;
unsigned long restartLedBlink_ms = 6000ul; // each 10 sec blink 3 times

int blinkCounter = 0;

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(1000);
  }
  Serial.println();

  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);   // initialize the digital pin as an output.
  digitalWrite(led, LOW); // turn led off
  ledOn = false;

  // start delay
  ledDelay.start(ledDelay_ms);
  blinkCounter = 3; // blink only 3 times
  restartLedBlink.start(restartLedBlink_ms);

}

void ledBlink() {
  // check if delay has timed out
  if (ledDelay.justFinished()) {
    ledDelay.repeat(); // start delay again without drift
    if (blinkCounter <= 0) {
      return; // nothing to do
    }
    // toggle the led
    ledOn = !ledOn;
    if (ledOn) {
      digitalWrite(led, HIGH); // turn led on
    } else {
      digitalWrite(led, LOW); // turn led off
      blinkCounter--;
    }
  }
}

void loop() {
  ledBlink(); // call this every loop
  
  // set counter to > 0 to start blinking
  if (restartLedBlink.justFinished()) {
    restartLedBlink.repeat();
    Serial.println(F("restart blink"));
    blinkCounter = 3;
  }
}

Thank you Drmpf for the code. I only got a chance to try it and I had made a few changes to the timing and also number of flashes.

I killed my board by a stupid mistake when connecting the power for the board, I connected 12v to the vcc instead of connecting it to Vin. I have a some more ordered as not the dear to buy.