#include <Arduino.h>
#include <math.h>
//TRUE RMS START
#include <Filters.h> //Easy library to do the calculations
#include <TM1637Display.h>
float testFrequency = 100; // test signal frequency (Hz)
float windowLength = 100.0 / testFrequency; // how long to average the signal, for statistist
int Sensor = 0; //Sensor analog input, here it's A0
int Sensor2 = 0; //Sensor analog input, here it's A1
float intercept = -0.04; // to be adjusted based on calibration testing
float slope = 0.0405; // to be adjusted based on calibration testing
float current_Volts, output_volts; // Voltage
unsigned long printPeriod = 500; //Refresh rate
unsigned long previousMillisTRUE = 0;
int truermsflag = 0;
int inputvolt = 0, outputvolt = 0;
RunningStatistics inputStats;
RunningStatistics inputStats2;
int sigmaint = 0;
//TRUE RMS end
//#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
//#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
// Module connection pins (Digital Pins)
#define CLK 3
#define DIO 2
#define CLK1 4
#define DIO1 5
#define switcht 6
#define Relay1 8
#define Relay2 9
#define Relay3 10
#define Relay4 11
#define TRelay 12
// The amount of time (in milliseconds) between tests
int i = 0;
int timerflag = 0, onetimeflag = 0;
int volt = 0, voltoutput = 0;
TM1637Display display(CLK, DIO);
TM1637Display display2(CLK1, DIO1);
const uint8_t DONE[] =
{
//SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
SEG_D, // O
//SEG_C | SEG_E | SEG_G, // n
//SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E
};
const uint8_t Dely[] =
{
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // E
SEG_F | SEG_E | SEG_D, // L
SEG_F | SEG_B | SEG_G | SEG_C // Y
};
const uint8_t HIVOLT[] =
{
SEG_F | SEG_E | SEG_B | SEG_C | SEG_G, // H
//SEG_B | SEG_C, // I
//SEG_F | SEG_E | SEG_D | SEG_C | SEG_B, // V
};
const uint8_t L0VOLT[] =
{
SEG_F | SEG_E | SEG_D, // L
//SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,//0
//SEG_F | SEG_E | SEG_D | SEG_C | SEG_B ,// V
};
int ledPin = 13; // the number of the LED pin
unsigned long previousMillis1 = 0, previousMillis2 = 0, previousMillis3 = 0; // will store last time LED was updated
//unsigned long currentMillis;
unsigned long previousmacro = 0;
unsigned long currentMacro = 0;
int acVoltage(int adcpin);
int average(int pin);
void setup()
{
//
// sbi(ADCSRA, ADPS2);
// cbi(ADCSRA, ADPS1);
// cbi(ADCSRA, ADPS0);
analogReference(EXTERNAL); // set ADC positive reference voltage to 1.1V (internal)
pinMode(switcht, INPUT);
pinMode(switcht, INPUT_PULLUP);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
pinMode(TRelay, OUTPUT);
//For Optocuplar Relay
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay4, HIGH);
digitalWrite(TRelay, HIGH);
pinMode(ledPin, OUTPUT);
display.setBrightness(0x0f);
display2.setBrightness(0x0f);
//display.setSegments(Dely);
// delay(3000);
// display.clear();
// display.setSegments(HIVOLT, 3, 0);
//
// delay(3000);
// display.clear();
// display.setSegments(L0VOLT, 3, 0);
//display2.setSegments(Dely);
//display.clear();
Serial.begin(57600);
//delay(1000);
}
void loop()
{
if (truermsflag == 0)
{
//TRUE RMS
truermsflag = 1;
//END TRUE RMS
}
unsigned long currentMac = micros();
if (currentMac - previousMillis3 > 3)
{
previousMillis3 = currentMac;
//Easy life lines, actual calculation of the RMS requires a load of coding
inputStats.setWindowSecs( windowLength );
inputStats2.setWindowSecs( windowLength );
Sensor = analogRead(A0); // read the analog in value:
Sensor2 = analogRead(A1); // read the analog in value:
inputStats.input(Sensor); // log to Stats function
inputStats2.input(Sensor2); // log to Stats function
//Serial.println("run") ;
}
if ((unsigned long)(millis() - previousMillisTRUE) >= printPeriod)
{
previousMillisTRUE = millis(); // update time every second
current_Volts = intercept + slope * inputStats.sigma(); //Calibartions for offset and amplitude
current_Volts = current_Volts * (56.613); //Further calibrations for the amplitude
inputvolt = round(current_Volts);
Serial.print(" START ");
Serial.print("Input Volts:");
Serial.println(inputvolt);
Serial.print("Input Sigma");
Serial.println(inputStats.sigma());
Serial.println(" END ");
output_volts = intercept + slope * inputStats2.sigma();
output_volts = output_volts * (56.613);
outputvolt = round(output_volts);
//Serial.print("Output Volts:");
//Serial.println(outputvolt);
}
//end
unsigned long currentMillis = millis();
if (currentMillis - previousMillis3 > 1000)
{
// save the last time you blinked the LED
previousMillis3 = currentMillis;
digitalWrite(ledPin, LOW);
// display.showNumberDec(inputvolt, false, 3, 1);
}
/*
unsigned long currentMillis = millis();
if ((currentMillis - previousMillis1 > 1000) && timerflag == 0) {
previousMillis1 = currentMillis;
digitalWrite(ledPin, HIGH);
if ((digitalRead(switcht) == LOW) && timerflag == 0 && onetimeflag == 0) {
i = 10;
onetimeflag = 1;
}
if ((digitalRead(switcht) == HIGH) && timerflag == 0 && onetimeflag == 0) {
i = 180;
onetimeflag = 1;
}
display2.showNumberDec(i--);
if (i == 0) {
digitalWrite(TRelay, LOW);
timerflag = 1;
}
}
//Timer Relay Function END
//ADC Functions
//display.setSegments(HIVOLT, 1, 0);
if (timerflag == 1) {
display2.showNumberDec(outputvolt, false, 3, 1); // Expect: _14_
if (outputvolt > 245) {
display2.setSegments(HIVOLT, 1, 0);
digitalWrite(TRelay, HIGH);
}
else if (outputvolt <= 180) {
display2.setSegments(L0VOLT, 1, 0);
digitalWrite(TRelay, HIGH);
} else {
display2.setSegments(DONE, 1, 0);
digitalWrite(TRelay, LOW);
}
}
*/
if (240 < current_Volts)
{
digitalWrite(Relay1, LOW);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay4, HIGH);
}
if ((90 <= current_Volts) && (current_Volts <= 120))
{
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay4, LOW);
}
if ((121 <= current_Volts) && (current_Volts <= 150))
{
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, HIGH);
}
if ((151 <= current_Volts) && (current_Volts <= 240))
{
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay4, HIGH);
}
//ITimer2.init();
//ITimer1.init();
// ITimer1.attachInterruptInterval(TIMER2_INTERVAL_MS, TimerHandler2);
//ITimer2.attachInterruptInterval(TIMER2_INTERVAL_MS, TimerHandler2);
}
int acVoltage(int adcpin)
{
int adcvalue = 0, returnresult = 0;
unsigned long sum = 0, avg = 0, squre = 0;
double root = 0;
for (int j = 0; j < 10; j++)
{
unsigned long currentMacro = micros();
unsigned long currentMillis;
if ((currentMacro - previousmacro > 2000))
{
previousmacro = currentMillis;
adcvalue = average(adcpin);
}
squre = (long)adcvalue * adcvalue;
//delayMicroseconds(667);
sum += squre;
}
avg = (long)sum / 10;
//Serial.print("after average of squre");
//Serial.println(avg);
root = sqrt(avg);
returnresult = (int)root;
return returnresult;
}
int average(int pin)
{
int sum = 0;
float avg = 0;
int val = 0;
int funsend;
for (int i = 0; i < 5; i++)
{
val = analogRead(pin);
sum += val;
}
avg = (float)sum / 5;
funsend = round(avg);
//Serial.print(" average function");
//Serial.println(avg);
//Serial.print("Value");
//Serial.println(funsend);
//delay(20);
return funsend;
}
</code>```
-
Please describe what you mean when you say "not stable".VE7JRO– VE7JRO2020-08-19 19:31:43 +00:00Commented Aug 19, 2020 at 19:31
Add a comment
|