Question
Please help fix my code. My code is for a CT sensor connected to an arduino. We made the breadboard connections and we have our
Please help fix my code. My code is for a CT sensor connected to an arduino. We made the breadboard connections and we have our CT receiving input we checked. Our CT delivers voltage instead of current in the analog input so we convert the voltage to an RMS current using the max voltage obtained. The CT is working on AC while our Arduino works on DC which means that we are having fluctuating voltages so we have to find the max voltage which we think we have it right in the code. The problem is that the CT is receiving a 0 value for the data and its displaying 0 for all the data being displayed on the LED. I think its a logical error in the code but if someone can fix that'd be great.
#include
void setup(){ lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc. lcd.clear(); lcd.setCursor(0,0); // set cursor to column 0, row 0 (the first row) lcd.print("RIP HARAMBE"); lcd.setCursor(0,1); // set cursor to column 0, row 1 (the second row) lcd.print("Energy Meter"); startMillis = millis();
}
void loop() { int voltage = 0; int minVoltage = 5; int maxVoltage = 0; // read the input on analog pin 1: ///Monitors and logs the voltage input for 200 cycles to determine max and min current for (int i=0 ; i<=200 ; i++) { //Reads current input and records maximum and minimum current int sensorValue = analogRead(A1); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): double voltage = sensorValue * (5.0 / 1023.0); if(voltage >= maxVoltage) maxVoltage = voltage; else if(voltage <= minVoltage) minVoltage = voltage; }
int RMSVoltage=maxVoltage/sqrt(2.0);
//Calculates RMS current based on maximum value double RMSCurrent = RMSVoltage*50; //Calculates RMS Power Assuming Voltage 230VAC int RMSPower = 230*RMSCurrent;
if (RMSPower > peakPower) { peakPower = RMSPower; } endMillis = millis(); unsigned long time = endMillis - startMillis; kilos = kilos + ((double)RMSPower * ((double)time/60/60/1000000));
startMillis = millis(); delay(2000);
lcd.clear();
// Displays all current data lcd.setCursor(0,0); lcd.print(RMSCurrent); lcd.print("A"); lcd.setCursor(10,0);
// Displays Power data lcd.print(RMSPower); lcd.print("W"); lcd.setCursor(0,1); lcd.print(kilos); lcd.print("kWh"); lcd.setCursor(10,1); lcd.print(RMSVoltage); lcd.print("V"); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started