I'm somewhat new to Arduino and programming, but have had experience for 6 months of trial and error and research. I have a project that has a lot of parts and a lot of programming and my program has stopped working, I think i know whats wrong, but I'm not sure how to fix it.
My project in short is an arduino uno with a GLCD and sensors with a complicated menu system.
Data comes the sensors go to the GLCD and with buttons to go through various menus. I'm going to post my code a little stripped down without all the print commands to save space and I'll also exclude all the sensor codes too.
/*00000000 Libraries included 00000000*/
#include <SerialGraphicLCD.h>
#include <SoftwareSerial.h>
#include "DHT.h"
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP183.h>
#include <DS3234.h>
/*00000000 GLCD screen size 00000000*/
#define maxX 127//159
#define maxY 63 //127
/*00000000 BMPsensor 00000000*/
#define BMP183_CLK 13 // AKA CLK
#define BMP183_SDO 12 // AKA MISO
#define BMP183_SDI 11 // AKA MOSI
#define BMP183_CS 4
Adafruit_BMP183 bmp = Adafruit_BMP183(BMP183_CLK, BMP183_SDO, BMP183_SDI, BMP183_CS);
volatile float pressure_mmHg;
volatile float pressurepascal;
volatile float Altitude;
float last_microsBMP;
/*00000000 DeadOn time chip 00000000*/
volatile int TimeDate [7]; //second,minute,hour,null,day,month,year
const int CSpin=10;
const int MOSIpin=11;
const int MISOpin=12;
const int CLKpin=13;
float last_microsTime;
/*00000000 DHT sensor stuff 00000000*/
#define DHTPIN 9 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
volatile float humidity=0;
volatile float airtemp=0;
volatile float heatindex=0;
volatile float last_microsDHT;
/*00000000 variables for debounce 00000000*/
long debouncing_time = 250; //Debouncing delay coefficent
long last_micros;
int state = LOW;
/*00000000 variables for menus 00000000*/
int optionspotx=0;
int optionspoty=0;
int enterbutton=0;
int backbutton=0;
int BUTTONLEFT=false;
int BUTTONRIGHT=false;
int BUTTONENTER=false;
int BUTTONBACK=false;
int BUTTONEXIT=false;
int BUTTON10=false;
int menustack[6]={1,0,0,0,0,0}; //array acting as stack for menu porder when hopping from one menu to the other.
int currentmenuspot=1; // set array size to number of menus desired
int prevmenuspot=1;
int menustackspot=0;
int menunumber;
volatile long BUTTON_micros=0;
int SELECTORSPOT=0;
/*00000000 LCD class declaration 00000000*/
LCD LCD;
/*00000000 CoSensor declaration 00000000*/
volatile int CoCode;
volatile int CoLevel;
float last_microsCO;
/*00000000 SETUP 00000000*/
void setup()
{
pinMode(5, INPUT_PULLUP); //button on pin 5
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
attachInterrupt(0, debounce, FALLING); //interrupt
// pinMode(ledPin13, OUTPUT); // pin 13 on board LED
RTC.configure(MOSIpin,MISOpin,CLKpin,CSpin);
// RTC.setDateTime(12,8,2014,23,25,0); // RTC.setDateTime(int d, int mo, int y, int h, int mi, int s);
dht.begin();
bmp.begin();
//RTC.setDateTime(24, 10, 2014, 23, 17, 0); // int d, int mo, int y, int h, int mi, int s
//splash screen
LCD.setBacklight(20);
LCD.clearScreen();
LCD.setHome();
LCD.printStr(" Program starts in "); //displays "program is starting in 5/4/3/2/1 etc"
for(int i = 5; i >= 0; i--)
{
LCD.setX(113);
LCD.setY(0);
LCD.printNum(i);
delay(500);
}
LCD.clearScreen();
delay(200);
}
/*00000000 LOOP 00000000*/
void loop()
{
SensorUpdate(); //update all sensors including time
menu_start();
//MainMenuDisplay();
//menu_select();
//sensor_menu();
//time_menu();
}
void debounce()
{
if(((long)(micros() - last_micros) >= debouncing_time * 1000) || last_micros==0)
{
state = !state;
//digitalWrite(13, state);
last_micros = micros();
buttontest();
menu_button_action_reader();
}
}
void buttontest()
{
if(digitalRead(5)==LOW)
{BUTTONLEFT=true;}
if(digitalRead(6)==LOW)
{BUTTONRIGHT=true;}
if(digitalRead(7)==LOW)
{BUTTONENTER=true;}
if(digitalRead(8)==LOW)
{BUTTONBACK=true;}
if(digitalRead(9)==LOW)
{BUTTONEXIT=true;}
if(digitalRead(10)==LOW)
{BUTTON10=true;}
}
void menu_start()
{
findsmenuspot(); //function call to get what current position is. Sends array to check it
switch(currentmenuspot) //based on result opens the a menu.
{case 1: MainMenuDisplay();
break;
case 2: /*sensor_menu()*/;
break;
case 3: /*time_menu()*/;
break;
case 4: /*Text()*/;
break;
}
BOXSELECTOR();
}
/*00000000000000000000 Previous menu function 0000000000000000000000000*/
int prevmenu () //function to find what previous menu is
{
for(int c=0; menustack[c]!=0; c++ ) //scans from array[0] until it finds a 0
{
currentmenuspot=menustack[c-1]; //saves number which is the menu to variable to be tested
}
if (menustack[currentmenuspot-1] !=1) //checks to make sure youre not at menustack[0]
{prevmenuspot=menustack[currentmenuspot-1];} //shifts variable by 1 to before array spot
switch(prevmenuspot) //uses variable and lanuches previous menu
{
case 1:
MainMenuDisplay();
break;
case 2:
sensor_menu();
break;
case 3:
time_menu();
break;
case 4:
Text();
}
}
/*00000000000 menu stuff 00000000000000000000000000*/
void findsmenuspot()
{
for(int c=0; menustack[c]!=0; c++ ) //scans array by starting from beginning and looks for a zero. Zero mean no menu was set and is top of stack
{currentmenuspot=menustack[c];} //puts number representing which menu into variable for testing
}
int menuspotmake(int menunumber) //updates array with current menu
{
for(int c=0; menustack[c]!=0; c++ ) //scans array by starting from beginning and looks for a zero. Zero mean no menu was set and is top of stack
{
menustackspot=c;
} //puts number representing which menu into variable for testing
if(menustack[menustackspot]!=menunumber)
{
menustack[menustackspot+1]=menunumber;
}
}
/*0000000000000000 menus 0000000000000000*/
void BOXSELECTOR()
{
switch(SELECTORSPOT) //based on result opens function that decides what buttons do in that menu
{case 0:/*The Matrix.*/;
break;
case 1:
LCD.drawBox(4,53,0,47,0);//x1,y1,x2,y2 draw box on S Sensor
LCD.drawBox(3,52,1,48,0);
LCD.drawBox(2,51,2,49,0);
//delay(50);
;
break;
case 2: ;
break;
case 3: ;
break;}
}
void menu_button_action_reader()//reads the current menu it is in
{
switch(currentmenuspot) //based on result opens function that decides what buttons do in that menu
{case 1: MainMenuButtonAction();
break;
case 2: sensor_menuButtonAction();
break;
case 3: time_menuButtonAction();
break;
case 4: TextButtonAction();
break;}
}
void MainMenuButtonAction()
{
if(BUTTONLEFT==true)
{
SELECTORSPOT=0;
//currentmenuspot=2;
/*action*/;
}
else if(BUTTONRIGHT==true)
{
SELECTORSPOT=1;
//currentmenuspot=2;
/*action*/;
}
else if(BUTTONENTER==true)
{
//SELECTORSPOT=0;
//currentmenuspot=3;
/*action*/;
}
else if(BUTTONBACK==true)
{
//SELECTORSPOT=0;
/*action*/;
}
else if(BUTTONEXIT==true)
{
//SELECTORSPOT=0;
;
}
else if(BUTTON10==true)
{
/*action*/;
}
}
My program stops and gets stuck in a function and never calls other functions and/maybe never gets back to void loop() I believe my uC menustack gets full and cant get back. When I press a button the sequence of functions are as follows
inside Void loop() is menu_start() -> interrupts void debounce() -> buttontest() -> void debounce() -> menu_button_action_reader() -> MainMenuButtonAction() -> if nest within MainMenuButtonAction()
The if nest has options that checks button pushes where one button puts a blinking box and another option that turns the box off. The program goes through all the functions correctly and turns the box on and then off once and then gets stuck the screen and sensors still work and new info is sent and displayed on the screen but no other button pushes register and the box function stops working. Is the uC menu stack overflowing? is it the if nest or switch case? If my code is too complicated or needs explain please say so. I researched the forum and found that there is no limit to the uC stack is that correct? Did i fill up the RAM?