The board is functioning properly after uploading the sketch: the relays are properly triggered depending on the conditions. However I had issue with the LEDs consuming too much current vs what the Attiny can deliver to both the coils and the LEDs. I should have added some transistors in between to offload that to the power supply.
Not a big deal though, after removing the min/max LEDs it still works perfectly. I will just use the sound of the fan and relays clicking as an indicator instead.
Now onto fine tuning the programming part:
edit: the steps below have been now replaced by a simple Vpp measurement. It's more than enough for what we want to achieve here. The sketch named Measurement2.ino has been used to display the ADC Vpp values we need.
What needs to be adjusted is the sampling phase: In order to calculate the RMS value we need to measure the sine wave current at regular intervals. Each measurement is squared and added to the previous sum. The 3 lines below do that:
currentSampleRead = analogRead(currentAnalogInputPin) currentSampleSum = currentSampleSum + sq(currentSampleRead) ; currentSampleCount = currentSampleCount + 1;
Using the timestamps of the serial output I found that this set of instructions takes about 180uS to be executed. I need a measurement every 1ms, so I will add a 820uS delay in the loop.
When a certain amount of measurement as been reached, we can calculate the mean and root of it, giving us the TRMS value.
The testing phase is a bit repetitive with the Attiny85 though. I'm using an Arduino as ISP in the middle and the process goes as following:
- Upload ArduinoISP sketch to the Arduino Uno
- Change the board to Attiny and programmer to Arduino as ISP
- Change wiring from Arduino to Attiny
- Upload Sketch
- Change board to Arduino Uno and programmer back to ArduinoISP
- Upload empty Sketch to Arduino
- Change wiring to serial TX/RX
- Launch Serial Monitor
- Debug, adjust Attiny .ino file