I would like some help please.
My clock is working on my Nano, but now I want to add a toggle switch to change the hour every six months for Daylight Savings (summer time?).
When the switch is down, the hour stays the same. But when the switch is up, the hour should increase by one.
Here's what I have...
int switchPin = 3; // switch D3 pin 6
int switchState = 0;
void setup()
{
pinMode(switchPin, INPUT_PULLUP);
}
void loop()
{
// Get the current time
DateTime now = rtc.now();
int h = now.hour();
int m = now.minute();
switchState = digitalRead(switchPin); // read the switch input pin:
{
if (switchState == HIGH) // if the switch is HIGH, increment the hour - if LOW, do nothing
h = h + 1;
else
}
}
Nothing happens when I toggle the switch. Can someone see what's wrong?
I am using a 10K resister between ground and Pin 6 (D3)
You're pulling the pin UP with INPUT_PULLUP and at the same time, DOWN with a 10k resistor? You have a 30:10 voltage divider, so the idle voltage on the pin will be about 1.25V. What is the other leg of the switch connected to?
const byte SwitchPin = 3; // switch to GND
void setup()
{
pinMode(SwitchPin, INPUT_PULLUP);
}
void loop()
{
// Get the current time
DateTime now = rtc.now();
byte h = now.hour();
byte m = now.minute();
const bool SwitchState = digitalRead(SwitchPin);
// if the switch is HIGH, increment the hour - if LOW, do nothing
if(SwitchState == HIGH)
//same as
//if(SwitchState)
{
h = h + 1;
//same as
//h++;
//or
//h += 1;
}
}
Thanks Jca34f. I've never done this before but you've given me something to go on, some research to do. At the moment the switch is between 5V and ground.
And thanks Septillion. I've just read the code you cleaned up for me a couple of times, I'll work on understanding it. I've never done this before, it's all new to me. An answer like this gives me so much to go on, I can learn a lot from it. To get this far has taken days of reading these forums but one targeted post like this makes the world of difference.
NZHotAir:
I have the switch on the next pin D3 (the 6th one). Do I reference that as 3?
Yes, the D-numbers are the "normal" Arduino numbers.
NZHotAir:
And while at it, why isn't it defined like Pin 2 is?
Better ask it the other way around, why isn't that defined as a const byte. Using macro's (#define) is the old C way of defining a constant. It acts just like a search-and-replace before compiling. But nowadays in C++ we have the const keyword to tell the compiler it's a constant. This gives more control (things like type checking etc) and tends to give way better error messages when there is something wrong.
NZHotAir:
Switch is between ground and D3. About 2v when switch not pressed, drops to zero when pressed (which seems wrong to me...)
Do you:
only have the switch connected to the pin (no resistors anymore)?
Thanks for clarifying about the pin numbers. It's assumed knowledge, hard to verify.
I started with a very old base for my code so that's where the #define came from.
Yes I removed the resistor as suggested.
Yes, pinMode(switchPin, INPUT_PULLUP);
Then
void loop()
{
DateTime now = rtc.now();
byte h = now.hour();
byte m = now.minute();
const bool switchState = digitalRead(switchPin);
// if the switch is HIGH, increment the hour - if LOW, do nothing
if(switchState == HIGH){
h++;
}
else
// If the hours are in the 24h format and the hours are >= 12, then convert to 12h format
h_12 = h>=12 ? h-12 : h;
Thanks again for your help. It's the little things like confirming the pin numbers that really helps.
LOL, thanks for asking that question! Now that I've had a play for the last hour, I think I've figured out that's my problem!
I'm making a word clock that only has numbers from 1 to 12, but the RTC pumps out 1 to 24.
That line is supposed to let me show only 1 to 12. And it seems to work, but is actually why my time is not changing.
Under h++; I added h_12++ and my switch worked.
But then I realized that it was then trying to output 13 so display failed. So for testing I made it h--; and h_12--; and it worked.
BUT... at the end of my code was a 20 second delay. And now the hour changes down by one every 20 seconds as it loops through. I need to move the hour read from the RTC out of the loop.
So... the lesson here, is if I had posted the complete code, someone could have spotted my screw ups, but I didn't expect that of anyone. I hope no one feels like I have wasted their time though.
I think I'll swallow my pride and go to the local university and see if a computer science or electrical engineering student would like some beer money to tweak it for me!
You might want to examine this line of code. Please test what happens when edge case h = 12. Your results might not be what you expect.
h_12 = h>=12 ? h-12 : h;
Test Case h = 12
void setup() {
// put your setup code here, to run once:
int h_12 = 12;
int h = 12;
h_12 = h>=12 ? h-12 : h;
Serial.begin(115200);
Serial.println(h_12);
}
void loop() {
// put your main code here, to run repeatedly:
}
NZHotAir:
So... the lesson here, is if I had posted the complete code, someone could have spotted my screw ups, but I didn't expect that of anyone. I hope no one feels like I have wasted their time though.
I think I'll swallow my pride and go to the local university and see if a computer science or electrical engineering student would like some beer money to tweak it for me!
Why give up? Post your code, we can and will help, part of the fun is what you learn along the way.