First off I am fairly new at this but have worked through a number of minor projects. This is my first somewhat larger project.
First a bit of background on the project. I am creating a tank monitoring solution to monitor the fluid level in up to 4 holding tanks. The sensors used to monitor the tanks return a variable voltage between 0 and 5 volts. I have set up 4 analog inputs to read the levels. I use 2 buttons to execute the required functions. I call them "mode" and "set".
As part of the system initialization, I determine the number of connected tanks and then in several nested loops pick a name from a preconfigured array (tankDescript) and assign it to a tank. At the same time I write the chosen name to EEPROM to save it through power cycles or resets.
This all works perfectly as long as I only have 4 names in the tankDescript array. As soon as I add more than 4 names to the array, things get hairy in that I seem to get corruption and system freezes as it runs through the loops.
I am using an array technique I found here by nickgammon A true "ARRAY OF STRINGS". Here's how I did it using pointers. :) - Programming Questions - Arduino Forum post #4.
I have attached my code below. Sorry if it a bit long but is stripped to the minimum I could while still making it functional.
Any insight welcome. BTW this is running on a Nano V3.
Thanks
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <JC_Button.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
Button modeButton(2, 50); // create Button object that attach to pin 2;
Button setButton(3, 50); // create Button object that attach to pin 3;
const int tankSensor[] = { 14, 15, 16, 17 }; // Tank sensor inputs
const int LED = 13; // Built in LED
char tankName[4][10];
String tankType[4];
int tankNumber = 0;
int numberOfTanks = 0;
const int numberOfDescripts = 5;
const int maxSize = 7;
char tankDescript [numberOfDescripts] [maxSize] = {
{ "Fresh " },
// { "Fresh2" },
{ "Galley" },
{ "Bath " },
{ "Grey " },
{ "Black " },
};
void setup() {
// initialize the LCD and Console
lcd.init();
lcd.backlight();
Serial.begin(9600);
modeButton.begin();
setButton.begin();
// initialize the LED pin as an output.
pinMode(LED, OUTPUT);
for (int index = 0; index < 4; index++) {
pinMode(tankSensor[index], INPUT_PULLUP);
}
// Wipe EEPROM memory if both Mode and Set buttons are pressed.
// The only time this can be done is immediately after a reset.
if(modeButton.read() && setButton.read()) {
int i;
for (i = 0; i < 60; i++) {
EEPROM.write(i, 0);
}
while (i) { // Flash the built in LED when done
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
lcd.setCursor(0,1);
lcd.print(" Initialized. Press");
lcd.setCursor(0,2);
lcd.print(" RESET to continue.");
}
}
for (int t = 0; t < 4; t++) { // Determine how many tanks are connected. Max 4.
if (analogRead(t) < 1001) numberOfTanks++;
}
if (numberOfTanks == 0) {
lcd.setCursor(0, 0);
lcd.print("No tanks detected");
lcd.setCursor(0, 1);
lcd.print("Connect a tank");
lcd.setCursor(0, 2);
lcd.print("and press 'Reset'");
bool i = true;
while (i);
}
int knownTanks = EEPROM.read(17); // EEPROM location 17 contains the number of known tanks
if (knownTanks < numberOfTanks) { // If new tanks detected, configure them
lcd.print(numberOfTanks);
lcd.print(" tanks detected.");
lcd.setCursor(0, 1);
lcd.print(numberOfTanks - knownTanks);
lcd.print(" new tanks detected");
tankNumber = 0;
knownTanks = numberOfTanks; // Update the EEPROM with the new knownTanks
EEPROM.write(17, knownTanks);
// tankNumber is the number of the tank being programmed
while (tankNumber < numberOfTanks) {
lcd.setCursor(0, 2);
lcd.print("Select tank #");
lcd.print(tankNumber + 1);
lcd.print(" type");
bool buttonPress = false;
bool set = false;
int t = 0; // ''t' is an index to the name of the tank being programmed
while (t < numberOfDescripts) {
lcd.setCursor(0, 3);
lcd.print(tankDescript[t]);
// The Mode button will step through the tank types. When the proper tank
// type is displayed, pressing the Set button will lock it in.
while (!buttonPress) { // Wait for a button press
modeButton.read();
setButton.read();
if (modeButton.wasReleased()) {
Serial.println("Mode pressed");
buttonPress = true;
}
if (setButton.wasReleased()) {
Serial.println("Set pressed");
int ADDR = 20 + (tankNumber * 10); // Memory locations 20 -> 59
tankType[t] = tankDescript[t];
EEPROM.put(ADDR, tankDescript[t]);
Serial.print("Wrote ");
Serial.print(tankDescript[t]);
Serial.print(" at location ");
Serial.println(ADDR);
t = 0;
set = true;
buttonPress = true;
} // End if (setButton.released())
} // End while (!buttonPress)
buttonPress = false;
t++;
if (set) t = numberOfDescripts; // Exit while (t < numberOfDescripts)
} // end of while (t < numberOfDescripts)
if (set) tankNumber++;
} // end of while (tankNumber < numberOfTanks)
} // end of if (knownTanks < numberOfTanks)
Serial.println("Setup Complete");
lcd.clear();
lcd.print(" Complete ");
} // end of setup
void loop() {
} // End of void(loop)