How to add openlog to mpr121?

Hello,

i have an arduino pro mini interconnected with an mpr121 and can read on the serial monitor when one of the contacts of the mpr121 has been touched.

The code please find below.

When i want that the data from the mpr121 is being written on the sd card of the openlog what might i have to add to my code here?

Tks a lot and regards,

code:

#include "mpr121.h"
#include <Wire.h>
int irqpin = 2;  // Digital 2
boolean touchStates[12]; //to keep track of the previous touch states
void setup(){
  pinMode(irqpin, INPUT);
  digitalWrite(irqpin, HIGH); //enable pullup resistor
  
  Serial.begin(9600);
  Wire.begin();
  mpr121_setup();
}
void loop(){
  readTouchInputs();
}

void readTouchInputs(){
  if(!checkInterrupt()){
    
    //read the touch state from the MPR121
    Wire.requestFrom(0x5A,2); 
    
    byte LSB = Wire.read();
    byte MSB = Wire.read();
    
    uint16_t touched = ((MSB << 8) | LSB); //16bits that make up the touch states
    
    for (int i=0; i < 12; i++){  // Check what electrodes were pressed
      if(touched & (1<<i)){
      
        if(touchStates[i] == 0){
          //pin i was just touched
          Serial.print("pin ");
          Serial.print(i);
          Serial.println(" was just touched");
        
        }else if(touchStates[i] == 1){
          //pin i is still being touched
        }  
      
        touchStates[i] = 1;      
      }else{
        if(touchStates[i] == 1){
          Serial.print("pin ");
          Serial.print(i);
          Serial.println(" is no longer being touched");
          
          //pin i is no longer being touched
       }
        
        touchStates[i] = 0;
      }
    
    }
    
  }
}
 

void mpr121_setup(void){
  set_register(0x5A, ELE_CFG, 0x00); 
  
  // Section A - Controls filtering when data is > baseline.
  set_register(0x5A, MHD_R, 0x01);
  set_register(0x5A, NHD_R, 0x01);
  set_register(0x5A, NCL_R, 0x00);
  set_register(0x5A, FDL_R, 0x00);
  // Section B - Controls filtering when data is < baseline.
  set_register(0x5A, MHD_F, 0x01);
  set_register(0x5A, NHD_F, 0x01);
  set_register(0x5A, NCL_F, 0xFF);
  set_register(0x5A, FDL_F, 0x02);
  
  // Section C - Sets touch and release thresholds for each electrode
  set_register(0x5A, ELE0_T, TOU_THRESH);
  set_register(0x5A, ELE0_R, REL_THRESH);
 
  set_register(0x5A, ELE1_T, TOU_THRESH);
  set_register(0x5A, ELE1_R, REL_THRESH);
  
  set_register(0x5A, ELE2_T, TOU_THRESH);
  set_register(0x5A, ELE2_R, REL_THRESH);
  
  set_register(0x5A, ELE3_T, TOU_THRESH);
  set_register(0x5A, ELE3_R, REL_THRESH);
  
  set_register(0x5A, ELE4_T, TOU_THRESH);
  set_register(0x5A, ELE4_R, REL_THRESH);
  
  set_register(0x5A, ELE5_T, TOU_THRESH);
  set_register(0x5A, ELE5_R, REL_THRESH);
  
  set_register(0x5A, ELE6_T, TOU_THRESH);
  set_register(0x5A, ELE6_R, REL_THRESH);
  
  set_register(0x5A, ELE7_T, TOU_THRESH);
  set_register(0x5A, ELE7_R, REL_THRESH);
  
  set_register(0x5A, ELE8_T, TOU_THRESH);
  set_register(0x5A, ELE8_R, REL_THRESH);
  
  set_register(0x5A, ELE9_T, TOU_THRESH);
  set_register(0x5A, ELE9_R, REL_THRESH);
  
  set_register(0x5A, ELE10_T, TOU_THRESH);
  set_register(0x5A, ELE10_R, REL_THRESH);
  
  set_register(0x5A, ELE11_T, TOU_THRESH);
  set_register(0x5A, ELE11_R, REL_THRESH);
  
  // Section D
  // Set the Filter Configuration
  // Set ESI2
  set_register(0x5A, FIL_CFG, 0x04);
  
  // Section E
  // Electrode Configuration
  // Set ELE_CFG to 0x00 to return to standby mode
  set_register(0x5A, ELE_CFG, 0x0C);  // Enables all 12 Electrodes
  
  
  // Section F
  // Enable Auto Config and auto Reconfig
  /*set_register(0x5A, ATO_CFG0, 0x0B);
  set_register(0x5A, ATO_CFGU, 0xC9);  // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V   set_register(0x5A, ATO_CFGL, 0x82);  // LSL = 0.65*USL = 0x82 @3.3V
  set_register(0x5A, ATO_CFGT, 0xB5);*/  // Target = 0.9*USL = 0xB5 @3.3V
  
  set_register(0x5A, ELE_CFG, 0x0C);
  
}

boolean checkInterrupt(void){
  return digitalRead(irqpin);
}

void set_register(int address, unsigned char r, unsigned char v){
    Wire.beginTransmission(address);
    Wire.write(r);
    Wire.write(v);
    Wire.endTransmission();
}
/*
    MPR121.h
April 8, 2010
    by: Jim Lindblom
    
*/
// MPR121 Register Defines
#define MHD_R    0x2B
#define NHD_R    0x2C
#define    NCL_R     0x2D
#define    FDL_R    0x2E
#define    MHD_F    0x2F
#define    NHD_F    0x30
#define    NCL_F    0x31
#define    FDL_F    0x32
#define    ELE0_T    0x41
#define    ELE0_R    0x42
#define    ELE1_T    0x43
#define    ELE1_R    0x44
#define    ELE2_T    0x45
#define    ELE2_R    0x46
#define    ELE3_T    0x47
#define    ELE3_R    0x48
#define    ELE4_T    0x49
#define    ELE4_R    0x4A
#define    ELE5_T    0x4B
#define    ELE5_R    0x4C
#define    ELE6_T    0x4D
#define    ELE6_R    0x4E
#define    ELE7_T    0x4F
#define    ELE7_R    0x50
#define    ELE8_T    0x51
#define    ELE8_R    0x52
#define    ELE9_T    0x53
#define    ELE9_R    0x54
#define    ELE10_T    0x55
#define    ELE10_R    0x56
#define    ELE11_T    0x57
#define    ELE11_R    0x58
#define    FIL_CFG    0x5D
#define    ELE_CFG    0x5E
#define GPIO_CTRL0    0x73
#define    GPIO_CTRL1    0x74
#define GPIO_DATA    0x75
#define    GPIO_DIR    0x76
#define    GPIO_EN        0x77
#define    GPIO_SET    0x78
#define    GPIO_CLEAR    0x79
#define    GPIO_TOGGLE    0x7A
#define    ATO_CFG0    0x7B
#define    ATO_CFGU    0x7D
#define    ATO_CFGL    0x7E
#define    ATO_CFGT    0x7F

// Global Constants
#define TOU_THRESH    0x06
#define    REL_THRESH    0x0A

jumbo9001:
Hello,

i have an arduino pro mini interconnected with an mpr121 and can read on the serial monitor when one of the contacts of the mpr121 has been touched.

I am confused. Did you change something so that it now works when it didn't at the time of your last post ? In any case I will assume the Arduino and MPR121 are working as they should.

When i want that the data from the mpr121 is being written on the sd card of the openlog what might i have to add to my code here?

Ideally you shouldn't have to add any code at all. Just add a wire from the TX-O from the Arduino to the Openlog's RX-I pin. The Openlog should start logging as soon as it has power and gets input. Anywhere in the code above when you see Serial.print(XXXXXX), that message will be sent to both the Openlog and the serial monitor/PC (if attached). I see a few messages saying that pin Z has been touched, is being (still) touched and has been released (untouched). What else are you looking to log ?

That said there is code you can add if you want to configure the Openlog, the question is what do you want the Openlog to do or not do ? You can add simple Serial.print() statements in your code to do the same things as noted in the Openlog manual.

It shows up at the serial monitor. And for me unkown reason minor part of the sensor data then is only written on the openlog.

so i connected the RXI of openlog now with TXO of Arduino. And i connected the TXO of the openlog with RXI of the Arduino, and so that openlog is able to run i connected VCC of openlog with 3.3V of the mpr121 and the gnd of openlog with the GND of the Arduino. The sensors show up in the serial monitor, but then after some time i get error message and the serial monitor stops working show up below in the IDE line like this

: at gnu.io.RXTXPort.send Event(RTXport.java:732)

at gnu.io.RXTXPort.eventLoop(Native Method)

at gnu.io.RXTXPort$MonitorThread.run (RXTXPort.java:1575)

mistake in Serial .serialEvent()

Ok it now it works. I dont understand why it works but it works, and everything is also being written on openlog directly.

I just released the wiring of the sensors and touched the pins of the mpr121 directly. Probably my wiring was not proper isolated.

I have a little Lion battery 110mAh 3.7V. How do i connect the battery with the arduino and can i then release the ftdi connection with the computer and the data is still being written on the openlog?

the battery needs to be connected with the GND and the VCC of the arduino pro mini is that correct?

On the battery there is a red an a black cable, which one to be connected to the gnd and which one to the vcc or does this not matter?

The black cable as i see this here looks to be the -Pol and the red cable the +Pol.

I’ll assume you’re using the 8MHz/3.3V Pro Mini. It has a very low drop-out voltage regulator on it that I think you should use. What that means (practically speaking) is that the Mini, the Openlog, the MPR121 and the battery should all share a common ground. If you connect the black battery wire (pol -) to any Mini GND and then connect the Openlog and MPR121 GND’s to the other Mini GND pins (there are 3) … then every device shares a common ground.

Your LiPo battery, when fully charged, might output a voltage as high as 4.2V. Then as you draw current from it the voltage will decrease to … well pretty low, but you should stop using it when the voltage gets to 3V … perhaps even higher, so as not to damage the battery. The Openlog has the same voltage regulator as on the Mini and so can handle the “high” (>3.3V) voltage. The MPR121 does not have a regulator and so needs a proper regulated 3.3V as it’s power source. Luckily you can get that from the Mini.

So take the battery red wire (pol +) and tack 2 wires onto it (by whatever means) so the battery now has 2 pol+ output wires. Run 1 of those to the Openlog’s VCC input (see Openlog manual below). That, along with the common GND, will provide power to the Openlog. The other pol+ wire should go the the Mini’s RAW input. That, along with the common GND, will power the Mini. The Mini then has 2 output pins labelled VCC. Yes, that can be confusing given that VCC is an input on the Openlog, but looking at the Mini’s schematic … that’s the way it is, VCC the output of the voltage regulator. Now run a wire from the Mini’s VCC output pin to the MPR121’s input pin labelled 3.3V. That, along with the common GND, will power the MPR121.

http://dlnmh9ip6v2uc.cloudfront.net/dat … ni-v13.pdf

https://github.com/sparkfun/OpenLog/wiki/datasheet

How do i add a sparkfun realtime clock to the arduino pro mini which itself runs on 3.3v?

Wiring like this?

REAL TIME CLOCK → ARDUINO

SDA → A5

SCL → A4

GND → GND

5V → VCC

And how do i adjust the correct time there?

I got here some possible codes to use but dont understand which one makes most sense

http://bildr.org/2011/03/ds1307-arduino/

http://wiring.org.co/learning/libraries … clock.html

bst rgds

Your connections look proper but I caution that you’ll be using 3.3 signalling levels to the RTC due to the MPR121 using the same I2C lines. That should be OK as 3.3V is > than the 2.5V needed to make a logic 1. That said, read the product page on the DS1307. IIRC there was some concern about being able to program the time (?and other?) register(s) using 3.3V levels (makes no sense to me). As far as coding goes, got me, I’ve not used the device before.

ok, will focus on the batteries before.

i dont understand how do i connect proper a lithium ion battery with wire and then connect it to the arduino?

as on the picture enclosed. Should i ripp off this white thing at the end of the red and black cable and then solder the cables with the wire together and to the arduino then as well?

For what use is this white thing there, or should the wire there be put in some holes there?

tks

The white thing is a connector. Ideally you should attach your wires to a mating connector, one that plugs into the white thing. If you take it off, how are you going to charge up the battery ? I’m sure that most chargers come with a mating connector. What you should do is look to replace the power wires to your Arduino with something like this (I’m not sure it’s exactly the proper mating connector)

https://www.sparkfun.com/products/8670

https://dlnmh9ip6v2uc.cloudfront.net/im … L_i_ma.jpg

http://en.wikipedia.org/wiki/Electrical_connector

ok tks. This is probably the angle connector. But how do i solder correctly this connector to the arduino pro mini?

I thought wrong. I mean i practically just need a connector and then solder there another wire which i then solder to the needed components vcc pin and so on right?

I would probably rather need this JST Right-Angle Connector - Through-Hole 2-Pin right and then solder it with another wire right or maybe connect a cable on it when there is one that matches.

This should be possible to be connected right? Or would you right away solder the upper part of the angle connector?

jumbo9001:
I would probably rather need this JST Right-Angle Connector - Through-Hole 2-Pin right and then solder it with another wire right or maybe connect a cable on it when there is one that matches.

IF that is the proper mating connector (my guess is that it is) then you would solder that directly to the Arduino Pro that I believe you are using. It would be soldered directly to the PWB at the RAW and GND holes. Or you can find a wired, not-right-angle connector like I originally showed and solder the bare wire ends to the same holes. Just be very very very sure that whatever you do, you get the polarity correct. That the battery + (red wire) ends up connected to the Arduino RAW and the battery - wire (black) gets connected to the Arduino GND. If you get it backwards, even once, you'll do bad things to the Arduino and make it into trash. If you use the right angle connector, make sure you solder it to the proper side of the PWB. Soldering it to the top side will have a different polarity from soldering it to the bottom side. If you use the vertical connector, make sure it twisted correctly so the polarity is correct.

I think you should read this tutorial carefully.

https://learn.sparkfun.com/tutorials/co … troduction

And this page (which pertains to your battery I think)

https://learn.sparkfun.com/tutorials/ba … um-polymer

One last thing … the distance btw the pins on the JST connectors is 2.0 mm. The standard for the holes on the Pro Mini is 0.1" or 2.5 mm. There’s probably enough “slop” in the hole to allow a JST to be soldered to the PWB but you may find it easier to use the connector with wires and just solder the wires to the holes. This is also a surer way to get the polarity correct. You can plug your new connector into the battery and then use a DVM to check the voltage at the end of the bare wires before you solder them to the Arduino. You can cut the wires to a shorter length if you want to. The only problem is that I don’t know if SF (or anyone) sells wired connectors that have the proper gender to mate with the battery. You may be stuck using either the right angle or vertical through hole JST connecter, soldered to the PWB.

Hi,

maybe this tutorial here would fit better? http://wiring.org.co/learning/libraries … clock.html

So i have arduino pro mini, mpr121 and the openlog connected and everything is running perfect also with battery.

I want to add the RTClock control orientating on the tutorial above.

What do you think?

So i would connect the RTClock with Digitalpins 8 and 9 here.

I have a question. So this is my existing and well working code for the arduino-mpr121-openlog operation:

mpr121

#include "mpr121.h"
#include
int irqpin = 2;  // Digital 2
boolean touchStates[12]; //to keep track of the previous touch states
void setup(){
  pinMode(irqpin, INPUT);
  digitalWrite(irqpin, HIGH); //enable pullup resistor
  
  Serial.begin(9600);
  Wire.begin();
  mpr121_setup();
}
void loop(){
  readTouchInputs();
}

void readTouchInputs(){
  if(!checkInterrupt()){
    
    //read the touch state from the MPR121
    Wire.requestFrom(0x5A,2); 
    
    byte LSB = Wire.read();
    byte MSB = Wire.read();
    
    uint16_t touched = ((MSB << 8) | LSB); //16bits that make up the touch states
    
    for (int i=0; i < 12; i++){  // Check what electrodes were pressed
      if(touched & (1<       
        if(touchStates[i] == 0){
          //pin i was just touched
          Serial.print("pin ");
          Serial.print(i);
          Serial.println(" was just touched");
        
        }else if(touchStates[i] == 1){
          //pin i is still being touched
        }  
      
        touchStates[i] = 1;      
      }else{
        if(touchStates[i] == 1){
          Serial.print("pin ");
          Serial.print(i);
          Serial.println(" is no longer being touched");
          
          //pin i is no longer being touched
       }
        
        touchStates[i] = 0;
      }
    
    }
    
  }
}
 

void mpr121_setup(void){
  set_register(0x5A, ELE_CFG, 0x00); 
  
  // Section A - Controls filtering when data is > baseline.
  set_register(0x5A, MHD_R, 0x01);
  set_register(0x5A, NHD_R, 0x01);
  set_register(0x5A, NCL_R, 0x00);
  set_register(0x5A, FDL_R, 0x00);
  // Section B - Controls filtering when data is < baseline.
  set_register(0x5A, MHD_F, 0x01);
  set_register(0x5A, NHD_F, 0x01);
  set_register(0x5A, NCL_F, 0xFF);
  set_register(0x5A, FDL_F, 0x02);
  
  // Section C - Sets touch and release thresholds for each electrode
  set_register(0x5A, ELE0_T, TOU_THRESH);
  set_register(0x5A, ELE0_R, REL_THRESH);
 
  set_register(0x5A, ELE1_T, TOU_THRESH);
  set_register(0x5A, ELE1_R, REL_THRESH);
  
  set_register(0x5A, ELE2_T, TOU_THRESH);
  set_register(0x5A, ELE2_R, REL_THRESH);
  
  set_register(0x5A, ELE3_T, TOU_THRESH);
  set_register(0x5A, ELE3_R, REL_THRESH);
  
  set_register(0x5A, ELE4_T, TOU_THRESH);
  set_register(0x5A, ELE4_R, REL_THRESH);
  
  set_register(0x5A, ELE5_T, TOU_THRESH);
  set_register(0x5A, ELE5_R, REL_THRESH);
  
  set_register(0x5A, ELE6_T, TOU_THRESH);
  set_register(0x5A, ELE6_R, REL_THRESH);
  
  set_register(0x5A, ELE7_T, TOU_THRESH);
  set_register(0x5A, ELE7_R, REL_THRESH);
  
  set_register(0x5A, ELE8_T, TOU_THRESH);
  set_register(0x5A, ELE8_R, REL_THRESH);
  
  set_register(0x5A, ELE9_T, TOU_THRESH);
  set_register(0x5A, ELE9_R, REL_THRESH);
  
  set_register(0x5A, ELE10_T, TOU_THRESH);
  set_register(0x5A, ELE10_R, REL_THRESH);
  
  set_register(0x5A, ELE11_T, TOU_THRESH);
  set_register(0x5A, ELE11_R, REL_THRESH);
  
  // Section D
  // Set the Filter Configuration
  // Set ESI2
  set_register(0x5A, FIL_CFG, 0x04);
  
  // Section E
  // Electrode Configuration
  // Set ELE_CFG to 0x00 to return to standby mode
  set_register(0x5A, ELE_CFG, 0x0C);  // Enables all 12 Electrodes
  
  
  // Section F
  // Enable Auto Config and auto Reconfig
  /*set_register(0x5A, ATO_CFG0, 0x0B);
  set_register(0x5A, ATO_CFGU, 0xC9);  // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V   set_register(0x5A, ATO_CFGL, 0x82);  // LSL = 0.65*USL = 0x82 @3.3V
  set_register(0x5A, ATO_CFGT, 0xB5);*/  // Target = 0.9*USL = 0xB5 @3.3V
  
  set_register(0x5A, ELE_CFG, 0x0C);
  
}

boolean checkInterrupt(void){
  return digitalRead(irqpin);
}

void set_register(int address, unsigned char r, unsigned char v){
    Wire.beginTransmission(address);
    Wire.write(r);
    Wire.write(v);
    Wire.endTransmission();
}

and

mpr121.h

/*
    MPR121.h
    April 8, 2010
    by: Jim Lindblom
*/
// MPR121 Register Defines
#define MHD_R    0x2B
#define NHD_R    0x2C
#define    NCL_R     0x2D
#define    FDL_R    0x2E
#define    MHD_F    0x2F
#define    NHD_F    0x30
#define    NCL_F    0x31
#define    FDL_F    0x32
#define    ELE0_T    0x41
#define    ELE0_R    0x42
#define    ELE1_T    0x43
#define    ELE1_R    0x44
#define    ELE2_T    0x45
#define    ELE2_R    0x46
#define    ELE3_T    0x47
#define    ELE3_R    0x48
#define    ELE4_T    0x49
#define    ELE4_R    0x4A
#define    ELE5_T    0x4B
#define    ELE5_R    0x4C
#define    ELE6_T    0x4D
#define    ELE6_R    0x4E
#define    ELE7_T    0x4F
#define    ELE7_R    0x50
#define    ELE8_T    0x51
#define    ELE8_R    0x52
#define    ELE9_T    0x53
#define    ELE9_R    0x54
#define    ELE10_T    0x55
#define    ELE10_R    0x56
#define    ELE11_T    0x57
#define    ELE11_R    0x58
#define    FIL_CFG    0x5D
#define    ELE_CFG    0x5E
#define GPIO_CTRL0    0x73
#define    GPIO_CTRL1    0x74
#define GPIO_DATA    0x75
#define    GPIO_DIR    0x76
#define    GPIO_EN        0x77
#define    GPIO_SET    0x78
#define    GPIO_CLEAR    0x79
#define    GPIO_TOGGLE    0x7A
#define    ATO_CFG0    0x7B
#define    ATO_CFGU    0x7D
#define    ATO_CFGL    0x7E
#define    ATO_CFGT    0x7F

// Global Constants
#define TOU_THRESH    0x06
#define    REL_THRESH    0x0A

The code below from the RTClock Tutorial in what way do i have to put it in my existing mpr121 code above? Do i just have to add to my #include section from my mpr121 code the rtc #include part and then do so with the others?

I dont understand how to copy what part of the rtclock tutorial code in what parts of the mpr121 code? Maybe u can advise.

Tks a lot and best regards

RTClock code:

#include 

int clockAddress = 0x68;  // This is the I2C address
int command = 0;  // This is the command char, in ascii form, sent from the serial port     
long previousMillis = 0;  // will store last time Temp was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test; 

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers, 
// Probably need to put in checks for valid numbers.
void setDateDs1307()                
{
  // Use of (byte) type casting and ascii math to achieve result.  
  second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); 
  minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  dayOfWeek = (byte) (Serial.read() - 48);
  dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  Wire.beginTransmission(clockAddress);
  Wire.write(byte(0x00));
  Wire.write(decToBcd(second));  // 0 to bit 7 starts the clock
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));    // If you want 12 hour am/pm you need to set
  // bit 6 (also need to change readDateDs1307)
  Wire.write(decToBcd(dayOfWeek));
  Wire.write(decToBcd(dayOfMonth));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.endTransmission();
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307() {
  // Reset the register pointer
  Wire.beginTransmission(clockAddress);
  Wire.write(byte(0x00));
  Wire.endTransmission();

  Wire.requestFrom(clockAddress, 7);

  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(Wire.read() & 0x7f);
  minute     = bcdToDec(Wire.read());
  
  // Need to change this if 12 hour am/pm
  hour       = bcdToDec(Wire.read() & 0x3f);  
  dayOfWeek  = bcdToDec(Wire.read());
  dayOfMonth = bcdToDec(Wire.read());
  month      = bcdToDec(Wire.read());
  year       = bcdToDec(Wire.read());

  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(year, DEC);

}


void setup() {
  Wire.begin();
  Serial.begin(57600);
}

void loop() {
  if (Serial.available()) {  // Look for char in serial que and process if found
    command = Serial.read();
    if (command == 84) {      //If command = "T" Set Date
      setDateDs1307();
      getDateDs1307();
      Serial.println(" ");
    }
    else if (command == 81) {  //If command = "Q" RTC1307 Memory Functions
      delay(100);     
      if (Serial.available()) {
        command = Serial.read(); 
        
        // If command = "1" RTC1307 Initialize Memory - All Data will be set to 255 (0xff).  
        // Therefore 255 or 0 will be an invalid value.  
        if (command == 49) { 
          
          // 255 will be the init value and 0 will be cosidered an error that 
          // occurs when the RTC is in Battery mode. 
          Wire.beginTransmission(clockAddress);
          
          // Set the register pointer to be just past the date/time registers.
          Wire.write(byte(0x08));  
          for (int i = 1; i <= 27; i++) {
            Wire.write(byte(0xff));
            delay(100);
          }   
          Wire.endTransmission();
          getDateDs1307();
          Serial.println(": RTC1307 Initialized Memory");
        }
        else if (command == 50) {      //If command = "2" RTC1307 Memory Dump
          getDateDs1307();
          Serial.println(": RTC 1307 Dump Begin");
          Wire.beginTransmission(clockAddress);
          Wire.write(byte(0x00));
          Wire.endTransmission();
          Wire.requestFrom(clockAddress, 64);
          for (int i = 1; i <= 64; i++) {
            test = Wire.read();
            Serial.print(i);
            Serial.print(":");
            Serial.println(test, DEC);
          }
          Serial.println(" RTC1307 Dump end");
        } 
      }  
    }
    Serial.print("Command: ");
    Serial.println(command);  // Echo command CHAR in ascii that was sent
  }

  command = 0;  // reset command                  
  delay(100);
}