Hello,
I just started using Arduino this Monday and I like it a lot, but I'm stumped. I've done research and really am using this as a last life line. So I'll describe my project thus far:
I have a IR LED powered on and shining directly to a photo-transistor, I also have a DS1307 RTC breakout board that is synced to my computer time connecting into my bread board / Arduino.
Now when the photo-transistor registers high, on I/O 2 of the Arduino board, I have the current date, time, and number of times the pin was set high display on the serial monitor. If the pin remains high it does not continue the count or time-stamp. Only when I block the IR light and then unblock it, (pin going from high to low, back to high) does it increase the count and time-stamp.
I'll post my code below so it's easier to discuss my issue:
-----------------------------------------------------------
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <TimeAlarms.h>
// this constant won't change:
const int photoGate = 2; // the pin that phototransistor's emmit pin is attached to
// Variables will change:
int IRinteruptCounter = 0; // counter for the number of interupts in IR light
int gateState = 0; // current state of the photo gate
int lastGateState = 0; // previous state of the photo gate
void setup(){
Serial.begin(9600);
while (!Serial);
delay(200);
pinMode(photoGate, INPUT);
setSyncProvider(RTC.get); // the function to sync the time from the RTC
}
void loop(){
tmElements_t tm;
gateState = digitalRead(photoGate);
if (RTC.read(tm)){
if (gateState != lastGateState){
if(gateState == HIGH) {
time_t timeNow = now();
time_t timeSec = second(timeNow);
if (timeSec == 59){
IRinteruptCounter = 0;
}
countInterupts();
showEvent(timeNow);
}
else {
Serial.println("off");
}
lastGateState = gateState;
}
}
else{
if (RTC.chipPresent()){
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
}
}
void countInterupts(){
IRinteruptCounter++;
Serial.println("on");
Serial.print("number of interupts: ");
Serial.print (IRinteruptCounter);
Serial.println();
}
void print2digits(int number){
if (number >= 0 && number <10){
Serial.write('0');
}
Serial.print(number);
}
void showEvent(time_t t){
Serial.print("Ok, Time = ");
print2digits(hour(t));
Serial.write(':');
print2digits(minute(t));
Serial.write(':');
print2digits(second(t));
Serial.print(", DATE (D/M/Y) = ");
Serial.print(day(t));
Serial.write('/');
Serial.print(month(t));
Serial.write('/');
Serial.print((year(t)));
Serial.println();
}
So, what I would like to do is count the impulses/minute (ideally per second but I think I'd need a new clock and it would be over kill!)
The issue is that the current way that I have it set up:
if (timeSec == 0){
IRinteruptCounter = 0;
}
It will reset the count, but for the entire ":00" second mark it will keep resetting the count, leaving IRinteruptCounter = 1 . When all I want it to do is that when it reaches ":00 seconds" it resets the count once and then continues, cause I do not want to lose the count at that second. I've considered interrupts but only have managed to understand them working with the Arduino clock and not knowing how to sync it with my RTC.
Any ideas on how to get impulses/minute while synced to an RTC without freezing at the ":00 second" mark? Any insight would help, thank you very much in advance