My Arduino Giga crashes whenever I try to do something when in the mode to set the variables, it crashes. I have tried increasing the time handler delay and using screens inside of hiding and showing flags on objects, but it still crashes when trying to use any of the objects on the screen. I’ve included a majority of the code down below:
#include “Arduino_H7_Video.h”
#include “Arduino_GigaDisplayTouch.h”
#include “lvgl.h”
#include <Arduino_USBHostMbed5.h>
USBHostMSD msd;
mbed::FATFileSystem usb(“usb”);
Arduino_H7_Video Display(800, 480, GigaDisplayShield);
Arduino_GigaDisplayTouch TouchDetector;
lv_obj_t *cont;
lv_obj_t *cont2;
lv_obj_t *LeftCont;
lv_obj_t *FeedListBtn;
lv_obj_t *FeedListlabel;
lv_obj_t *RFIDBtn;
lv_obj_t *RFIDLabel;
lv_obj_t *RightCont;
lv_obj_t *Btn1;
lv_obj_t *Btn2;
lv_obj_t *Btn3;
lv_obj_t *Btn4;
lv_obj_t *btnLabel;
lv_obj_t *FeedList;
lv_obj_t *FeedListCont;
lv_obj_t *ManualAutoBtn;
lv_obj_t *LoadingLabel;
lv_obj_t *Btn1Label;
lv_obj_t *Btn2Label;
lv_obj_t *Btn3Label;
lv_obj_t *Btn4Label;
lv_obj_t *ScanningLabel;
lv_obj_t *BackBtn;
lv_obj_t *BackBtnLabel;
lv_obj_t *TopLeftButton;
lv_obj_t *TopLeftButtonLabel;
lv_obj_t *TopRightButton;
lv_obj_t *TopRightButtonLabel;
lv_obj_t *BottomLeftButton;
lv_obj_t *BottomLeftButtonLabel;
lv_obj_t *BottomRightButton;
lv_obj_t *BottomRightButtonLabel;
lv_obj_t *labelT;
lv_obj_t * sliderT;
lv_obj_t * BottomCont;
lv_obj_t *SetBtn1;
lv_obj_t *SetBtn2;
lv_obj_t *SetBtn3;
lv_obj_t *SetBtn4;
lv_obj_t *SetBtn1Label;
lv_obj_t *SetBtn2Label;
lv_obj_t *SetBtn3Label;
lv_obj_t *SetBtn4Label;
lv_obj_t *ChangeIncBtn;
lv_obj_t *ChangeIncBtnLabel;
lv_obj_t *screen2;
lv_obj_t *value_label;
lv_obj_t *title_label;
lv_obj_t *ManualRunningCont;
lv_obj_t *manual_title_label;
lv_obj_t *manual_value_label;
lv_obj_t *manual_on_label;
lv_obj_t *screen1;
int set1, set2, set3, set4;
int AnimalModeDivider = 10;
float sliderValue2;
float timer;
float Cow1line1, Cow1line2, Cow1line3, Cow1line4;
float Cow2line1, Cow2line2, Cow2line3, Cow2line4;
float Cow3line1, Cow3line2, Cow3line3, Cow3line4;
float Cow4line1, Cow4line2, Cow4line3, Cow4line4;
int CowSelection;
float Weight1, Weight2, Weight3, Weight4;
int scanning = 1;
int FeedMode = 0;
int ManualMode = 0;
int FeedListMode = 0;
int on = 1;
int amounts;
int RFIDMode;
int selected;
int running;
int AnimalMode;
String LabelTextStr;
const char *LabelText;
String LabelTextStr2;
const char *LabelText2;
String LabelTextStr3;
const char *LabelText3;
String LabelTextStr4;
const char *LabelText4;
float TargetWeight = 950;
float CurrentWeight = 422;
int Days = 231;
int DaysIn;
float DailyIntake;
float FeedAmount;
float Percentage = 0.02;
float feedToWeightRatio = .16;
String FeedAmountListStr [800];
static void ManualAutoBtn_event_cb(lv_event_t *e) {
static uint32_t cnt = 1;
lv_obj_t *btn = (lv_obj_t *)lv_event_get_target(e);
lv_obj_t *label = lv_obj_get_child(btn, 0);
if (FeedMode == 0){
FeedMode = 1;
lv_obj_add_flag(FeedListCont, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(RightCont, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(btnLabel, “Manual Mode”);
lv_label_set_text(FeedListlabel, “Start”);
lv_label_set_text(RFIDLabel, “Back”);
lv_obj_set_style_bg_color(cont, lv_color_hex(0x42B15A), LV_PART_MAIN);
ManualMode = 0;
}
if (FeedMode == 1 && ManualMode == 1) {
ManualMode = 0;
lv_label_set_text(btnLabel, “Auto Mode”);
lv_obj_add_flag(RightCont, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(ManualRunningCont, LV_OBJ_FLAG_HIDDEN);
}
else if (FeedMode == 1 && ManualMode == 0){
ManualMode = 1;
lv_obj_clear_flag(RightCont, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(btnLabel, “Manual Mode”);
lv_obj_set_style_bg_color(cont, lv_color_hex(0x42B15A), LV_PART_MAIN);
lv_obj_add_flag(ManualRunningCont, LV_OBJ_FLAG_HIDDEN);
}
}
}
static void RFIDBtn_event_cb(lv_event_t *e) {
if (FeedMode == 1){
if(selected != 0){
lv_obj_clear_flag(ManualAutoBtn, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(title_label, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(value_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(Btn1Label, “Program 1”);
lv_label_set_text(Btn2Label, “Program 2”);
lv_label_set_text(Btn3Label, “Program 3”);
lv_label_set_text(Btn4Label, “Program 4”);
selected = 0;
}
else{FeedMode = 0;
lv_obj_add_flag(ManualRunningCont, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(RightCont, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(btnLabel, “Auto/Manual Feeding”);
lv_label_set_text(FeedListlabel, “Feed List”);
lv_label_set_text(RFIDLabel, “Switch Animal”);
lv_obj_set_style_bg_color(cont, lv_color_hex(0x9c9c9c), LV_PART_MAIN);
}
}
else{
lv_scr_load(screen2);
}
}
static void BackBtn_event_cb(lv_event_t *e) {
lv_scr_load(screen1);
}
static void Btn1_event_cb(lv_event_t *e) {
if (set1 == 0){
LabelTextStr = String(Weight1);
LabelText = LabelTextStr.c_str();
set1 = 1;
}
selected = 1;
lv_obj_add_flag(ManualAutoBtn, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(title_label, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(value_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(title_label, “Program 1”);
lv_label_set_text(value_label, LabelText);
lv_label_set_text(Btn1Label, LabelText);
lv_label_set_text(Btn2Label, “Program 2”);
lv_label_set_text(Btn3Label, “Program 3”);
lv_label_set_text(Btn4Label, “Program 4”);
}
static void Btn2_event_cb(lv_event_t *e) {
if(set2 == 0){
LabelTextStr2 = String(Weight2);
LabelText2 = LabelTextStr2.c_str();
set2 = 1;
}
selected = 2;
lv_obj_add_flag(ManualAutoBtn, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(title_label, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(value_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(title_label, “Program 2”);
lv_label_set_text(value_label, LabelText2);
lv_label_set_text(Btn2Label, LabelText2);
lv_label_set_text(Btn1Label, “Program 1”);
lv_label_set_text(Btn3Label, “Program 3”);
lv_label_set_text(Btn4Label, “Program 4”);
}
static void Btn3_event_cb(lv_event_t *e) {
if(set3 == 0){
LabelTextStr3 = String(Weight3);
LabelText3 = LabelTextStr3.c_str();
set3 = 1;
}
selected = 3;
lv_obj_add_flag(ManualAutoBtn, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(title_label, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(value_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(title_label, “Program 3”);
lv_label_set_text(value_label, LabelText3);
lv_label_set_text(Btn3Label, LabelText3);
lv_label_set_text(Btn2Label, “Program 2”);
lv_label_set_text(Btn1Label, “Program 1”);
lv_label_set_text(Btn4Label, “Program 4”);
}
static void Btn4_event_cb(lv_event_t *e) {
selected = 4;
lv_obj_add_flag(ManualAutoBtn, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(title_label, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(value_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(title_label, “Program 4”);
lv_label_set_text(value_label, LabelText4);
lv_label_set_text(Btn4Label, LabelText4);
lv_label_set_text(Btn2Label, “Program 2”);
lv_label_set_text(Btn1Label, “Program 1”);
lv_label_set_text(Btn3Label, “Program 3”);
}
void setup() {
Serial.begin(115200);
Display.begin();
TouchDetector.begin();
screen1 = lv_obj_create(NULL);
screen2 = lv_obj_create(NULL);
lv_scr_load(screen1);
static lv_coord_t col_dsc = { 370, 370, LV_GRID_TEMPLATE_LAST };
static lv_coord_t row_dsc = { 215, 215, LV_GRID_TEMPLATE_LAST };
cont = lv_obj_create(screen1);
lv_obj_set_grid_dsc_array(cont, col_dsc, row_dsc);
lv_obj_set_size(cont, Display.width(), Display.height());
lv_obj_set_style_bg_color(cont, lv_color_hex(0x000000), LV_PART_MAIN);
lv_obj_center(cont);
cont2 = lv_obj_create(screen2);
lv_obj_set_grid_dsc_array(cont2, col_dsc, row_dsc);
lv_obj_set_size(cont2, Display.width(), Display.height());
lv_obj_set_style_bg_color(cont2, lv_color_hex(0x00A6AF), LV_PART_MAIN);
lv_obj_center(cont2);
static lv_style_t Style10;
lv_style_init (&Style10);
lv_style_set_text_font(&Style10, &lv_font_montserrat_10);
static lv_style_t Style18;
lv_style_init (&Style18);
lv_style_set_text_font(&Style18, &lv_font_montserrat_18);
static lv_style_t Style24;
lv_style_init (&Style24);
lv_style_set_text_font(&Style24, &lv_font_montserrat_24);
static lv_style_t Style36;
lv_style_init (&Style36);
lv_style_set_text_font(&Style36, &lv_font_montserrat_36);
static lv_style_t Style42;
lv_style_init (&Style42);
lv_style_set_text_font(&Style42, &lv_font_montserrat_42);
static lv_style_t Style48;
lv_style_init (&Style48);
lv_style_set_text_font(&Style48, &lv_font_montserrat_48);
lv_obj_t *obj;
lv_obj_t *label;
LeftCont = lv_obj_create(cont);
lv_obj_set_grid_cell(LeftCont, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_STRETCH, 0, 2);
lv_obj_add_flag(LeftCont, LV_OBJ_FLAG_HIDDEN);
RightCont = lv_obj_create(cont);
lv_obj_set_grid_cell(RightCont, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_STRETCH, 0, 2);
lv_obj_add_flag(RightCont, LV_OBJ_FLAG_HIDDEN);
lv_obj_set_style_bg_color(LeftCont, lv_color_hex(0x5F5F5F), LV_PART_MAIN);
lv_obj_set_style_bg_color(RightCont, lv_color_hex(0x5F5F5F), LV_PART_MAIN);
FeedListCont = lv_obj_create(cont);
lv_obj_set_grid_cell(FeedListCont, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_STRETCH, 0, 2);
lv_obj_add_flag(FeedListCont, LV_OBJ_FLAG_HIDDEN);
ManualRunningCont = lv_obj_create(cont);
lv_obj_set_grid_cell(ManualRunningCont, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_STRETCH, 0, 2);
lv_obj_add_flag(ManualRunningCont, LV_OBJ_FLAG_HIDDEN);
lv_obj_set_style_bg_color(ManualRunningCont, lv_color_hex(0x5F5F5F), LV_PART_MAIN);
TopLeftButton = lv_btn_create(cont);
lv_obj_set_grid_cell(TopLeftButton, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_STRETCH, 0, 1);
lv_obj_set_style_bg_color(TopLeftButton, lv_color_hex(0x666666), LV_PART_MAIN);
lv_obj_add_event_cb(TopLeftButton, TopLeftButton_event_cb, LV_EVENT_CLICKED, NULL);
TopLeftButtonLabel = lv_label_create(TopLeftButton);
lv_label_set_text(TopLeftButtonLabel, “Cow 1”);
lv_obj_center(TopLeftButtonLabel);
lv_obj_add_style(TopLeftButtonLabel, &Style36, 0);
TopRightButton = lv_btn_create(cont);
lv_obj_set_grid_cell(TopRightButton, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_STRETCH, 0, 1);
lv_obj_set_style_bg_color(TopRightButton, lv_color_hex(0x666666), LV_PART_MAIN);
lv_obj_add_event_cb(TopRightButton, TopRightButton_event_cb, LV_EVENT_CLICKED, NULL);
TopRightButtonLabel = lv_label_create(TopRightButton);
lv_label_set_text(TopRightButtonLabel, “Cow 2”);
lv_obj_center(TopRightButtonLabel);
lv_obj_add_style(TopRightButtonLabel, &Style36, 0);
BottomLeftButton = lv_btn_create(cont);
lv_obj_set_grid_cell(BottomLeftButton, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_STRETCH, 1, 1);
lv_obj_set_style_bg_color(BottomLeftButton, lv_color_hex(0x666666), LV_PART_MAIN);
lv_obj_add_event_cb(BottomLeftButton, BottomLeftButton_event_cb, LV_EVENT_CLICKED, NULL);
BottomLeftButtonLabel = lv_label_create(BottomLeftButton);
lv_label_set_text(BottomLeftButtonLabel, “Cow 3”);
lv_obj_center(BottomLeftButtonLabel);
lv_obj_add_style(BottomLeftButtonLabel, &Style36, 0);
BottomRightButton = lv_btn_create(cont);
lv_obj_set_grid_cell(BottomRightButton, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_STRETCH, 1, 1);
lv_obj_set_style_bg_color(BottomRightButton, lv_color_hex(0x666666), LV_PART_MAIN);
lv_obj_add_event_cb(BottomRightButton, BottomRightButton_event_cb, LV_EVENT_CLICKED, NULL);
BottomRightButtonLabel = lv_label_create(BottomRightButton);
lv_label_set_text(BottomRightButtonLabel, “Cow 4”);
lv_obj_center(BottomRightButtonLabel);
lv_obj_add_style(BottomRightButtonLabel, &Style36, 0);
ManualAutoBtn = lv_btn_create(LeftCont);
lv_obj_set_size(ManualAutoBtn, LV_PCT(100), LV_PCT(33));
lv_obj_set_style_bg_color(ManualAutoBtn, lv_color_hex(0x2E2E2E), LV_PART_MAIN);
lv_obj_add_event_cb(ManualAutoBtn, ManualAutoBtn_event_cb, LV_EVENT_CLICKED, NULL);
btnLabel = lv_label_create(ManualAutoBtn);
lv_label_set_text(btnLabel, “Manual/Auto Feeding”);
lv_obj_center(btnLabel);
lv_obj_add_style(btnLabel, &Style18, 0);
FeedListBtn = lv_btn_create(LeftCont);
lv_obj_set_size(FeedListBtn, LV_PCT(100), LV_PCT(33));
lv_obj_set_style_bg_color(FeedListBtn, lv_color_hex(0x2E2E2E), LV_PART_MAIN);
lv_obj_add_event_cb(FeedListBtn, FeedListBtn_event_cb, LV_EVENT_CLICKED, NULL);
lv_obj_add_event_cb(FeedListBtn, FeedListBtnPressed_event_cb, LV_EVENT_PRESSED, NULL);
lv_obj_add_event_cb(FeedListBtn, FeedListBtnReleased_event_cb, LV_EVENT_RELEASED, NULL);
FeedListlabel = lv_label_create(FeedListBtn);
lv_label_set_text(FeedListlabel, “Feed List”);
lv_obj_center(FeedListlabel);
lv_obj_add_style(FeedListlabel, &Style18, 0);
RFIDBtn = lv_btn_create(LeftCont);
lv_obj_set_size(RFIDBtn, LV_PCT(100), LV_PCT(33));
lv_obj_set_style_bg_color(RFIDBtn, lv_color_hex(0x2E2E2E), LV_PART_MAIN);
lv_obj_add_event_cb(RFIDBtn, RFIDBtn_event_cb, LV_EVENT_CLICKED, NULL);
RFIDLabel = lv_label_create(RFIDBtn);
lv_label_set_text(RFIDLabel, “Switch Animal”);
lv_obj_center(RFIDLabel);
lv_obj_add_style(RFIDLabel, &Style18, 0);
Btn1 = lv_btn_create(RightCont);
lv_obj_set_size(Btn1, LV_PCT(100), LV_PCT(25));
lv_obj_set_style_bg_color(Btn1, lv_color_hex(0x2E2E2E), LV_PART_MAIN);
lv_obj_add_event_cb(Btn1, Btn1_event_cb, LV_EVENT_CLICKED, NULL);
lv_obj_align_to(Btn1, RightCont, LV_ALIGN_TOP_MID, 0, 0);
Btn1Label = lv_label_create(Btn1);
lv_label_set_text(Btn1Label, “Program 1”);
lv_obj_center(Btn1Label);
lv_obj_add_style(Btn1Label, &Style18, 0);
Btn2 = lv_btn_create(RightCont);
lv_obj_set_size(Btn2, LV_PCT(100), LV_PCT(25));
lv_obj_set_style_bg_color(Btn2, lv_color_hex(0x2E2E2E), LV_PART_MAIN);
lv_obj_add_event_cb(Btn2, Btn2_event_cb, LV_EVENT_CLICKED, NULL);
lv_obj_align_to(Btn2, Btn1, LV_ALIGN_BOTTOM_MID, 0, 112);
Btn2Label = lv_label_create(Btn2);
lv_label_set_text(Btn2Label, “Program 2”);
lv_obj_center(Btn2Label);
lv_obj_add_style(Btn2Label, &Style18, 0);
Btn3 = lv_btn_create(RightCont);
lv_obj_set_size(Btn3, LV_PCT(100), LV_PCT(25));
lv_obj_set_style_bg_color(Btn3, lv_color_hex(0x2E2E2E), LV_PART_MAIN);
lv_obj_add_event_cb(Btn3, Btn3_event_cb, LV_EVENT_CLICKED, NULL);
lv_obj_align_to(Btn3, Btn2, LV_ALIGN_BOTTOM_MID, 0, 112);
Btn3Label = lv_label_create(Btn3);
lv_label_set_text(Btn3Label, “Program 3”);
lv_obj_center(Btn3Label);
lv_obj_add_style(Btn3Label, &Style18, 0);
Btn4 = lv_btn_create(RightCont);
lv_obj_set_size(Btn4, LV_PCT(100), LV_PCT(25));
lv_obj_set_style_bg_color(Btn4, lv_color_hex(0x2E2E2E), LV_PART_MAIN);
lv_obj_add_event_cb(Btn4, Btn4_event_cb, LV_EVENT_CLICKED, NULL);
lv_obj_align_to(Btn4, Btn3, LV_ALIGN_BOTTOM_MID, 0, 112);
Btn4Label = lv_label_create(Btn4);
lv_label_set_text(Btn4Label, “Program 4”);
lv_obj_center(Btn4Label);
lv_obj_add_style(Btn4Label, &Style18, 0);
title_label = lv_label_create(LeftCont);
lv_obj_set_style_bg_color(title_label, lv_color_hex(0x000000), LV_PART_MAIN);
lv_obj_add_style(title_label, &Style24, 0);
lv_label_set_text(title_label, “Program 1”);
lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 30);
lv_obj_add_flag(title_label, LV_OBJ_FLAG_HIDDEN);
value_label = lv_label_create(LeftCont);
lv_obj_set_style_bg_color(value_label, lv_color_hex(0x000000), LV_PART_MAIN);
lv_obj_add_style(value_label, &Style48, 0);
lv_label_set_text(value_label, “0.00”);
lv_obj_align(value_label, LV_ALIGN_CENTER, 0, -120);
lv_obj_add_flag(value_label, LV_OBJ_FLAG_HIDDEN);
manual_title_label = lv_label_create(ManualRunningCont);
lv_obj_set_style_bg_color(title_label, lv_color_hex(0x000000), LV_PART_MAIN);
lv_obj_add_style(manual_title_label, &Style24, 0);
lv_label_set_text(manual_title_label, “Current Weight:”);
lv_obj_align(manual_title_label, LV_ALIGN_TOP_MID, 0, 60); // Add padding
manual_value_label = lv_label_create(ManualRunningCont);
lv_obj_set_style_bg_color(manual_value_label, lv_color_hex(0x000000), LV_PART_MAIN);
lv_obj_add_style(manual_value_label, &Style48, 0);
lv_label_set_text(manual_value_label, “0.00”);
lv_obj_align(manual_value_label, LV_ALIGN_CENTER, 0, -90);
manual_on_label = lv_label_create(ManualRunningCont);
lv_obj_set_style_bg_color(manual_on_label, lv_color_hex(0x000000), LV_PART_MAIN);
lv_obj_add_style(manual_on_label, &Style42, 0);
lv_label_set_text(manual_on_label, “Off”);
lv_obj_align(manual_on_label, LV_ALIGN_CENTER, 0, 50);
BottomCont = lv_obj_create(screen2);
lv_obj_align(BottomCont, LV_ALIGN_CENTER, 0, 125);
lv_obj_set_size(BottomCont, LV_PCT(100), LV_PCT(45));
lv_obj_set_grid_cell(BottomCont, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_STRETCH, 1, 1);
lv_obj_set_style_bg_color(BottomCont, lv_color_hex(0x007076), LV_PART_MAIN);
sliderT = lv_slider_create(screen2);
lv_obj_set_size(sliderT, 350,30);
lv_slider_set_value(sliderT, 0, LV_ANIM_OFF);
lv_obj_align_to(sliderT, screen2, LV_ALIGN_CENTER, 0, -80);
lv_obj_add_event_cb(sliderT, sliderT_event_cb, LV_EVENT_CLICKED, NULL);
lv_obj_set_style_bg_color(sliderT, lv_color_hex(0x444444), LV_PART_INDICATOR);
lv_obj_set_style_opa(sliderT, LV_OPA_100, LV_PART_MAIN);
lv_obj_set_style_bg_color(sliderT, lv_color_hex(0x000000), LV_PART_MAIN);
static lv_style_t style_slider;
lv_style_init(&style_slider);
lv_style_set_bg_color(&style_slider, lv_color_hex(0x58585E));
lv_obj_add_style(sliderT, &style_slider, LV_PART_KNOB);
SetBtn1 = lv_btn_create(BottomCont);
lv_obj_set_size(SetBtn1, LV_PCT(25), LV_PCT(100));
lv_obj_align_to(SetBtn1, BottomCont, LV_ALIGN_BOTTOM_MID, -275, 0);
lv_obj_set_style_bg_color(SetBtn1, lv_color_hex(0x006267), LV_PART_MAIN);
lv_obj_add_event_cb(SetBtn1, SetBtn1_event_cb, LV_EVENT_CLICKED, NULL);
SetBtn1Label = lv_label_create(SetBtn1);
lv_label_set_text(SetBtn1Label, “Program 1”);
lv_obj_center(SetBtn1Label);
lv_obj_add_style(SetBtn1Label, &Style18, 0);
SetBtn2 = lv_btn_create(BottomCont);
lv_obj_set_size(SetBtn2, LV_PCT(25), LV_PCT(100));
lv_obj_align_to(SetBtn2, BottomCont, LV_ALIGN_BOTTOM_MID, -90, 0);
lv_obj_set_style_bg_color(SetBtn2, lv_color_hex(0x006267), LV_PART_MAIN);
lv_obj_add_event_cb(SetBtn2, SetBtn2_event_cb, LV_EVENT_CLICKED, NULL);
SetBtn2Label = lv_label_create(SetBtn2);
lv_label_set_text(SetBtn2Label, “Program 2”);
lv_obj_center(SetBtn2Label);
lv_obj_add_style(SetBtn2Label, &Style18, 0);
SetBtn3 = lv_btn_create(BottomCont);
lv_obj_set_size(SetBtn3, LV_PCT(25), LV_PCT(100));
lv_obj_align_to(SetBtn3, BottomCont, LV_ALIGN_BOTTOM_MID, 95, 0);
lv_obj_set_style_bg_color(SetBtn3, lv_color_hex(0x006267), LV_PART_MAIN);
lv_obj_add_event_cb(SetBtn3, SetBtn3_event_cb, LV_EVENT_CLICKED, NULL);
SetBtn3Label = lv_label_create(SetBtn3);
lv_label_set_text(SetBtn3Label, “Program 3”);
lv_obj_center(SetBtn3Label);
lv_obj_add_style(SetBtn3Label, &Style18, 0);
SetBtn4 = lv_btn_create(BottomCont);
lv_obj_set_size(SetBtn4, LV_PCT(25), LV_PCT(100));
lv_obj_align_to(SetBtn4, BottomCont, LV_ALIGN_BOTTOM_MID, 280, 0);
lv_obj_set_style_bg_color(SetBtn4, lv_color_hex(0x006267), LV_PART_MAIN);
lv_obj_add_event_cb(SetBtn4, SetBtn4_event_cb, LV_EVENT_CLICKED, NULL);
SetBtn4Label = lv_label_create(SetBtn4);
lv_label_set_text(SetBtn4Label, “Program 4”);
lv_obj_center(SetBtn4Label);
lv_obj_add_style(SetBtn4Label, &Style18, 0);
ChangeIncBtn = lv_btn_create(screen2);
lv_obj_set_size(ChangeIncBtn, LV_PCT(25), LV_PCT(35));
lv_obj_align_to(ChangeIncBtn, screen2, LV_ALIGN_TOP_MID, 300, 0);
lv_obj_set_style_bg_color(ChangeIncBtn, lv_color_hex(0x006267), LV_PART_MAIN);
lv_obj_add_event_cb(ChangeIncBtn, ChangeIncBtn_event_cb, LV_EVENT_CLICKED, NULL);
ChangeIncBtnLabel = lv_label_create(ChangeIncBtn);
lv_label_set_text(ChangeIncBtnLabel, “Set Sheep values”);
lv_obj_center(ChangeIncBtnLabel);
lv_obj_add_style(ChangeIncBtnLabel, &Style18, 0);
lv_obj_set_style_radius(ChangeIncBtn, 0, LV_PART_MAIN);
BackBtn = lv_btn_create(screen2);
lv_obj_set_size(BackBtn, LV_PCT(25), LV_PCT(35));
lv_obj_align_to(BackBtn, screen2, LV_ALIGN_TOP_MID, -300, 0);
lv_obj_set_style_bg_color(BackBtn, lv_color_hex(0x006267), LV_PART_MAIN);
lv_obj_add_event_cb(BackBtn, BackBtn_event_cb, LV_EVENT_CLICKED, NULL);
BackBtnLabel = lv_label_create(BackBtn);
lv_label_set_text(BackBtnLabel, “Back”);
lv_obj_center(BackBtnLabel);
lv_obj_add_style(BackBtnLabel, &Style18, 0);
lv_obj_set_style_radius(BackBtn, 0, LV_PART_MAIN);
ScanningLabel = lv_label_create(screen2);
lv_label_set_text(ScanningLabel, “0.00”);
lv_obj_align_to(ScanningLabel, screen2, LV_ALIGN_CENTER, -50, -175);
lv_obj_add_style(ScanningLabel, &Style48, 0);
static lv_style_t style;
lv_style_init(&style);
lv_style_set_border_width(&style, 0);
lv_obj_add_style(cont, &style, 0);
lv_obj_add_style(LeftCont, &style, 0);
lv_obj_add_style(RightCont, &style, 0);
lv_obj_add_style(FeedListCont, &style, 0);
lv_obj_add_style(ManualRunningCont, &style, 0);
lv_obj_add_style(BottomCont, &style, 0);
lv_obj_add_style(cont2, &style, 0);
while (!msd.connect()) {
}
int err = usb.mount(&msd);
if (err) {
while (1)
;
}
FILE *f = fopen(“/usb/numbers.txt”, “r”);
if (!f) {
return;
}
if (fscanf(f, “%f”, &Cow1line1) == 1 && fscanf(f, “%f”, &Cow1line2) == 1 && fscanf(f, “%f”, &Cow1line3) == 1 && fscanf(f, “%f”, &Cow1line4) == 1) {
} else {
}
if ( fscanf(f, “%f”, &Cow2line1) == 1 && fscanf(f, “%f”, &Cow2line2) == 1 && fscanf(f, “%f”, &Cow2line3) == 1 && fscanf(f, “%f”, &Cow2line4) == 1){
} else{}
if ( fscanf(f, “%f”, &Cow3line1) == 1 && fscanf(f, “%f”, &Cow3line2) == 1 && fscanf(f, “%f”, &Cow3line3) == 1 && fscanf(f, “%f”, &Cow3line4) == 1){
} else{}
if ( fscanf(f, “%f”, &Cow4line1) == 1 && fscanf(f, “%f”, &Cow4line2) == 1 && fscanf(f, “%f”, &Cow4line3) == 1 && fscanf(f, “%f”, &Cow4line4) == 1){
} else{}
fflush(stdout);
err = fclose(f);
if (err < 0) {
} else {
err = usb.unmount();
}
for (int i = 1; i < Days; i++){
DailyIntake = CurrentWeight * Percentage;
FeedAmount = DailyIntake;
CurrentWeight += FeedAmount * feedToWeightRatio;
float FeedAmountListFloat[Days];
FeedAmountListFloat[i] = FeedAmount;
FeedAmountListStr[i] = String(FeedAmountListFloat[i], 4);
}
String labelText = “”;
for (int i = 1; i < Days; i++){
String iStr = String(i, 10);
labelText += "Day " + iStr + ": " + FeedAmountListStr[i] + “\n”;
}
FeedList = lv_label_create(FeedListCont);
lv_obj_add_style(FeedList, &Style36, 0);
lv_label_set_text(FeedList, labelText.c_str());
lv_obj_align_to(FeedList, FeedListCont, LV_ALIGN_TOP_LEFT, 0, 0);
}
void loop() {
lv_timer_handler();
delay(150);
if (running == 1){
if (selected == 1 && timer < Weight1){
timer += .1;
delay(100);
String timerStr = String(timer);
lv_label_set_text(value_label, timerStr.c_str());
}
else if (selected == 2 && timer < Weight2){
timer += .1;
delay(100);
String timerStr = String(timer);
lv_label_set_text(value_label, timerStr.c_str());
}
else if(selected == 3 && timer < Weight3){
timer += .1;
delay(100);
String timerStr = String(timer);
lv_label_set_text(value_label, timerStr.c_str());
}
else if (selected == 4 && timer < Weight4){
timer += .1;
delay(100);
String timerStr = String(timer);
lv_label_set_text(value_label, timerStr.c_str());
}
else if (selected != 0){
lv_obj_set_style_bg_color(cont, lv_color_hex(0x42B15A), LV_PART_MAIN);
running = 0;
timer = 0;
}
}
if (!msd.connected()) {
msd.connect();
}
}
static void ChangeIncBtn_event_cb(lv_event_t *e) {
delay(200);
if(AnimalMode == 0){
lv_label_set_text(ChangeIncBtnLabel, “Set cow values”);
AnimalModeDivider = 1;
AnimalMode = 1;
}
else{
lv_label_set_text(ChangeIncBtnLabel, “Set sheep values”);
AnimalModeDivider = 10;
AnimalMode = 0;
}
}
static void SetBtn1_event_cb(lv_event_t *e){
if(sliderValue2 != 0){
Weight1 = sliderValue2;
if (CowSelection == 1){
Cow1line1 = sliderValue2;
}
if (CowSelection == 2){
Cow2line1 = sliderValue2;
}
if (CowSelection == 3){
Cow3line1 = sliderValue2;
}
if (CowSelection == 4){
Cow4line1 = sliderValue2;
}
int err = usb.mount(&msd);
if (err) {
while (1)
;
}
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
FILE *f = fopen(“/usb/numbers.txt”, “w+”);
for (int i = 0; i < 1; i++) {
fflush(stdout);
err = fprintf(f, “%.2f\n”, Cow1line1);
err = fprintf(f, “%.2f\n”, Cow1line2);
err = fprintf(f, “%.2f\n”, Cow1line3);
err = fprintf(f, “%.2f\n”, Cow1line4);
err = fprintf(f, “%.2f\n”, Cow2line1);
err = fprintf(f, “%.2f\n”, Cow2line2);
err = fprintf(f, “%.2f\n”, Cow2line3);
err = fprintf(f, “%.2f\n”, Cow2line4);
err = fprintf(f, “%.2f\n”, Cow3line1);
err = fprintf(f, “%.2f\n”, Cow3line2);
err = fprintf(f, “%.2f\n”, Cow3line3);
err = fprintf(f, “%.2f\n”, Cow3line4);
err = fprintf(f, “%.2f\n”, Cow4line1);
err = fprintf(f, “%.2f\n”, Cow4line2);
err = fprintf(f, “%.2f\n”, Cow4line3);
err = fprintf(f, “%.2f\n”, Cow4line4);
if (err < 0) {
error(“error: %s (%d)\n”, strerror(errno), -errno);
}
}
fflush(stdout);
err = fclose(f);
if (err < 0) {
} else {
err = usb.unmount();
}
lv_label_set_text(ScanningLabel, “Saved!”);
}
}
static void SetBtn2_event_cb(lv_event_t *e){
if(sliderValue2 != 0){
Weight2 = sliderValue2;
if (CowSelection == 1){
Cow1line2 = sliderValue2;
}
if (CowSelection == 2){
Cow2line2 = sliderValue2;
}
if (CowSelection == 3){
Cow3line2 = sliderValue2;
}
if (CowSelection == 4){
Cow4line2 = sliderValue2;
}
int err = usb.mount(&msd);
if (err) {
while (1)
;
}
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
FILE *f = fopen(“/usb/numbers.txt”, “w+”);
for (int i = 0; i < 1; i++) {
fflush(stdout);
err = fprintf(f, “%.2f\n”, Cow1line1);
err = fprintf(f, “%.2f\n”, Cow1line2);
err = fprintf(f, “%.2f\n”, Cow1line3);
err = fprintf(f, “%.2f\n”, Cow1line4);
err = fprintf(f, “%.2f\n”, Cow2line1);
err = fprintf(f, “%.2f\n”, Cow2line2);
err = fprintf(f, “%.2f\n”, Cow2line3);
err = fprintf(f, “%.2f\n”, Cow2line4);
err = fprintf(f, “%.2f\n”, Cow3line1);
err = fprintf(f, “%.2f\n”, Cow3line2);
err = fprintf(f, “%.2f\n”, Cow3line3);
err = fprintf(f, “%.2f\n”, Cow3line4);
err = fprintf(f, “%.2f\n”, Cow4line1);
err = fprintf(f, “%.2f\n”, Cow4line2);
err = fprintf(f, “%.2f\n”, Cow4line3);
err = fprintf(f, “%.2f\n”, Cow4line4);
if (err < 0) {
error(“error: %s (%d)\n”, strerror(errno), -errno);
}
}
fflush(stdout);
err = fclose(f);
if (err < 0) {
} else {
err = usb.unmount();
}
lv_label_set_text(ScanningLabel, “Saved!”);
}
}
static void SetBtn3_event_cb(lv_event_t *e){
if(sliderValue2 != 0){
Weight3 = sliderValue2;
if (CowSelection == 1){
Cow1line3 = sliderValue2;
}
if (CowSelection == 2){
Cow2line3 = sliderValue2;
}
if (CowSelection == 3){
Cow3line3 = sliderValue2;
}
if (CowSelection == 4){
Cow4line3 = sliderValue2;
}
int err = usb.mount(&msd);
if (err) {
while (1)
;
}
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
FILE *f = fopen(“/usb/numbers.txt”, “w+”);
for (int i = 0; i < 1; i++) {
fflush(stdout);
err = fprintf(f, “%.2f\n”, Cow1line1);
err = fprintf(f, “%.2f\n”, Cow1line2);
err = fprintf(f, “%.2f\n”, Cow1line3);
err = fprintf(f, “%.2f\n”, Cow1line4);
err = fprintf(f, “%.2f\n”, Cow2line1);
err = fprintf(f, “%.2f\n”, Cow2line2);
err = fprintf(f, “%.2f\n”, Cow2line3);
err = fprintf(f, “%.2f\n”, Cow2line4);
err = fprintf(f, “%.2f\n”, Cow3line1);
err = fprintf(f, “%.2f\n”, Cow3line2);
err = fprintf(f, “%.2f\n”, Cow3line3);
err = fprintf(f, “%.2f\n”, Cow3line4);
err = fprintf(f, “%.2f\n”, Cow4line1);
err = fprintf(f, “%.2f\n”, Cow4line2);
err = fprintf(f, “%.2f\n”, Cow4line3);
err = fprintf(f, “%.2f\n”, Cow4line4);
if (err < 0) {
error(“error: %s (%d)\n”, strerror(errno), -errno);
}
}
fflush(stdout);
err = fclose(f);
if (err < 0) {
} else {
err = usb.unmount();
}
lv_label_set_text(ScanningLabel, “Saved!”);
}}
static void SetBtn4_event_cb(lv_event_t *e){
if(sliderValue2 != 0){
Weight4 = sliderValue2;
if (CowSelection == 1){
Cow1line4 = sliderValue2;
}
if (CowSelection == 2){
Cow2line4 = sliderValue2;
}
if (CowSelection == 3){
Cow3line4 = sliderValue2;
}
if (CowSelection == 4){
Cow4line4 = sliderValue2;
}
int err = usb.mount(&msd);
if (err) {
while (1)
;
}
mbed::fs_file_t file;
struct dirent *ent;
int dirIndex = 0;
int res = 0;
FILE *f = fopen(“/usb/numbers.txt”, “w+”);
for (int i = 0; i < 1; i++) {
fflush(stdout);
err = fprintf(f, “%.2f\n”, Cow1line1);
err = fprintf(f, “%.2f\n”, Cow1line2);
err = fprintf(f, “%.2f\n”, Cow1line3);
err = fprintf(f, “%.2f\n”, Cow1line4);
err = fprintf(f, “%.2f\n”, Cow2line1);
err = fprintf(f, “%.2f\n”, Cow2line2);
err = fprintf(f, “%.2f\n”, Cow2line3);
err = fprintf(f, “%.2f\n”, Cow2line4);
err = fprintf(f, “%.2f\n”, Cow3line1);
err = fprintf(f, “%.2f\n”, Cow3line2);
err = fprintf(f, “%.2f\n”, Cow3line3);
err = fprintf(f, “%.2f\n”, Cow3line4);
err = fprintf(f, “%.2f\n”, Cow4line1);
err = fprintf(f, “%.2f\n”, Cow4line2);
err = fprintf(f, “%.2f\n”, Cow4line3);
err = fprintf(f, “%.2f\n”, Cow4line4);
if (err < 0) {
error(“error: %s (%d)\n”, strerror(errno), -errno);
}
}
fflush(stdout);
err = fclose(f);
if (err < 0) {
} else {
err = usb.unmount();
}
lv_label_set_text(ScanningLabel, “Saved!”);
}
}
static void sliderT_event_cb(lv_event_t *e){
float sliderValue = lv_slider_get_value(sliderT);
sliderValue2 = (sliderValue/AnimalModeDivider);
String sliderValueStr = String(sliderValue2);
lv_label_set_text(ScanningLabel, sliderValueStr.c_str());
}