I'm back. I appreciate the comments and realise it would be easier and simpler to do this with analogue components, but I really want to give this a go.
I have the beginnings of a sketch here but before I post it, I'll provide some background information.
I have a 1kohm pot connected to A0, this will be my demand.
A voltage divider circuit is as follows: the generator output is full wave (bridge rectified) rectified to produce an equivalent DC voltage. This is then fed into a voltage divider circuit for the Arduino to measure. The nominal output voltage is 100VAC, which would rectify to give approximately 140VDC after diode drops, but I want some upper limit slack. For convenience I want 133.33VAC to equate to 1023 and 0VAC to equate to 0VAC; this means that the nominal 100VAC will read as 767 (three quarters of the range). The voltage divider circuit is thus designed to take:
(133.33VAC * 1.414) - 2 = 186.67VDC (the -2 is the diode drops).
This 186.67VDC is then divided so that at this voltage, 5VDC can be read by the Arduino. The voltage divider circuit therefore consists of a 36kohm, a 330ohm and a 1kohm resistor in series, with the Arduino across the 1kohm. I think this is convenient because a direct comparison can then be made with the voltage across the 1kohm resistor and the 1kohm pot with no need for scaling one to the other. This results in 3.75VDC being read by the Arduino when the generator is at 100VAC. These resistor values can be increased to Mohm if required for additional safety.
A power MOSFET will be acted upon to regulate the generator field strength via PWM to it's gate.
Three LEDs will light up under various conditions, red, green and blue.
Now that the two inputs are established, I'd like to compare them and act appropriately. Say that the pot is variable one and the output is variable two, if variable 1 is greater than variable two, I want the PWM function to increment, increasing the field strength. Vice versa, if variable two is greater than variable one, I want the PWM to decrement. Whenever the PWM is operating, I want the green LED to flash (it will flash all the time since PWM operations will be constantly running). Additionally I want the red LED to flash if variable 2 is 1VAC (8 samples) above variable one, and go solid if 110VAC or higher is read (>=844). Similarly I want the blue LED to flash if variable 2 is 1VAC (8 samples) below variable one, and go solid if 90VAC or lower is read (<=690).
I also want two switches. One to be pressed to run the programme, and the other to be pressed to stop the programme.
OK, here is what I've done so far on the sketch. It's not a lot, but I'm a rookie so I'd like some help to get the above.
int const potPin = A0; //pot to pin A0
int const sgopPin = A5; //voltage divider voltage to pin A5
int const mosfetPin = 9; //MOSFET gate to pin 9
int const redLEDPin = 13; //red led to pin 13
int const greenLEDPin = 12; //green led to pin 12
int const blueLEDPin = 11; //blue led to pin 11
int const switchAPIN = 2; //first switch to pin 2
int const switchBPIN = 4; //second switch to pin 4
int potValue; //getting this ready for later
int sgopValue; //getting this ready for later
int desiredVoltage; //getting this ready for later
int outputVoltage; //getting this ready for later
int potValue = 0; //initial condition for pot voltage. This line might be incorrect?
int sgopValue = 0; //initial condition for divider voltage. This line might be incorrect?
int switchstateA = ; //Initial condition for switch A. This line might be incorrect?
int switchstateB = ; //Initial condition for switch B. This line might be incorrect?
void setup ()
{
Serial.begin(9600); //Sets up PC link to serial window in Arduino PC software
pinMode (redLEDPin, OUTPUT);
pinMode (greenLEDPin, OUTPUT);
pinMode (blueLEDPin, OUTPUT);
pinMode (mosfetPin, OUTPUT);
pinMode (switchAPin, INPUT);
pinMode (switchBPin, INPUT);
pinMode (potPin, INPUT);
pinMode (sgopPin, INPUT);
void loop ()
{
potValue = analogRead(potPin);
Serial.print ("potValue: ");
Serial.print (potValue);
sgopValue = analogRead(sgopPin);
Serial.print (", output: ");
Serial.println (sgopValue);
}
OK I'm aware I haven't done much as of yet. From looking at the commands I have some theories as how to proceed, but I'd like some input before I do so. I was thinking of assigning the pot to var1 and the output to var2 as previously described by using:
var1 = analogRead (A0);
var2 = analogRead (A5);
if (var1 > var2)
//increment PWM to get MOSFET to increase field
else
//decrement PWM to get MOSFET to reduce field
Or something similar?
Many thanks in advance!