Problem with Arduino Computations

What am I doing wrong here? I’m not getting any decimal values in power.

Here is my code

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7);

#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance


//voltage variables
int sensorValue = 0;
float voltage = 0;
int voltagepin = A0;

//current variables
double current = 0;
int currentpin = A1;

//power reading variables
float power = 0;
int V = 0;
int C = 0;

void Voltage();
void Current();
void Power();

void setup() {
  // put your setup code here, to run once:
  lcd.begin(20,4);
  lcd.setBacklightPin(3, POSITIVE);

  //LCD backlight
  lcd.setBacklight(HIGH);
  Serial.begin(9600);

  emon1.current(currentpin, 111.1);// Current: input pin, calibration.

 lcd.home();
lcd.setCursor(7,1);
lcd.print("Power");
lcd.setCursor(4,2);
lcd.print("Measurement");
delay(5000);
lcd.clear();
  
}

void loop() {
  // put your main code here, to run repeatedly:
Voltage();
Current();

Power();
}

void Power(){

V = voltage;
C = current;
power = V * C;
lcd.setCursor(7,2);
lcd.print(power);
lcd.setCursor(7,3);
lcd.print("Watts");

//delay(1000);
}

void Voltage(){
    // read the input on analog pin 0:
  int sensorValue = analogRead(voltagepin);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 250V):
  voltage = (sensorValue * (220.0 / 1024.0)) + 0.2;
  // print out the value you read:
  //lcd.clear();
  lcd.home();
  //lcd.print("AC Voltage: ");
  Serial.print("AC Voltage: ");
  //lcd.setCursor(0,1);
  lcd.print(voltage);
  Serial.print(voltage);
  lcd.setCursor(0,1);
  lcd.print("Volts");
  Serial.println(" Volts");
  //delay(500);
}

void Current(){
  current = emon1.calcIrms(1480);  // Calculate Irms only
  
  //Serial.print(Irms*230.0);         // Apparent power
  //Serial.print(" ");
  //lcd.home();
  //lcd.print("AC Current: ");
  lcd.setCursor(14,0);
  lcd.print(current);
  lcd.setCursor(14,1);
  lcd.print(" Amps");
  Serial.print("AC Current: ");
  Serial.print(current);// Irms
  Serial.println("Amps");
  //delay(500);
}
float power = 0;
[color=red]int[/color] V = 0;
[color=red]int[/color] C = 0;
...
[color=red]power = V * C;[/color]

so why would you get any decimal?

an int multiplied by an int is an int (and can overflow)... casting that int into a float does not create magic decimals...

float power = 0;
int V = 0;
int C = 0;

V = voltage;
C = current;
power = V * C;

When you multiply two integers you get a whole number right.

Your problem occurs when you assign the float “voltage” to the int “V” (and similarly with C).

Why did you not just use power=voltage *current?

OOHHH now i seeeee. thank youu :wink:

I rewrote my code.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7);

#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance


//voltage variables
int sensorValue = 0;
float voltage = 0;
int voltagepin = A0;

//current variables
double current = 0;
int currentpin = A1;

//power reading variables
float power = 0;

void Voltage();
void Current();
void Power();

void setup() {
  // put your setup code here, to run once:
  lcd.begin(20,4);
  lcd.setBacklightPin(3, POSITIVE);

  //LCD backlight
  lcd.setBacklight(HIGH);
  Serial.begin(9600);

  emon1.current(currentpin, 111.1);// Current: input pin, calibration.

 lcd.home();
lcd.setCursor(7,1);
lcd.print("Power");
lcd.setCursor(4,2);
lcd.print("Measurement");
delay(5000);
lcd.clear();
  
}

void loop() {
  // put your main code here, to run repeatedly:
Voltage();
Current();

Power();
}

void Power(){
power = voltage * current;
lcd.setCursor(7,2);
lcd.print(power);
lcd.setCursor(7,3);
lcd.print("Watts");
delay(500);
}

void Voltage(){
    // read the input on analog pin 0:
  int sensorValue = analogRead(voltagepin);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 250V):
  voltage = (sensorValue * (220.0 / 1024.0)) + 0.2;
  // print out the value you read:
  //lcd.clear();
  lcd.home();
  //lcd.print("AC Voltage: ");
  Serial.print("AC Voltage: ");
  //lcd.setCursor(0,1);
  lcd.print(voltage);
  Serial.print(voltage);
  lcd.setCursor(0,1);
  lcd.print("Volts");
  Serial.println(" Volts");
  delay(500);
}

void Current(){
  current = emon1.calcIrms(1480);  // Calculate Irms only
  
  //Serial.print(Irms*230.0);         // Apparent power
  //Serial.print(" ");
  //lcd.home();
  //lcd.print("AC Current: ");
  lcd.setCursor(14,0);
  lcd.print(current);
  lcd.setCursor(14,1);
  lcd.print(" Amps");
  Serial.print("AC Current: ");
  Serial.print(current);// Irms
  Serial.println("Amps");
  delay(500);
}

these would be more appropriate because your pins won't change (and fit on a byte)

const byte VoltagePin = A0;
....
const byte  CurrentPin = A1;

I see you use double and float. On a UNO these are using 4 bytes each so you are not better off with double.

(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE to indent your code properly)