I am building a data logger that reads rpm and voltage from a dc generator and encoder.
I am trying to set up the RTC for time stamping of the data.
All I get as a return is Time 00:00:00 which never changes on my serial port.
If I swap the I2C lines I Get Time 09:00:00 and it increases from there.
I have set the time using the example RTCtest provided with the library.
The battery is inserted in the correct way and it shows 3v when tested.
Here is my code:
Code: Select all
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SD.h>
#include <SoftI2C.h>
#include <DS3232RTC.h>
SoftI2C i2c(SDA, SCL);
DS3232RTC rtc(i2c);
LiquidCrystal_I2C lcd(0x20, 16, 2);
//voltage and rpm of motor
unsigned long start = 0;
const byte encoderPinA = 2;//A pin -> interrupt pin 0
const byte encoderPinB = 4;//B pin -> digital pin 4
volatile long pulse;
volatile bool pinB, pinA, dir;
int interval = 100; //how often you want a reading in ms
int gearRatio = 30; //gear box reduction
int ppr = 32; // encoder pulses per revolution
int sampleRate = 1000/interval*60; // how many samples per minute
int rpm;
float voltsRaw = 0;
float volts = 0;
float analog = 0;
int analogPin = 0;
void setup() {
Wire.begin();
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(" Volts");
lcd.setCursor(0,1);
lcd.print("@ RPM");
attachInterrupt(0, readEncoder, CHANGE);
pinMode(encoderPinA,INPUT);
pinMode(encoderPinB,INPUT);
}
void loop() {
if(millis() - start > interval)
{
start = millis();
rpm = (pulse/ppr)*sampleRate/30;
//Serial.println(rpm);
lcd.setCursor(2,1);
lcd.print(" ");
lcd.setCursor(2,1);
lcd.print(rpm);
pulse = 0;
//analog = analogRead(analogPin);
voltsRaw = analogRead(analogPin);
//voltsRaw = as.smooth(analog);
volts = voltsRaw/40.92;
//Serial.println(volts);
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(volts);
RTCTime time;
rtc.readTime(&time);
Serial.print("Time: ");
printDec2(time.hour);
Serial.print(':');
printDec2(time.minute);
Serial.print(':');
printDec2(time.second);
Serial.println();
}
}
void readEncoder() // reading the encoder each time it registers a tick.
{
pinA = bitRead(PIND,encoderPinA);
pinB = bitRead(PIND,encoderPinB);
dir = pinA ^ pinB; // if pinA & pinB are the same
dir ? --pulse : ++pulse; // dir is CW, else CCW
}
void printDec2(int value)
{
Serial.print((char)('0' + (value / 10)));
Serial.print((char)('0' + (value % 10)));
}