General code structure for temp control/display/logging

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.
Thanks, Edward

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
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looks good, but I would remove the 'if (initialized)'; just do this before you enter the endless loop. It saves one nesting level and you always want to keep nesting to a minimum.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good idea, changed.
Edward

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You might want to take several readings for each temp-sensor and average the result.
This would minimize jitter when temp is close to change it's value for next degree.
If your summed up results can be divided by 2 (8, 16... 1024) code produced at assembly level just need to do right shifts and code will be smaller/faster.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm going to look at how much time each cycle takes, worst case with a lot of UART coms, and then fill the spare time with averaging (well, more oversampling to increase resolution) though 1/1024 jitter isn't going to cause large swings in output. For my actual output I'm going to be using semi-fixed point maths to keep things fast and with a high degree of accuracy.

Edward