Hi everyone!
It's my first time connecting an Arduino to a display and working up a GUI. I am using the LVGL library and the GUI setup is mainly working how I intended. However, when I press the "Measure OD." button on the display, the Mbed OS crashes everytime and I am really not sure what is going on. I've put some serial.println commands to see if the functions are executed and they seem to be fine.
The idea is that I want to take readings from a photosensor, which should be iterated in a specified time interval. The measurements should be depicted in the measurementTable and if a pre-defined threshold is exceeded, another part of the code should be executed.
Any ideas?? Thanks in advance! Here is a (big) code snippet of the sketch I am working on:
#include <math.h>
#include <Arduino_H7_Video.h>
#include <lvgl.h>
#include <Arduino_GigaDisplayTouch.h>
const int turbidostatPin = A0;
const int pumpMediumPin = 2;
const int pumpWastePin = 3;
const int numReadings = 20;
const int fixedDiltuionVolume = 5;
const float ODThreshold = 0.400;
const float flowRate = 67.0;
const float fixedFlowTimeInterval = fixedDiltuionVolume / flowRate;
const unsigned long ODInterval = 5000;
int rawIntensity = 0;
int darkRef = 0;
int blankRef = 0;
int sampleRawIntensity = 0;
float ODValue = 0.000;
bool runContinues = false;
unsigned long elapsedMillis = 0;
unsigned long startMillis = 0;
unsigned long previousMillis = 0;
Arduino_H7_Video Display(800, 480, GigaDisplayShield);
Arduino_GigaDisplayTouch TouchDetector;
lv_obj_t * menu;
lv_obj_t * mainPage;
lv_obj_t * pumpSubpage;
lv_obj_t * parametersSubpage;
lv_obj_t * calibrateSubpage;
lv_obj_t * ODSubpage; /
lv_obj_t * rawIntensityLabel;
lv_obj_t * darkRefLabel;
lv_obj_t * blankRefLabel;
lv_obj_t * sampleRawIntensityLabel;
lv_obj_t * ODButtonLabel;
lv_obj_t * timeLabel;
lv_obj_t * ODValueLabel;
lv_obj_t * measurementTable;
void setup() {
Serial.begin(115200);
Serial.println("Program started.");
delay(1000);
analogRead(turbidostatPin);
pinMode(turbidostatPin, INPUT);
pinMode(pumpMediumPin, OUTPUT);
pinMode(pumpWastePin, OUTPUT);
pinMode(pumpIncubationPin, OUTPUT);
pinMode(pumpCuvettePin, OUTPUT);
digitalWrite(pumpMediumPin, LOW);
digitalWrite(pumpWastePin, LOW);
digitalWrite(pumpCuvettePin, LOW);
digitalWrite(pumpIncubationPin, LOW);
delay(1000);
Display.begin();
TouchDetector.begin();
lv_obj_t * screen = lv_obj_create(lv_scr_act());
lv_obj_set_size(screen, Display.width(), Display.height());
menu = lv_menu_create(screen);
lv_obj_set_size(menu, Display.width(), Display.height());
mainPage = lv_menu_page_create(menu, NULL);
lv_obj_set_size(mainPage, Display.width(), Display.height());
lv_menu_set_page(menu, mainPage);
static lv_coord_t colMain[] = { LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST };
static lv_coord_t rowMain[] = { 40, LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST };
lv_obj_t *gridMain = lv_obj_create(mainPage);
lv_obj_set_grid_dsc_array(gridMain, colMain, rowMain);
lv_obj_set_size(gridMain, Display.width(), Display.height());
lv_obj_t *welcomeLabel = lv_label_create(gridMain);
lv_label_set_text(welcomeLabel, "Welcome!");
lv_obj_t *pumpButton = lv_btn_create(gridMain);
lv_obj_set_size(pumpButton, 300, 150);
lv_obj_t *pumpLabel = lv_label_create(pumpButton);
lv_label_set_text(pumpLabel, "Pumps.");
lv_obj_align(pumpLabel, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(pumpButton, pumpButtonClicked, LV_EVENT_CLICKED, NULL);
lv_obj_t *parametersButton = lv_btn_create(gridMain);
lv_obj_set_size(parametersButton, 300, 150);
lv_obj_t *parametersLabel = lv_label_create(parametersButton);
lv_label_set_text(parametersLabel, "Set parameters.");
lv_obj_align(parametersLabel, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(parametersButton, parametersButtonClicked, LV_EVENT_CLICKED, NULL);
lv_obj_t *calibrateButton = lv_btn_create(gridMain);
lv_obj_set_size(calibrateButton, 300, 150);
lv_obj_t *calibrateLabel = lv_label_create(calibrateButton);
lv_label_set_text(calibrateLabel, "Calibrate device.");
lv_obj_align(calibrateLabel, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(calibrateButton, calibrateButtonClicked, LV_EVENT_CLICKED, NULL);
lv_obj_t *startButton = lv_btn_create(gridMain);
lv_obj_set_size(startButton, 300, 150);
lv_obj_t *startLabel = lv_label_create(startButton);
lv_label_set_text(startLabel, "Start run.");
lv_obj_align(startLabel, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(startButton, startButtonClicked, LV_EVENT_CLICKED, NULL);
lv_obj_set_grid_cell(welcomeLabel, LV_GRID_ALIGN_CENTER, 0, 2, LV_GRID_ALIGN_CENTER, 0, 1);
lv_obj_set_grid_cell(pumpButton, LV_GRID_ALIGN_CENTER, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1);
lv_obj_set_grid_cell(parametersButton, LV_GRID_ALIGN_CENTER, 1, 1, LV_GRID_ALIGN_CENTER, 1, 1);
lv_obj_set_grid_cell(calibrateButton, LV_GRID_ALIGN_CENTER, 0, 1, LV_GRID_ALIGN_CENTER, 2, 1);
lv_obj_set_grid_cell(startButton, LV_GRID_ALIGN_CENTER, 1, 1, LV_GRID_ALIGN_CENTER, 2, 1);
Serial.println("Menu created");
delay(2000);
}
void loop() {
lv_task_handler();
delay(5);
unsigned long currentMillis = millis();
if (runContinues == true) {
Serial.println("Continuous OD measurements started");
continuousODMeasurement(currentMillis);
}
}
static void startButtonClicked(lv_event_t * e) {
Serial.println("Start button clicked");
ODSubpage = lv_menu_page_create(menu, "Back");
static lv_coord_t colOD[] = { LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST };
static lv_coord_t rowOD[] = { 100, LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST };
lv_obj_t * gridOD = lv_obj_create(ODSubpage);
lv_obj_set_grid_dsc_array(gridOD, colOD, rowOD);
lv_obj_set_size(gridOD, Display.width(), Display.height());
lv_obj_t * ODButton = lv_btn_create(gridOD);
lv_obj_t * ODButtonLabel = lv_label_create(ODButton);
lv_label_set_text(ODButtonLabel, "Measure OD.");
lv_obj_align(ODButtonLabel, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(ODButton, ODButtonClicked, LV_EVENT_CLICKED, NULL);
lv_obj_align(ODButton, LV_ALIGN_CENTER, 0, 0);
lv_obj_t * measurementTable = lv_table_create(gridOD);
lv_table_set_col_cnt(measurementTable, 3);
lv_table_set_row_cnt(measurementTable, 1);
lv_obj_set_pos(measurementTable, 10, 10);
lv_table_set_col_width(measurementTable, 0, 200);
lv_table_set_col_width(measurementTable, 1, 200);
lv_table_set_col_width(measurementTable, 2, 200);
lv_table_set_cell_value(measurementTable, 0, 0, "Time [s]");
lv_table_set_cell_value(measurementTable, 0, 1, "OD");
lv_table_set_cell_value(measurementTable, 0, 2, "Sample intensity");
lv_obj_set_grid_cell(ODButton, LV_GRID_ALIGN_CENTER, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1);
lv_obj_set_grid_cell(measurementTable, LV_GRID_ALIGN_CENTER, 0, 2, LV_GRID_ALIGN_STRETCH, 1, 1);
lv_menu_set_page(menu, ODSubpage);
}
// Button events
static void ODButtonClicked(lv_event_t * e) {
Serial.println("Measure OD button clicked");
runContinues = true;
}