Hello guys i have a project at my school were i need to make an energy metter with a msp430f5529 and a hardware part i did all the code myself and the hardware part (current metters,voltage divider etc), (i know is rudimental and ineffective) , the code and the montage works well and does everything i need it to do.Now my professor asked me to save "energie1" and "energie2" so when my microcontroler runs out of battery this 2 variables are saved , i read a lot of threds but i can't figure it out.
Here is the code if it helps ( i can provide you with hardware montage if needed).
Here is the code if it helps ( i can provide you with hardware montage if needed).
Code:
#include <Wire.h>
#include "LiquidCrystal_I2C.h"
byte address = 0x27; // LCD I2C address
int columns = 20; // number of columns on LCD
int rows = 2; // number of rows on LCD
LiquidCrystal_I2C lcd(address, columns, rows);
long previousMillis = 0;
long previousMillis1 = 0;
int timp;
int sensorPin = 23;
int sensorValue = 0;
float volt;
byte newChar1[] = {
B11111,
B10001,
B10001,
B10001,
B10001,
B10001,
B10001,
B11111
};
byte newChar2[] = {
B00000,
B00000,
B11100,
B11100,
B11100,
B11100,
B00000,
B00000
};
byte newChar3[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
int sensorPin1 = 24;
int sensorValue1 = 0;
float amp1;
int sensorPin2 = 25;
int sensorValue2 = 0;
float amp2;
float putere1;
float total_putere1;
float energie1;
float media_puteri1;
float putere2;
float total_putere2;
float energie2;
float media_puteri2;
long interval = 1000;
long interval1=270;
const int buttonPin = PUSH2;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
lcd.init();
pinMode(buttonPin, INPUT_PULLUP);
lcd.createChar(0, newChar1);
lcd.createChar(1, newChar2);
lcd.createChar(2, newChar3);
}
void loop() {
buttonState = digitalRead(buttonPin);
sensorValue = analogRead(sensorPin);
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
unsigned long currentMillis = millis();
unsigned long currentMillis1 = millis();
timp=currentMillis/1000;
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
volt=sensorValue;
volt=(volt*3.3)/4096;
volt=volt*8.51;
amp1=sensorValue1;
amp1=(amp1*3.3)/4096;
if(amp1<0.5)
amp1=0;
amp1=amp1/0.5;
amp2=sensorValue2;
amp2=(amp2*3.3)/4096;
amp2=amp2/0.05;
if(amp2<1.25)
amp2=0;
putere1=volt*amp1;
putere2=volt*amp2;
total_putere1=putere1+total_putere1;
total_putere2=putere2+total_putere2;}
if (buttonState == LOW) {
lcd.backlight();
}
else
lcd.noBacklight();
if(currentMillis1 - previousMillis1 > interval1) {
previousMillis1 = currentMillis1;
if(buttonPushCounter==1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Battery Voltage");
lcd.setCursor(0,1);
lcd.print(volt);
lcd.print(" V");}
if(buttonPushCounter==2){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Solar Current");
lcd.setCursor(0,1);
lcd.print(amp1);
lcd.print(" A");}
if(buttonPushCounter==3){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pump Current");
lcd.setCursor(0,1);
lcd.print(amp2);
lcd.print(" A");}
if(buttonPushCounter==4){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Solar Energy");
lcd.setCursor(0,1);
lcd.print(energie1);
lcd.print(" Wh");}
if(buttonPushCounter==5){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pump Energy");
lcd.setCursor(0,1);
lcd.print(energie2);
lcd.print(" Wh");}
if(buttonPushCounter==6){
lcd.clear();
if(volt>12.6){
lcd.clear();
lcd.setCursor(0,1);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(1);
lcd.setCursor(0,0);
lcd.print("State Of Charge");
lcd.setCursor(6,1);
lcd.print("100");
lcd.print("%");}
if(volt<12.45 && volt>12.21){
lcd.clear();
lcd.setCursor(0,1);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(0);
lcd.write(1);
lcd.setCursor(0,0);
lcd.print("State Of Charge");
lcd.setCursor(6,1);
lcd.print("80");
lcd.print("%");}
if(volt<12.20 && volt>11.91){
lcd.clear();
lcd.setCursor(0,1);
lcd.write(2);
lcd.write(2);
lcd.write(2);
lcd.write(0);
lcd.write(0);
lcd.write(1);
lcd.setCursor(0,0);
lcd.print("State Of Charge");
lcd.setCursor(6,1);
lcd.print("60");
lcd.print("%");}
if(volt<11.90 && volt>11.60){
lcd.clear();
lcd.setCursor(0,1);
lcd.write(2);
lcd.write(2);
lcd.write(0);
lcd.write(0);
lcd.write(0);
lcd.write(1);
lcd.setCursor(0,0);
lcd.print("State Of Charge");
lcd.setCursor(6,1);
lcd.print("40");
lcd.print("%");}
if(volt<11.59){
lcd.clear();
lcd.setCursor(0,1);
lcd.write(2);
lcd.write(0);
lcd.write(0);
lcd.write(0);
lcd.write(0);
lcd.write(1);
lcd.setCursor(0,0);
lcd.print("State Of Charge");
lcd.setCursor(6,1);
lcd.print("20");
lcd.print("%");}}
if(buttonPushCounter>6)
buttonPushCounter=1;}
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;}}
lastButtonState = buttonState;
media_puteri1=total_putere1/timp;
energie1=media_puteri1*timp/3600;
media_puteri2=total_putere2/timp;
energie2=media_puteri2*timp/3600;
}