Example - you have the Bluetooth shield connected to your Eleven, and the RX jumper is over 2 and TX jumper over 3 - and the RTC is connected and working with the example sketch:
http://cdn.shopify.com/s/files/1/0045/8 ... ds3232.ino
To send the data out via Bluetooth, we create a new Softwareserial port using the following at the start of the sketch:
Code: Select all
#include <SoftwareSerial.h>
SoftwareSerial bt(2,3); // RX, TX
... and then activate it within void setup() with:
bt.begin(9600);
Now anything that you can send to the serial monitor with Serial.print (etc) you can send out to Bluetooth with
bt.print()
You can see the modifications in the following sketch:
Code: Select all
// DS3232 I2C test with Bluetooth output
#include <SoftwareSerial.h>
SoftwareSerial bt(2,3); // RX, TX
#include "Wire.h"
#define DS3232_I2C_ADDRESS 0x68
// 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) );
}
void setup()
{
Wire.begin();
Serial.begin(9600);
bt.begin(9600);
}
void readDS3232time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3232_I2C_ADDRESS);
Wire.write(0); // set DS3232 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3232_I2C_ADDRESS, 7); // request 7 bytes of data from DS3232 starting from register 00h
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void setDS3232time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
// sets time and date data to DS3232
{
Wire.beginTransmission(DS3232_I2C_ADDRESS);
Wire.write(0); // sends 00h - seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1~31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0~99)
Wire.endTransmission();
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3232
readDS3232time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
// send it to the serial monitor
bt.print(hour, DEC);// convert the byte variable to a decimal number when being displayed
bt.print(":");
if (minute<10)
{
bt.print("0");
}
bt.print(minute, DEC);
bt.print(":");
if (second<10)
{
bt.print("0");
}
bt.print(second, DEC);
bt.print(" ");
bt.print(dayOfMonth, DEC);
bt.print("/");
bt.print(month, DEC);
bt.print("/");
bt.print(year, DEC);
bt.print(" Day of week:");
switch(dayOfWeek){
case 1:
bt.println("Sunday");
break;
case 2:
bt.println("Monday");
break;
case 3:
bt.println("Tuesday");
break;
case 4:
bt.println("Wednesday");
break;
case 5:
bt.println("Thursday");
break;
case 6:
bt.println("Friday");
break;
case 7:
bt.println("Saturday");
break;
}
}
void loop()
{
// setDS3232time(00, 31, 23, 2, 23, 7, 12); // set DS3232 seconds, minutes, hours, day of week, date, month, year
displayTime();
delay(1000);
}
Don't forget to ensure your other device (computer etc) is paired, connected and operating at 9600 bps.