/* Dummy Load
Version 1.003
15/9/2024
*/
//
// Includes-
#include <LiquidCrystal.h>
//
//
//
//
//
//
//
// Declare variables
//
/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
float LoadResistance; // Load resistance value determined from pin selection function reading pind D2-D4
float RawVoltage; // Analogue voltage read from top of load resistor in ADC counts. Analogue voltage scaled down by 8 to fit Arduino ADC input range. eg 40V p-p becomes 5V p-p
float UncalibratedVoltage; // RawVoltage scaled to
int OhmSelectionPin4 = 2; // Digital input pin 2 to select 4 ohm resistor- it will be high if selector switch isd set to 4 ohm load
float OhmRealValue4 = 3.99; // Acual resistor value measured eg 3.98ohms
int OhmSelectionPin8 = 3; // Digital input pin 3 to select 8 ohm resistor- it will be high if selector switch isd set to 8 ohm load
float OhmRealValue8 = 7.99; // Acual resistor value measured eg 7.98ohms
int OhmSelectionPin15 = 4; // Digital input pin 4 to select 15 ohm resistor- it will be high if selector switch isd set to 15 ohm load
float OhmRealValue15 = 14.99; // Acual resistor value measured eg 14.98ohms
float CurrentResistorValue=6.5;
int analogueVoltagePin = 0;
float peakToRMS = 0.70711;
float RMSVoltage;
float scaledVoltage;
float scaleVoltageADCFactor = 0.0390625;
float voltCalibratedVoltage;
float voltageCalibrationFactor = 0.48; //use to test input voltage on Wokwi.com simulator
float freqCalibratedVoltage;
float frequencyCalibrationFactor = 0.99;
bool pinState4;
bool pinState8;
bool pinState15;
// Frequency stuff
int Htime; //integer for storing high time
int Ltime; //integer for storing low time
float Ttime; // integer for storing total time of a cycle
float frequency; //storing frequency
int FrequencyInputPin = 5; // D5 has the frequeny signal on it
//Values to put onscreen
float voltageDisplay;
float currentDisplay;
float resistanceDisplay;
float powerDisplay;
float frequencyDisplay;
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2);
// Pin Assignments
pinMode(OhmSelectionPin4, INPUT); // resistor selection pin
pinMode(OhmSelectionPin8, INPUT); // resistor selection pin
pinMode(OhmSelectionPin15, INPUT); // resistor selection pin
pinMode(FrequencyInputPin,INPUT); // frequency Pin
}
// Functions Below-
void readVoltage ()
{
RawVoltage = analogRead(analogueVoltagePin); // get adc counts
}
void scaleADCVoltage()
{
scaledVoltage = RawVoltage * scaleVoltageADCFactor; // one ADC count equals 0.0390625 Volts
}
void calibrateVoltage()
{
//freq
freqCalibratedVoltage = scaledVoltage * frequencyCalibrationFactor;
//voltage
voltCalibratedVoltage = freqCalibratedVoltage * voltageCalibrationFactor; //r
}
void toRMSVoltage ()
{
RMSVoltage = voltCalibratedVoltage * peakToRMS;
}
void setDisplayVoltage ()
{
voltageDisplay=RMSVoltage;
}
void calculateCurrent ()
{
currentDisplay = RMSVoltage/CurrentResistorValue;
}
void readResistorSelection ()
{
pinState4 = digitalRead(OhmSelectionPin4);
pinState8 = digitalRead(OhmSelectionPin8);
pinState15 = digitalRead(OhmSelectionPin15);
if (pinState4 == HIGH) {resistanceDisplay = OhmRealValue4;} //
if (pinState8 == HIGH) {resistanceDisplay = OhmRealValue8;}
if (pinState15 == HIGH) {resistanceDisplay = OhmRealValue15;}
}
void readFrequenccy ()
{
Htime=pulseIn(FrequencyInputPin,HIGH); //read high time
Ltime=pulseIn(FrequencyInputPin,LOW); //read low time
Ttime = Htime+Ltime;
frequency=1000000/Ttime; //getting frequency with Ttime is in Micro seconds
frequencyDisplay=frequency;
}
void writeValuesToLCD ()
{
lcd.setCursor(0, 0);
lcd.print(voltageDisplay); // Works
lcd.setCursor(6, 0);
lcd.print("V");
lcd.setCursor(10, 0);
lcd.print(currentDisplay); // works
lcd.setCursor(15, 0);
lcd.print("A");
lcd.setCursor(0, 1);
lcd.print(voltageDisplay * currentDisplay); // works
lcd.setCursor(6, 1);
lcd.print("W");
lcd.setCursor(10, 1);
lcd.print(frequencyDisplay); // works n
lcd.setCursor(14, 1);
lcd.print("Hz");
delay (1000);
}
void loop() {
// put your main code here, to run continuously:
readFrequenccy ();
readVoltage ();
scaleADCVoltage (); //Convert ADC Counts to voltage
calibrateVoltage(); //placeholder function pending completed build and testing/calibration
toRMSVoltage ();
setDisplayVoltage ();
readResistorSelection ();
calculateCurrent ();
writeValuesToLCD ();
delay(100);
}