So I was hoping I could get some help with my first serious project.
My project is a temp controlled humidor which uses ADC inputs from several temp sensors and a humidity sensor to control a peltier and some fans via PWM. It also displays data on an LCD and sends it to a PC via a UART to USB bridge.
I plan to have only 2 interrupts enabled, a timer that starts each ADC get and processing and an UART receive interrupt. UART send will be done with in-line delays and completion checks as will ADC.
Below is my general layout/structure for the code and I would appreciate it if you guys and gals could look at it and tell me if it will work and how it could be changed/completely redone to be better.
PS Sorry for writing in LUA but it's what I am most familiar with, but it should be understandable.
function main() while (true) do if (initialized) then if (ADCtime) then getADC() calcPWM() if (on) then if (closed) then setPWM() else --turn on lights, turn off everything but hotside fans, set hotside PWM end elseif (closed) then --turn off everything else --turn on lights, turn off everything else end setLCD() -- queue data for sending to PC if (UARTGotData) then ProcessUARTData() end if (USBOperating) then if (USBEnabled) then UARTSendData() else USBOperating = false end elseif (USBEnabled) then checkUSBOperating() end end else initialiseDevices() getADC() end sleep() end end function timerInterupt() ADCtime=true end function UARTRecieveInterupt() --sets gotUARTData=true and puts data into buffer end function getADC() --gets ADC values, does some initial calcs while waiting and sets ADCtime=false end function calcPWM() --calculates PWM values end function setPWM() --sets PWM for fans/TECs from values calculated in calcPWM() end function setLCD() --displays data on the LCD end function ProcessUARTData() --//transfers UART recieve buffer to a temporary array so it won't get changed while being processed, checks for validity and completeness if not complete do nothing and try again next cycle. If complete sets gotUARTData=false (unless there is another incomplete packet waiting) and does what it's told ie change settings, get notified that USB is operating (USBOperating=true) or maybe in the future switch to bootloader for firmware update. //-- end function UARTSendData() -- sends any data in the send buffer and deletes it. end function checkUSBOperating() -- sends a request to the PC to check if the USB link is working end function initialiseDevices() -- set everything up end