Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Saturday, 28 February 2015

DS1302RTC Display on OLED






// How to use DS1302-library to make a quick
// clock using a DS1302 and a I2C OLED.

// How to connect the DS1302 and OLED.
// DS1302:    CE pin         -> Arduino Digital 2  :also called rst
//                   I/O pin        -> Arduino Digital 3  :also called dat
//                   SCLK pin    -> Arduino Digital 4  :also called clk
//                   GND pin      -> Arduino GND
// OLED:       SCL pin       -> Arduino SCL
//                   SDA pin      -> Arduino SDA
//                   VCC pin      -> Arduino 5V
//                   GND pin      -> Arduino GND

#include <LiquidCrystal.h>
#include <DS1302.h>
#include "U8glib.h"

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI

// Init the DS1302
DS1302 rtc(2, 3, 4);


void setup()
{
  // Set the clock to run-mode, and disable the write protection
  rtc.halt(false);
  rtc.writeProtect(false);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }

  // The following lines are initially needed to set the time but should be commented out
  // and reuploaded to use the values already stored in the DS1302
  //rtc.setDOW(SUNDAY);        // Set Day-of-Week to FRIDAY
  //rtc.setTime(15, 39, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(28, 12, 2014);   // Set the date to August 6th, 2010
}

void loop()
{
  u8g.firstPage();
  do {
    draw();
  } while( u8g.nextPage() );

  // rebuild the picture after some delay
  delay(50);
}

void draw(void) {
  u8g.setFont(u8g_font_courB14r);  //change the font see notes
  u8g.setFontPosTop();  //reference position for a text
  u8g.drawStr( 0, 0, rtc.getTimeStr());//changing the two numbers adjusts the position of the words
  u8g.drawStr( 0, 22, rtc.getDOWStr());
  u8g.drawStr( 0, 44,rtc.getDateStr());
}

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();
 }