2012/05/22

Sketch for Testing Freeduino/Arduino LCD Keypad Shield

Pins Used

D8, D9, D4, D5, D6, D7, A0

Serial Monitor Output
Sketch


/*
  ASCII table

 Prints out byte values in all possible formats:  
 * as raw binary values
 * as ASCII-encoded decimal, hex, octal, and binary values

 For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII

 The circuit:  No external hardware needed.

 created 2006
 by Nicholas Zambetti 
 modified 18 Jan 2009
 by Tom Igoe

 modified by Befun Hung on Sunday, 20 May 2012
 file name: LCDKeypadShieldTestKeyNone.pde
 for use in testing 16x2 LCD module
 added codes from acw@home (HKEPC Hardware) to test Keypad on Monday, 21 May 2012
 added codes for debouncing and key touching on Tuesday, 22 May 2012

 This example code is in the public domain.
 The sketch works on Arduino IDE 0022

 <http://www.zambetti.com> 

 */
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// analog PIN A0
#define KEY_PIN 0
#define KEY_NONE 1020
int key;
int last_key = -1; //save the last key pressed

void setup() 

  Serial.begin(9600); 
  lcd.begin(16, 2);
  last_key = KEY_NONE;
  lcd.clear();

  // prints title with ending line break 
  // Serial.println("ASCII Table ~ Character Map"); 


// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33; 
// you can also write ASCII characters in single quotes.
// for example. '!' is the same as 33, so you could also use this:
//int thisByte = '!';  

void loop() 

  // prints value unaltered, i.e. the raw binary version of the 
  // byte. The serial monitor interprets all bytes as 
  // ASCII, so 33, the first number,  will show up as '!' 
  
  // lcd.clear();
  for (int i=0;i<16;i++)
  {
    Serial.print(thisByte, BYTE); 
    lcd.setCursor(i, 0);
    lcd.print(char(thisByte));
    lcd.setCursor(i, 1);
    lcd.print(char(thisByte));
    delay(50);
  }
  Serial.println();
  // Serial.print(", dec: "); 
  // prints value as string as an ASCII-encoded decimal (base 10).
  // Decimal is the  default format for Serial.print() and Serial.println(),
  // so no modifier is needed:
  // Serial.print(thisByte);      
  // But you can declare the modifier for decimal if you want to.
  //this also works if you uncomment it:

  // Serial.print(thisByte, DEC);  


  // Serial.print(", hex: "); 
  // prints value as string in hexadecimal (base 16):
  // Serial.print(thisByte, HEX);     

  // Serial.print(", oct: "); 
  // prints value as string in octal (base 8);
  // Serial.print(thisByte, OCT);     

  // Serial.print(", bin: "); 
  // prints value as string in binary (base 2) 
  // also prints ending line break:
  // Serial.println(thisByte, BIN);   

  // if printed last visible character '~' or 126, stop: 
  if(thisByte == 126) {     // you could also use if (thisByte == '~') {
    lcd.clear();
    // This loop loops forever and does nothing
    while(true) {
      key = analogRead(KEY_PIN);
      delay(200); // for debouncing
      if (key <= KEY_NONE && key != last_key) {
        lcd.setCursor(0,1);
        lcd.print(key);
        lcd.print("    ");
        Serial.println(key);
        last_key = key;
      }
      continue; 
    } 
  } 
  // go on to the next character
  thisByte++;  
  delay(100);
}

2012/05/02

Freeduino/Arduino Project Box

Freeduino/Arduino Project Box (300 NTD, about 10 USD sold at 4F, Guanghua Digital Plaza, Taipei, Taiwan) constructed from flame retardant ABS plastic opened with Arduino and RTC Sensor Shield in it.
Arduino Project Box closed.

2012/05/01

Freeduino/Arduino Data Logger Shield Creating File Name Combining Date And Time With Timestamp

Created data file name: MMDDhhmm.txt where M means month, D means day , h means hour and m means minute respectively. Once the reset on Arduino is pressed, a new logger file is created.

An alternative is to use a DS1307 RTC shield (such as RTC Sensor Shield) and a SD shield.

Datalogger Shield



Pins Used

Analog pins 4 and 5 are used for I2C protocol implemented by Maxim DS1307.
Digital pins 10, 11, 12 and 13 are used for SPI protocol to save data file to micro SD card.

RTClib.h download web page

http://www.ladyada.net/make/logshield/download.html

Sketch

// File Name: DataloggerFileNameMMDDHHMM.pde
// The sketch works on Arduino IDE 0022
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>

File file;                                // test file
const uint8_t SD_CS = 10; // SD chip select
RTC_DS1307 RTC;            // define the Real Time Clock object
String file_name = "";          // file name should not prefix with "prefix_word"
char fn[] = "MMDDHHMM.txt";
int i=0;
//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
  DateTime now = RTC.now();

  // return date using FAT_DATE macro to format fields
  *date = FAT_DATE(now.year(), now.month(), now.day());

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(now.hour(), now.minute(), now.second());
}
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Wire.begin();
  if (!RTC.begin()) {
    Serial.println("RTC failed");
    while(1);
  };
  // set date time callback function
  SdFile::dateTimeCallback(dateTime);

  // following line sets the RTC to the date & time this sketch was compiled
  // RTC.adjust(DateTime(__DATE__, __TIME__));
  DateTime now = RTC.now();

  if (now.month() < 10) {
      file_name = String(file_name + '0' + String(now.month(), DEC));
    }
    else {
    file_name = String(file_name +String(now.month(), DEC));
    }
  if (now.day() < 10) {
      file_name = String(file_name + '0' + String(now.day(), DEC));
    }
    else {
    file_name = String(file_name +String(now.day(), DEC));
    }
  if (now.hour() < 10) {
      file_name = String(file_name + '0' + String(now.hour(), DEC));
    }
    else {
    file_name = String(file_name +String(now.hour(), DEC));
    }
  if (now.minute() < 10) {
      file_name = String(file_name + '0' + String(now.minute(), DEC));
    }
    else {
    file_name = String(file_name +String(now.minute(), DEC));
    }

  file_name = String(file_name + ".txt");
  // Serial.println(file_name);
  // Serial.println(file_name.length());

  for (i=0;i<=file_name.length();i++) {
    fn[i] = file_name.charAt(i);
    // Serial.print(file_name.charAt(i));
  }
  // Serial.println(fn);

  if (!SD.begin(SD_CS)) {
    Serial.println("SD failed");
    while(1);
  }
  Serial.print("File Name: ");
  Serial.println(fn);
  //file = SD.open(fn, FILE_WRITE);
  //file.close();
  // Serial.println("Done");
}
//------------------------------------------------------------------------------
void loop() {
  DateTime now = RTC.now();

  String data_string = "";
  data_string = String(now.year(), DEC);
  data_string += "/";
  if (now.month() < 10) {
      data_string = String(data_string + '0' + String(now.month(), DEC));
    }
    else {
    data_string = String(data_string +String(now.month(), DEC));
    }
  data_string += "/";
  if (now.day() < 10) {
      data_string = String(data_string + '0' + String(now.day(), DEC));
    }
    else {
    data_string = String(data_string +String(now.day(), DEC));
    }
  data_string += " ";
  if (now.hour() < 10) {
      data_string = String(data_string + '0' + String(now.hour(), DEC));
    }
    else {
    data_string = String(data_string +String(now.hour(), DEC));
    }
  data_string += ":";
  if (now.minute() < 10) {
      data_string = String(data_string + '0' + String(now.minute(), DEC));
    }
    else {
    data_string = String(data_string +String(now.minute(), DEC));
    }
  data_string += ":";  
  if (now.second() < 10) {
      data_string = String(data_string + '0' + String(now.second(), DEC));
    }
    else {
    data_string = String(data_string +String(now.second(), DEC));
    }
  data_string += ",";

  //read sensor value from A0-A3 and append to the string
  for (int analogPin = 0; analogPin < 4; analogPin++)
    {
    int sensor = analogRead(analogPin);
    data_string += String(sensor);
    if (analogPin < 3) {
      data_string += ",";
      }
    }

  file = SD.open(fn, FILE_WRITE);
  if (file) {
    file.println(data_string);
    file.close();
    Serial.println(data_string);
    }
    else {
      Serial.print("error opening ");
      Serial.println(fn);
    }
  //Serial.println(data_string);
 
  delay(995);
}

Freeduino/Arduino RTC Sensor Shield


RTC Sensor Shield in Freeduino (Arduino compatible) Project Box with 9V battery holder besides.

Freeduino/Arduino RTC Sensor Shield To Display Date And Time In Arduino Serial Monitor Using Wire.h

Read the following link: An I2C Bus Example Using the DS1307 Real-Time Clock

http://www.glacialwanderer.com/hobbyrobotics/?p=12

Freeduino/Arduino RTC Sensor Shield To Display Date and Time In Arduino Serial Monitor Using Wire.h + RTClib.h

First, download RTClib.h from the following link.

There is a new version using Time.h library and showing day of week.

Pins Used

Analog pins 4 and 5 are used for I2C protocol implemented  by Maxim DS1307.


Serial Monitor Output


Sketch

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// 2010-02-04 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
// $Id: ds1307.pde 4773 2010-02-04 14:09:18Z jcw $
// Revised by Befun Hung <bfhorng@ms4.hinet.net> 2011-11-13
// RTClib.h download webpage http://www.ladyada.net/make/logshield/download.html
// File Name: DS1307RTClib.pde - Display Date And Time in Arduino Serial Monitor
// The sketch works on Arduino IDE 0022

#include <Wire.h>
#include <RTClib.h>

RTC_DS1307 RTC;

void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
 
    // following line sets the RTC to the date & time this sketch was compiled
    // RTC.adjust(DateTime(__DATE__, __TIME__));
}

void loop () {
    DateTime now = RTC.now();
 
    Serial.print(now.year(), DEC);
    Serial.print('/');
    if (now.month() < 10) {
      Serial.print('0');
    }
    Serial.print(now.month(), DEC);
    Serial.print('/');
    if (now.day() < 10) {
      Serial.print('0');
    }
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    if (now.hour() < 10) {
      Serial.print('0');
    }
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    if (now.minute() < 10) {
      Serial.print('0');
    }
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    if (now.second() < 10) {
      Serial.print('0');
    }
    Serial.print(now.second(), DEC);
    Serial.println();
    delay(995);
 
}