I have tested the library and LCD with simple script and everything worked fine. Now I am trying to add the LCD functionality to my working program but the code reaches the lcd.begin(); function it causes a runtime error and no following lines are executed. I have tried many different configurations and none of them have worked; including have lcd.begin() in the setup(), in the InitSM(), loop(), and RunSM() and it crashes every time.
I am wondering if I am running into a similar issue described here.
Following thebusybee's advice, I started with a simple working script that could print to the LCD screen. I then started adding initializing functions in the setup until it stopped working. InitSM(&AFC); is the line that causes the LCD functions to stop working. InitSM(&AFC); calls millis(), enables watchdog timer interrupts, and external interrupt on digital pin 2. Below is the exam code.
// Library
#include <Arduino.h>
#include <EventTimers.h>
#include <EventCheckers.h>
#include <avr/sleep.h>
#include <Timeout.h>
#include <AFCSM.h>
#include <DoorServo.h>
#include <EventTypes.h>
#include <HardwareInterrupts.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
//LiquidCrystal_I2C *lcdA; //0x27,20,4
//lcdA = &lcd;
//lcdA->begin();
// global variables
static uint8_t TimerCheck = 0;
static uint8_t Timer30;
static EventType LCDButton;
static EventType BatteryEvents;
static EventType BumperEvents;
static EventType FogEvents;
static EventType CurrentEvent;
static EventType FrontDoor;
static EventType BackDoor;
static AFCDATA AFC;
static AFCState PreviousState;
void setup() {
// set up serial communication with computer
Serial.begin(9600);
// uart for MiniOFS to MCU
Serial1.begin(1200, SERIAL_8N1);
/*** set up interrupts, not init ***/
SetUpWatchdogT(); // 8sec interrval counter for 32sec environ sensor check
SetUpButtonInt(); // external button interrupt for LCD screen
// init LCD
lcd.begin();
// Enable interrupts
sei();
// Set the sleep mode to Power-Down
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// initi doors
initDoor();
// init statemachine
InitSM(&AFC);
PreviousState = AFC.State;
}
void loop() {
Serial.flush();
lcd.clear();
// lcd.setCursor(2,0); //Set cursor to character 2 on line 0
lcd.print("LED INIT!");
Serial.println("printed");
}
ISR(WDT_vect) {
// Add to timer
TimerCheck += 1;
// check if 30secs have elapsed time has
if (TimerCheck >= 3){
TimerCheck = 0; // reset counter
Timer30 = true; // raise flag
}
}
void Buttonisr(){
// Button Flag
LCDButton = BUTTON_ON;
}
I narrowed it down to being the external interrupt enable on digital pin 2. Below are the functions for the setup and enable of the external interrupt.
void SetUpButtonInt(){
// set up the pin as an input and attach a isr
pinMode(ButtonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ButtonPin), Buttonisr, RISING); // interrupt on rising edge of pin
return;
}
void EnableButtonInt(){
EIMSK |= (1 << INT0); // enable external interrupt
Serial.println("enable external interrupt");
return;
}
How can this setup affect the lcd I2C setup?
lcdA->begin();to crash your program, PLEASE write a minimized sketch just with this initialization insetup()and an experimental output. I don't think anyone will rebuild your full hardware setup and debug the complete thing. Investigate step by step. Please [edit] your question and add this minimal sketch and its outcome. – the busybee May 07 '23 at 18:49lcd.begin()may well block for longer than that since the begin method if filled with delay() statements. Just for a test, replacelcd.begin()with delay(1000) to see if you get the same symptoms. Where, incidentally, is this defined:SetUpWatchdogT()– 6v6gt May 08 '23 at 04:25