Friday 9 August 2013

SavingsBox


#include <EEPROMEx.h>
#include <LiquidCrystal.h>
#include <Keypad.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(0, 1, 5, 4, 3, 13);

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int i = 0;
float Total;
float Save = 0;
int value[4] = {0};
float pos[3] = {0};
int keypress = 0;
int address = 0;
int pbIn = 0;

void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Total = EEPROM.readFloat(address);
  lcd.setCursor(0,0);
  lcd.print("Total amount");
  lcd.setCursor(0,1);
  lcd.print("saved is $");
  lcd.print(Total);
  delay(3000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Enter new amount");
  lcd.setCursor(0,1);
  lcd.print("$");
  attachInterrupt(pbIn, clearMemory, RISING);
}

void loop()
{
 i = 0;
 while (keypress <= 3)
 {
  char key = keypad.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '#':
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Enter new amount");
        lcd.setCursor(0,1);
        lcd.print("$");
        i = 0;          //returns to the first variable in array 'value'
        keypress = 0;
        break;
      case '*':
        if (keypress < 2)
          {lcd.clear();
           lcd.setCursor(0,0);
           lcd.print("Enter 0 before ");
           lcd.setCursor(0,1);
           lcd.print("$1 to $9 ");
           i = 0;
           keypress = 0;
           delay(2500);
           lcd.clear();
           lcd.setCursor(0,0);
           lcd.print("Press '#' to ");
           lcd.setCursor(0,1);
           lcd.print("re-enter.");}
        break;
      default:
        value[i] = key;
        i ++;
        lcd.print(key);
        keypress ++;
      if (keypress == 2)
           {lcd.print(".");}
        break;
     }
   }

  delay(100);
 }
  lcd.setCursor(0,0);
  lcd.print("Press * to save ");
  lcd.setCursor(0,1);
  lcd.print("or # to clear");

char key = keypad.getKey();
  if(key)  // Check for a valid key.
  {
    keypress = 0;
    if (key == '#')
    {lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Enter new amount");
    lcd.setCursor(0,1);
    lcd.print("$");}
    else if (key == '*')
    {
      pos[0] = (value[0] - 48) * 10;
      pos[1] = (value[1] - 48) * 1;
      pos[2] = (value[2] - 48) * 0.10;
      pos[3] = (value[3] - 48) * 0.01;
      Save  = pos[0] + pos[1] + pos[2] + pos[3];
      Total = EEPROM.readFloat(address);
      Total = Total + Save;
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Saved $");
      lcd.print(Save);
      lcd.setCursor(0,1);
      lcd.print("Total $");
      lcd.print(Total);
      delay(3000);
      EEPROM.writeFloat(address,Total);
      delay(500);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Saved. You may");
      //lcd.print("Press any key to");
      lcd.setCursor(0,1);
      lcd.print("now power off");
    }
  }
}

void clearMemory()
 {  EEPROM.writeFloat(address,0);
    Total = EEPROM.readFloat(address);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Memory erased,");
    lcd.setCursor(0,1);
    lcd.print("Please power off ");
    char key = keypad.waitForKey();
 }