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 #include LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Assign LCD screen pins, as per LCD shield requirements unsigned long startMillis; unsigned long endMillis; double peakVoltage; int peakPower = 0; double kilos = 0;

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!