Need help with a tricky code:

In this project. I want to use an arduino mega, 2 lcds, and 6 acs712 current sensing modules and 2 Lm2596 dc step up/down module

The set up I have in mind is like so; arduino is powered by ATX stand by power (IF possible)
there is a push button which turns on the programing to start the monitoring and also grounds the ATX and GReen wire to supply power to all the rails.
This set up will calculate Voltage and current printting them on the lcd's.
The constant voltages will be printed on on lcd and the variable voltages on the other lcd.
Ive written most of the code. I just want it to make sense.

I've added a picture, which my project is based on.. the difference are, im not using I2c bus for lcd, im measuring current, and im printing info on two different lcds.

thanks for the help.

psu_mine.ino (6.58 KB)

An good quality ATX power supply has a well balanced design, and changes to it could make it work less good.
You have to make sure that the ACS712's are properly wired.

I think it is possible.
The Standby voltage is 5V ? It is possible to run the Mega board with that, preferably with a usb cable to the usb socket.

In your sketch, please use the same kind of names for the pins. Do not use 'double', also #define is not preferred. Use "const int".

const int pinPowerOK = 4;
const int pinSenseVthree = A0;

It is allowed to combine the pins for the LCD with only seperate enable pins ? I guess that should work.

I don't understand the extra brackets. I would do it in a different way:

// Voltsthree = ((analogRead(sensePinVthree)) /218.0) *5;
Voltsthree = (float) analogRead(sensePinVthree) / 218.0 * 5.0;

You also have to explain why the value "218.0" is used. It is now a magic number without any meaning.

Thank you very much. As you can already tell. I'm very new to coding and chunks of this code was spliced from a different code. And Maybe if you see the code i mean you can understand the origins of 218.0

If it is your sketch, you have to explain the number. Or even better, let the Arduino do the calculation.
I'm sorry to say, but that is sketch is poorly written. It is not a good example.

I don't like it when someone calculates a certain value and put that in a sketch. That is what the compiler and the Arduino is for. It is far more better to show what is going on.
In my opinion it is better to have good written code that does not work, then bad written code that does work. Because when the code is written well, a bug is easy to spot. And bad written code is useless because no one knows what is going on.

How far are you with this project ?
Did you modify your sketch with "const int" for the pin numbers and so ?

I agree.
Just finished designing schematic and parts should be with me in the next few days. I wanted to get started on the code as its my weakest skill or lack there of. In my experience, I can complete a build in a blink of an eye but the code is what holds me back.

Yes i have changed it about, thanks it compiles ok now, but if you would be so kind and give it a second look it will be much appreciated.

.

//Bench PSU Arduino LCD


/*-----( Import needed libraries )-----*/
#include <Wire.h>
#include <LiquidCrystal.h>

#define PWRBTN 2
#define PWROK_PIN 4
#define PWRTRANS 6


//sensor info

const int three = 6; // Analog input pins that the sensor is attached to
const int five = 7;
const int fivex = 8;
const int twelve = 9;
const int Vrone = 10;
const int Vrtwo = 11;

double threeI = 0;  // intialize variable currnent
double fiveI = 0;  
double fivebI = 0;
double twelveI = 0;
double VroneI= 0;
double VrtwoI = 0;

double threeVo = 0;
double fiveVo = 0; // initialize variable for sensor output voltage
double fivebVo = 0;
double twelveVo = 0;
double VroneVo = 0;
double VrtwoVo = 0;

/*-----( Declare objects )-----*/

// Variables will change:
int PWRSTATE = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers


LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //16,4
LiquidCrystal lcd2(12, 10, 5, 4, 3, 2);//16,2


float threeV, fiveV,fiveb, twelveV, VroneV, VrtwoV;
float threevolts, fivevolts,fivebs, twelvevolts, Vronevolts, Vrtwovolts;


void LCDSetup(){
  
  lcd.begin(16, 4);
  lcd2.begin(16, 2);

 // Print a message to the LCD.
 lcd.setCursor(0,0); //take this out if monitor buggy
 lcd.print("Bench PSU");// this code be fine
 lcd.setCursor(0,1);
 lcd.print("Monitor  V1.0");
 lcd.setCursor(0,2);
 lcd.print("ICE");
 lcd.setCursor(4,3);
 lcd.print("ROBOTICS");
 
 lcd2.begin(16, 2); // replace with the date_time function
 lcd2.setCursor(0,0);
 lcd2.print("Hope 4 the best");
 lcd2.setCursor(0, 1);
 lcd2.print("prep 4 the worst");
 
 delay(3000);
 
 
 lcd.clear(); // wipes old message 
 lcd.setCursor(0,0);  
 lcd.print("Building");
 lcd.setCursor(6,1);
 lcd.print("The Future");
 lcd.setCursor(0,1);
 lcd.print("since   2014");
 
 delay(2500);
 //Inset the time function somewhere in here
 lcd.clear();
 lcd2.clear();
  
 
  
}


void readVoltages(){
  threeV =  (analogRead(A0)* 5)/1024.0; // attched to voltage deviders
  fiveV =   (analogRead(A1)* 5)/1024.0;
  fiveb =  (analogRead(A2)* 5)/1024.0;  
  twelveV = (analogRead(A3)* 5)/1024.0;
  VroneV = (analogRead(A4)* 5)/1024.0;
  VrtwoV = (analogRead(A5)* 5)/1024.0;
} 

void calculatePrintVoltages(){
  threevolts = threeV;
  lcd.setCursor(0,0); 
  lcd.print(threevolts);
  lcd.setCursor(5,0);
  lcd.print("V");
  
  
  fivevolts = (fiveV)/ (1770.0/(182.0+1770.0));
  lcd.setCursor(0,1); 
  lcd.print(fivevolts);
  lcd.setCursor(5,1);
  lcd.print("V");
  
  fivebs = (fiveb)/ (1770.0/(182.0+1770.0)); // usb rail
  lcd.setCursor(0,2); 
  lcd.print(fivebs);
  lcd.setCursor(5,2);
  lcd.print("V");
  
  twelvevolts = (twelveV ) / (1770.0/(4650.0+1770.0));
  lcd.setCursor(0,3); 
  lcd.print(twelvevolts);
  lcd.setCursor(5,3);
  lcd.print("V");
  
  Vronevolts = (VroneV ) / (1770.0/(11890.0+1770.0));
  lcd2.setCursor(0,0); 
  lcd2.print(Vronevolts);
  lcd2.setCursor(5,0);
  lcd2.print("V");
  
  
  Vrtwovolts = (VrtwoV ) / (1770.0/(11920.0+1770.0));
  lcd2.setCursor(0,1); 
  lcd2.print(Vrtwovolts);
  lcd.setCursor(5,1);
  lcd.print("V");
  
  
  
}

// uncomment the below code, it wont work with this
void checkPowerOK(){
  if (digitalRead(PWROK_PIN) == HIGH) {
    lcd.setCursor(8,2);
    lcd.print("ON ");
    lcd2.setCursor(4,0);
    lcd2.print("Hello");
  }
  else if (digitalRead(PWROK_PIN) == LOW) {
    lcd.setCursor(8,2);
    lcd.print("OFF");
    lcd2.setCursor(3, 0);
    lcd2.print("Good bye");
  }               
}     

void togglePower(){
 static unsigned long last_interrupt_time = 0;
 unsigned long interrupt_time = millis();
 // If interrupts come faster than 200ms, assume it's a bounce and ignore
 if (interrupt_time - last_interrupt_time > 500) 
 {
   // set the power:
  digitalWrite(PWRTRANS, PWRSTATE);
 }
 PWRSTATE = !PWRSTATE;
 last_interrupt_time = interrupt_time;  
}


void setup()   /****** SETUP: RUNS ONCE ******/
{
 LCDSetup(); 
 pinMode(PWRBTN, INPUT);
 pinMode(PWRTRANS, OUTPUT);
 attachInterrupt(0, togglePower, RISING);
 digitalWrite(PWRTRANS, PWRSTATE);
 
 
}
//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/{
  checkPowerOK();
  readVoltages();
  calculatePrintVoltages();
  delay(250);
  
  //read the analog in value
  threeVo = analogRead(three)*5.0/1023.0;
  // calibration to get actual current value
  threeI = (threeVo -2.5 )/0.185;
  
  //print to lcd
  lcd.setCursor(7,0);
  lcd.print(threeI);
  lcd.setCursor(14,0);
  lcd.print("A");
  
  delay(4);
  
  
  fiveVo = analogRead(five)*5.0/1023.0;
  // calibration to get actual current value
  fiveI = (fiveVo -2.5 )/0.185;
  
  lcd.setCursor(7,1);
  lcd.print(fiveI);
  lcd.setCursor(14,1);
  lcd.print("A");
  
  delay(4);
  
  
  fivebVo = analogRead(fivex)*5.0/1023.0;
  // calibration to get actual current value
  fivebI = (fivebVo -2.5 )/0.185;
  
  lcd.setCursor(7,2);
  lcd.print(fivebI);
  lcd.setCursor(14,2);
  lcd.print("A");
  
  delay(4);
  
  twelveVo = analogRead(twelve)*5.0/1023.0;
  // calibration to get actual current value
  twelveI = (twelveVo -2.5 )/0.185;
  
  lcd.setCursor(7,3);
  lcd.print(twelveI);
  lcd.setCursor(14,3);
  lcd.print("A");
  
  delay(4);
  
  VroneVo = analogRead(Vrone)*5.0/1023.0;
  //calibration
  VroneI = (VroneVo - 2.5 )/0.185;
  
  lcd2.setCursor(0,0);
  lcd2.print(VroneI);
  lcd2.setCursor(5,0);
  lcd2.print("A");
  
  delay(4);
  
  
  
  VrtwoVo = analogRead(Vrtwo)*5.0/1023.0;
  //calibration
  VrtwoI = (VrtwoVo - 2.5 )/0.185;
  
  lcd2.setCursor(0,1);
  lcd2.print(VrtwoI);
  lcd2.setCursor(5,1);
  lcd2.print("A");
  
  delay(4);
}
 
//--(end main loop )---






//*********( THE END )***********

You started with a bad looking sketch. I hope you don't mind, but it still looks ugly. If it will do something when the hardware is ready, you should redesign it. Perhaps using a function for pieces of code that are the same.

In the Arduino, a "const int" for a pin number is preferred over "#define", because a "#define" can be altered and a "const int" can not.

Could you make the "PWRBTN" and the others a "const int" as well ?

I also prefer to put the term "pin" in those names. For example "fivePin" or "pinFive" or "five_pin".

The 'double' is not used on a Arduino Mega, it will be converted to 'float'.

I would do these lines different:

// threeV =  (analogRead(A0)* 5)/1024.0;
treeV = (float) analogRead(A0) * 5.0 / 1024.0;

The analogRead() returns an integer, that is casted to a float with "(float)" and everything else is in float.

The PWRSTATE is an integer that is used for a digitalWrite(), it is either HIGH or LOW. That's okay.
But this is a boolean operation : PWRSTATE = !PWRSTATE;
I don't like that, even if it will work. A boolean operation should be done with a boolean variable that is true or false.
I think it is better to write it as normal code:

  // Toggle PWRSTATE
  if(PWRSTATE == LOW)
    PWRSTATE = HIGH;
  else
    PWRSTATE = LOW;

Your sketch has still unexplaned numbers (1770.0, 182.0, 4650.0, 11890.0, 11920.0).

There is absolutely nothing wrong with using #define, so long as you are aware it is
purely textual substitution.

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

You don't use these time stamp variables anywhere. But when or if you do, they have to be unsigned long type, or you will have a millis() rollover glitch.