So Im trying to answer with red text in your quote.
chucktodd:
Ok Peter,
Here is my interpretation of what you wrote.
-
You want to use multiple pins as inputs to measure something
-
You want to use multiple pins as output to control relays
-
You want to display multiple messages on the LCD at the same time.
-
You want to have multiple battery that can power your device
Yes thats correct above.
My advice, break you program into multiple blocks(function), have each function do one thing. It would help me if you would post a schematic of your circuit. Having an actual hardware description to reference coding examples against can avoid mistakes.
Ok, So the code for the input dont have to be in the same section? I can use the code that you wrote, and than write new code under that code and still get everything to show up on the screen?
One questionable item in your circuit description:
When the prime battery falls below 'x' Volts, connect secondary battery, wait 30 seconds, then disconnect prime battery. No, that not what I wrote =)
When the primary voltage drops below "x" volts, connect secondary battery, wait 0.5seconds and then disconnect prime battery, and when on the secondary battery, turn on buzzer to warn me every 30 seconds =)
Depending on your circuit, you could overload the secondary battery. If the Primary battery is 'dead' and you attach a fully charged secondary battery, the 'dead' primary battery will discharge the secondary battery at a high current. Unless your connection circuit is designed to disallow this recharge current. (a posted schematic would answer my question).
To answer question 1, Yes you can use multiple inputs. Be aware that the Arduino has only one Analog to Digital Converter (ADC), it has multiple inputs (A0..A5) that are switched between when you call analogRead(analogPIN); It takes a little while for the voltage on the ADC to stabilize after the switching event, good practice is to average multiple readings:
unsigned long totalReadings=0;
for(uint8_t i=0;i<4;i++){
totalReadings += analogRead(analogPin);
}
uint16_t reading = totalReadings/4;
Displaying multiple messages on the LCD is easy. Just realize you are going to have to position the cursor before every message display event, also you will have to overwrite what is on the display with space characters to erase the previous message.
If you are using a 20x4 LCD, you can call setCursor(0,0) to setCursor(19,3) to select where the next print() statement will display. to blink text on the screen, you have to erase that area of the screen, wait a short time, then re-print your message.
If your circuit can handle a dead battery and a fully charged cross connected the 30second switch time is quite easy;
void setup(){
pinMode(batteryOneRelayControlPin,OUTPUT);
digitalWrite(batteryOneRealyControlPin,HIGH); // if your ciruit is wired this way?
pinMode(batteryTwoRelayControlPin,OUTPUT);
digitalWrite(batteryTwoRealyControlPin,LOW); // if your ciruit is wired this way?
pinMode(senseOnePin,INPUT);
pinMode(senseTwoPin,INPUT);
}
// this is really Bad Coding, but it works. The Delay stops the arduino from doing anything else
// until the delay has completed, if another switch is activated it would not be acted upon until
// the delay has completed.
void switchBatteries(){ // switch decision has already been decided
digitalWrite(batteryTwoRelayControlPin,HIGH);
delay(30000);
digitalWrite(batteryOneRelayControlPin,LOW);
}
// a better coding would be this
enum POWERSTATES {
powerOff, // impossible, arduino can't run program if it has not power!
powerPrime, // on primary battery
powerToPrime, // switch back to primary battery ?
powerSecondary, // on backup battery
PowerToSecondary // transferring to backup battery
};
POWERSTATES power=powerOff;
void setup(){
pinMode(batteryOneRelayControlPin,OUTPUT);
digitalWrite(batteryOneRealyControlPin,HIGH); // if your circuit is wired this way?
pinMode(batteryTwoRelayControlPin,OUTPUT);
digitalWrite(batteryTwoRealyControlPin,LOW); // if your circuit is wired this way?
power=powerPrime;
}
unsigned long timeOut=0; // initialize a timeout counter for delay events.
bool blink=false; // control for blink on, off
void checkPower(){ // read batterySense, switch to Backup if necessary
unsigned long temp=0;
for(uint8_t i=0;i<4;i++){ // create average battery reading
temp = temp + analogRead(batterySense);
}
uint16_t voltage=temp /4; // average battery reading
char printBuff[21]; // display a fixed length string on the lcd
uint8_t len=sprintf(printbuff,"bat=%d",voltage);
for(uint8_t i =len; i<batFieldLen;i++){
printbuff[i]=' '; // blank out rest of voltage field
}
printbuff[batFieldLen]=0; // mark end of string
LCD.setCursor(row,col);
LCD.print(printbuff); // display battery voltage
switch(power){ // depending on what power source, the choices are different
case powerPrime:
if(voltage < lowVoltThreshold){ // are running off prime battery, and it is low!
timeout = millis() + 30000; // mark the start of the switch stage.
digitalWrite(batteryTwoRelayControlPin,HIGH); // turn on secondary battery
power = powerToSecondary;
LCD.setCursor(primeBatteryRow,primeBatteryCol);
LCD.print(" DEAD! ");
LCD.setCursor(secondaryBatteryRow,decondaryBatteryCol);
LCD.print(" Switching! ");
}
break;
case powerToSecondary : // in process to transfer to secondary battery
if(millis()> timeout) {
digitalWrite(batteryOneRelayControlPin,LOW); // turn off Primary Battery
power = powerSecondary;
LCD.setCursor(secondaryBatteryRow,decondaryBatteryCol);
LCD.print(" OnLine! ");
}
break;
case powerSecondary : // on Backup battery
if(timeout>millis()){ // flash Secondary Battery Status
LCD.setCursor(secondaryBatteryRow,decondaryBatteryCol);
if(blink) {
LCD.print(" ");
blink = false;
}
else{
LCD.print(" OnLine! ");
blink = true;
}
timeOut = millis() +500; // flash every 1/2 second
}
default : ; // handle other cases,
}
}
void loop(){
checkPower(); // need to check power at least every 1/2 second, more is better.
// do other stuff
}
This code is untest, but the logic will work.
Chuck.
Thanks for the code, I have not yet read it, but I will spend some time to try to learn from it together with a cup of coffee =)
I will try to paint a schematic for you, this is the relays, buttons and current/voltmeter I have.
http://www.ebay.com/itm/251969102131?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
http://www.ebay.com/itm/141625778140?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
So here is a picture printed out of my head.
Just a basic sketch, hope you see what I see =)

The positive and negative is going to be hooked up to some switches that can run monitor, receivers, and more stuff.
So to start the device, I thinking of just have a pushbutton from mainbattery that gives my Ubec power and turn arduino on and switch the relay on. Even if there is a bad mainbattery it will only be under 10.5v and be able to give ubec power to start up the relays, if the arduino sense that mainbattery is bad it will switch on the backup anyway.
Are you with me? =)