Hello all, I'm new to programming and I got stuck with my first more complex project. I have the following code but everything I put in void loop() is updating once per second if Sw1 is high. I assume that the ONE_HZ_SW is responsible for this behaviour. How should I avoid that? I need other things in the loop to run faster.
Thank you!
#include <Wire.h> // specify use of Wire.h library.
#define ONE_HZ_SW 2 // one Hz square wave from Ds1307
#define blinkPin 13
#define Sw0 4
#define Sw1 5
#define Sw2 6
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(57600);
digitalWrite(blinkPin, 0);
pinMode(ONE_HZ_SW, INPUT_PULLUP);
pinMode(Sw0, INPUT_PULLUP); // for this use a slide switch
pinMode(Sw1, INPUT_PULLUP); // N.O. push button switch
pinMode(Sw2, INPUT_PULLUP); // N.O. push button switch
}
void loop()
{
// wait or HIGH
while(!digitalRead(ONE_HZ_SW)) {
}
Wire.beginTransmission(0x68);
Wire.write(0); // location pointer
Wire.endTransmission();
Wire.requestFrom(0x68, 7); // send 7 bytes
byte secs = Wire.read();
byte mins = Wire.read();
byte hrs = Wire.read();
byte day = Wire.read();
byte date = Wire.read();
byte month = Wire.read();
byte year = Wire.read();
// hours, minutes, seconds
Serial.print("The time is ");
if (hrs < 10) Serial.print("0");
Serial.print(hrs,HEX);
Serial.print(":");
if (mins < 10) Serial.print("0");
Serial.print(mins, HEX);
Serial.print(":");
if (secs < 10) Serial.print("0");
Serial.println(secs, HEX);
// use DD-MM-YYYY
Serial.print("The date is ");
if (date < 10) Serial.print("0");
Serial.print(date, HEX);
Serial.print("-");
if (month < 10) Serial.print("0");
Serial.print(month,HEX);
Serial.print("-");
Serial.print("20");
if (year < 10) Serial.print("0");
Serial.println(year, HEX);
Serial.println();
if (!(digitalRead(Sw0))) set_time(); // hold the switch to set time
while(digitalRead(ONE_HZ_SW)) {
} // wait or LOW
toggle(blinkPin);
}
// toggle the state on a pin
void toggle(int pinNum)
{
int pinState = digitalRead(pinNum);
pinState = !pinState;
digitalWrite(pinNum, pinState);
}
void set_time() {
byte minutes = 0;
byte hours = 0;
while (!digitalRead(Sw0)) // set time switch must be released to exit
{
while (!digitalRead(Sw1)) // set minutes
{
minutes++;
if ((minutes & 0x0f) > 9) minutes = minutes + 6;
if (minutes > 0x59) minutes = 0;
Serial.print("Minutes = ");
if (minutes <= 9) Serial.print("0");
Serial.println(minutes, HEX);
delay(400);
}
while (!digitalRead(Sw2)) // set hours
{
hours++;
if ((hours & 0x0f) > 9) hours = hours + 6;
if (hours > 0x23) hours = 0;
Serial.print("Hours = ");
if (hours <= 9) Serial.print("0");
Serial.println(hours, HEX);
delay(400);
}
Wire.beginTransmission(0x68); // activate DS1307
Wire.write(0); // where to begin
Wire.write(0x00); //seconds
Wire.write(minutes); //minutes
Wire.write(hours); //hours (24hr time)
Wire.write(0x01); // Day 01-07
Wire.write(0x9); // Date 0-31
Wire.write(0x5); // month 0-12
Wire.write(0x16); // Year 00-99
Wire.write(0x10); // Control 0x10 produces a 1 HZ square wave on pin 7.
Wire.endTransmission();
}
}
For example I need to count the millis between the ONE_HZ_SW pulses but everywhere I put the code inside the void loop() Sw3 is read once per second and gives the elapsed value 0
if (!digitalRead(ONE_HZ_SW))
{
start=millis();
}
if (!digitalRead(Sw3))
{
finished=millis();
lcd.clear();
lcd.setCursor(0,1); //Start at character 0 on line 1
lcd.print(elapsed);
}
elapsed=finished-start;
UKHeliBob:
This is a wild guess, but if ONE_HZ_SW does what it seems to then there will be 1000 milliseconds between the pulses.
I need to have a stopwatch to count at millis level with the precision of the RTC. When I press the Sw3, I need to have the last date and time from the RTC plus the millis from the last second pulse til the Sw3 push.
paulus_v:
Is it possible to achieve this using interrupts?
Of course. That is what interrupts are for. Just be sure that you understand what happens when the interrupt happens, what you can, and can not, do in the interrupt handler, and where control returns to when the interrupt handler is done.
paulus_v:
Is it possible to achieve this using interrupts?
Edit: if I count the millis from one pulse and if millis 990 wait for the pulse 20 milis? But I think this will be too complicated for me to write...
I don't know why you are so obsessed with the 1 second timer pulse from the RTC.
Let loop() run freely and check each time through whether the "start timing" button has become pressed. not if it is pressed. When you detect that save the time from milli() and start looking for the "stop timing" button to become pressed. It can be the same button if you like. When you detect that, subtract the start value of millis() from the current value of millis() and you have the elapsed time in milliseconds. Format the value how you would like and display it.
All other stopwatch actions such as lap timing and split timing use the same technique.
UKHeliBob:
I don't know why you are so obsessed with the 1 second timer pulse from the RTC.
Yes I am obsessed, but for a reason.
I do not need a simple stopwatch/ to count the time between 2 button presses but I need, with only one button press, to get the RTC actual time with hundredths of a second precision.
And I will use a more precise temperature compensated RTC instead of the DS1307 when it arrives.
I need, with only one button press, to get the RTC actual time with hundredths of a second precision.
How long does it take to read the current time from the RTC and is the time taken consistent ?
What I have in mind is to let loop() run freely monitoring the state of the button pin. When a button press is detected request the current time from the RTC and apply a correction for the read time if required.
UKHeliBob:
How long does it take to read the current time from the RTC and is the time taken consistent ?
What I have in mind is to let loop() run freely monitoring the state of the button pin. When a button press is detected request the current time from the RTC and apply a correction for the read time if required.
The problem is that the current time from the RTC has only a 'seconds' resolution, but I need to have the hundredths of seconds count for the moment when the button is pressed. The precision of the 'hundredths' is not critical (could be counted by the arduino) but I need it.
My options are:
to find a millisecons resolution RTC;
try to use interrupts to handle the RTC square wave output;
use an arduino only for the above sketch to output e.g. a one second high/ one second low to be put in an other arduino to deal with the rest of the things.
paulus_v:
Is it possible to achieve this using interrupts?
Edit: if I count the millis from one pulse and if millis 990 wait for the pulse 20 milis? But I think this will be too complicated for me to write...
Yes, it is possible and easy. Here is a sample ISR from one of my clock programs:
void rtc_pps_handler()
{
thisRtcTick = millis();
rtc_pps_flag = true; //inform main program that a pulse has been received
}
In loop() you test for the rtc_pps_flag, and then reset it. That will tell you when a second has begun. After that, subtracting thisRtcTick from millis() will give you the milliseconds resolution. It will be accurate regardless of delays in responding to the interrupt in loop().