Folks this is my first post. I hope I have not broken any of your guidance rules etc - apologies if I have.
I am trying to use an Arduino Uno purchased a few months ago to control a very small (“pico” ie <2kW) hydro generator and turbine supplying a village in Uganda. The scheme uses two induction motors as generators wired with capacitors in a “C-2C” arrangement to give very approximately 200V at very approximately 60Hz. The Arduino controls the system by monitoring grid voltage and switching in or out, via solid state relays, so called “dump loads” (extra resistance in the form of cooking elements) to keep the load on the generators and turbine roughly constant whatever the “consumers”/ villagers, switch on or off.
My problem is that the Arduino seems not to do what I expect. (I am a virgin as far and Arduino and C++ etc are concerned, but I did write a complicated BBC BASIC code ~about 3500 lines - many years ago for my PhD.) So far I have hit four or five problems which have cost me a lot of time:
I thought that I would measure “grid” voltage and frequency by feeding an Arduino analogue port with a full wave rectified but unsmoothed voltage from a step-down transformer and a resistor ladder. I put a 100nF across it to take out the radio-frequencies but there is no 1000uF etc to produce a conventional smooth rectified ac. (The arduino itself is fed from the same transformers via a second full wave bridge, but this time with a large capacitor.) The Arduino goes fast enough, that in one half cycle, it will measure voltage at least 60 times. I thought that it could take these measures and square them and sum them and average to produce a mean-square voltage. The square-root of this would be the root-mean-square of the grid voltage. (Much of the load on the network will be compact fluorescent lightbulbs with odd waveforms so I thought it safer to try to get a true RMS voltage.) And by using the interval “micros()” between corresponding points in adjacent half cycles, the half period of the ac voltage and therefore frequency would be obtained. But some difficulties came my way:
My first approach was to run two while() loops to synchronise with the mains, then simply let the ADC run, taking the readings, squaring them, summing, then averaging, taking the square root and finally multiplying by a fiddle factor to get regular RMS volts. But I got rubbish. Ok second approach: get the thing to write ADC readings into an array and operate on those - this yielded more sensible results - but there was no real success until ADC 10bit output was right shifted twice ie made an 8_bit before squaring. In one of the umpteen versions of this I created another array to hold the Arduino square of the 10bit ADC readings and it was clear that up to 255, squares worked reliably, but above would usually give 0, but not always, and sometimes it was correct.
Integer arithmetic does not seem to work. In the code attached I thought I would divide 500,000 by the half period of the waveform to obtain a frequency. I did not need great precision so I thought integer arithmetic would be good enough. But 500k/20k = 2 (sic) with Arduino rather than the 25 or whatever one might expect. Only if the expected output is a float (obviously) AND one of the inputs (nominator or denominator) is also a float, will a sensible answer be generated at least in the code attached.
if() and else: the loop function has a series of if() statements. At the end originally I had an else, but the Aduino would execute a true if() AND then it would execute the else as well (sic).
constrain(a,b,c) does not seem to work in the loop function: I cannot get a simple test program to fail nor the real function to work!
I will happily concede that I am not top drawer material and that I do not understand the declaration of variables etc in C - particularly passed and returned variables. I used to do this successfully in BBC Basic, but I have had error messages in ARDUINO which defeat me - even when I duplicate (I think perfectly) working code.
Philosophically why is there a setup() function AND the ability to declare and set things before it? I would have thought one could have either all the declarations etc in the setup() or setup() could be superfluous?
So my question is: is this what happens when you let a well-meaning but ignorant amateur loose - are these my errors or are there bugs in the Arduino OS? And what do I do to get code which works?
Any help gratefully received
Colin Oram, Fort Portal, Uganda.