Hello,
Im designing a power supply with menus in it. However the voltage and amperage doesnt update itself until the page switches. This is because the state value only updates when you press a button, that means the screen will only update when you press a button.
I know that the issue is on line 47-50, but I have no idea how to approach this problem.
Thanks 
Johnny010:
whut?
int last_st=0,st=0;
What's the problem?
(Apart from the fact that they're global and zeroed anyway)
As I said, the values of "volts" and "amps2" dont update itself until the menu is switched.
If you dont understand whats wrong. See this video:
Why did you put your code in the rubbish bin? If you want help HERE, post your code HERE.
PaulS:
Why did you put your code in the rubbish bin? If you want help HERE, post your code HERE.
Im new to this forum, can you provide me with a valid explaination why pastebin is "rubbish"
can you provide me with a valid explaination why pastebin is "rubbish"
Sure. Because ANYONE can post anything there, there is no virus scanning that happens. So, my company, for one, blocks access to that site.
But, maybe you'll be lucky and someone on the pastebin site will be able to help you. (I'm not holding my breath.)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_ADS1015.h>
#define lcdBacklightPin 10
Adafruit_ADS1115 ads; // Declare an instance of the ADS1115
int16_t rawADCvalue;
int16_t rawADCvalue2; // The is where we store the value we receive from the ADS1115
//int16_t readADC_Differential_2_3
float scalefactor = 0.1875F; // This is the scale factor for the default +/- 6.144 Volt Range we will use
float volts = 0.0; // The result of applying the scale factor to the raw value
float amps = 0.0;
float amps2 = 0.0;
float watts = 0.0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
float temp_c,temp_f;
int y;
int state = 0;
int right=0,left=0,up=0,down=0;
int sel=0;
unsigned long up_time_1,up_time_2,up_time;
double time_1,time_2,time;
int last_st=0,st=0;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin();
ads.begin();
pinMode( lcdBacklightPin, INPUT );
digitalWrite(lcdBacklightPin, LOW);
// Print a message to the LCD.
lcd.setCursor(0,0);
lcd.print(" PSU BY SZYMON");
lcd.setCursor(0,1);
lcd.print(" MASTERNAK");
up_time_1 = millis();
time_1 = millis();
}
void loop() {
time_2 = millis();
last_st = state;
read_state();
st = state;
if(st != last_st){
process_state();
process_display();
//delay(45);
time_1 = millis();
}
time = time_2 - time_1;
time = time/1000;
if(time >=10){
pinMode( lcdBacklightPin, OUTPUT);
}else{
pinMode( lcdBacklightPin, INPUT );
}
delay(90);
rawADCvalue = ads.readADC_Differential_0_1();
volts = (rawADCvalue * scalefactor)/100.0;
if ( volts < 0.30 )
{ volts = 0; }
rawADCvalue2 = ads.readADC_Differential_2_3();
amps = (rawADCvalue2 * scalefactor )/1000.0;
amps2 = amps / 0.43;
if ( amps2 < 0.001 )
{ amps2 = 0; }
watts = (volts * amps2);
delay(100);
}
void process_display(){
switch (right) {
case 1:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("VOLTAGE=");
lcd.println(volts,6);
lcd.setCursor(0,1);
lcd.print("AMPERAGE=");
lcd.println(amps2,6);
break;
case 2:
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" WATTS=");
lcd.print(watts,2);
break;
case 3:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Analog 2");
lcd.setCursor(0,1);
lcd.print("Value");
break;
case 4:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Analog 3");
lcd.setCursor(0,1);
lcd.print("Value");
break;
}
}
void process_state(){
switch (state) {
case 1://right
right = right + 1;
right=right%5;
break;
case 2://up
up = up + 1;
break;
case 3://down
up = up - 1;
break;
case 4://left
right = right - 1;
break;
case 5:
break;
}
}
void read_state(){
state = 0;
y = analogRead (0);
//lcd.setCursor(10,1);
if (y < 100) {
//lcd.print ("Right ");
state = 1;
}
else if (y < 200) {
//lcd.print ("Up ");
state = 2;
}
else if (y < 400){
//lcd.print ("Down ");
state = 3;
}
else if (y < 600){
//lcd.print ("Left ");
state = 4;
}
else if (y < 800){
//lcd.print ("Select");
state = 5;
sel = 1;
}
}
Here you go
void read_state()
Why doesn't this function return a value? Why does state need to be global?
st = state;
Why do you need two global variables containing the same value?
double time_1,time_2,time;
Time variables are unsigned long, not double/float.
delay(90);
Along with using millis()? No!
This is because the state value only updates when you press a button, that means the screen will only update when you press a button.
I agree with the first part of the statement. The second part does not make sense, since YOU control when the display is updated. You do NOT have to make updates dependent on changes to the state variable.
Try commenting out lines 50 & 55, like this:
//if(st != last_st){
process_state();
process_display();
//delay(45);
time_1 = millis();
//}
And see what happens.
Break up 'process display' into two functions - one that clears it and draws all the labels, and one that just updates the values:
void draw_display_page(){
switch (right) {
case 1:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("VOLTAGE=");
lcd.setCursor(0,1);
lcd.print("AMPERAGE=");
break;
case 2:
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" WATTS=");
break;
case 3:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Analog 2");
lcd.setCursor(0,1);
lcd.print("Value");
break;
case 4:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Analog 3");
lcd.setCursor(0,1);
lcd.print("Value");
break;
}
}
void draw_display_fields(){
switch (right) {
case 1:
lcd.setCursor(8,0);
lcd.println(volts,6);
lcd.setCursor(9,1);
lcd.println(amps2,6);
break;
case 2:
lcd.setCursor(9,0);
lcd.print(watts,2);
break;
case 3:
break;
case 4:
break;
}
}
Call 'draw_display_page' where you currently call 'process_display':
if(st != last_st){
process_state();
draw_display_page();
//delay(45);
time_1 = millis();
}
And call draw_display_fields after you compute your values at the end of the loop
watts = (volts * amps2);
draw_display_fields();
delay(100);
}
I'm assuming that lcd.println(amps2,6) does the job of filling the field with blanks when necessary. If it doesn't, you'll need something that does, because if the amps is '10.123' and it changes to '9.5' and the field isn't filled with spaces, you wind up with '9.5123' which is not right.
Thanks alot everyone for your help. The outsiders solution to the problem was the easiest and most importantly it worked.