Go Down

Topic: Adding a button to a clock (Read 441 times) previous topic - next topic

_Kuga

Hi everyone,

First of all, thanks for view my help request and second, i will try to explain all my doubts correctly.

I'm building a clock and it works correctly but i want to add two new things that are:

1 button for select the hours or the minutes
1 button for increment so i can adjust the time correctly.

I know that in my code i can adjust it but i want a way to do it manually.

Here's my code and sorry for it being in portuguese.


#include <RTClib.h> // i think i don't need this

#include <SevSeg.h>

#include <Wire.h>


#define DS1307_ADDRESS 0x68


SevSeg display7seg;

int valor = 0;

byte zero = 0x00;
unsigned long timer;
void setup()
{
 Serial.begin(9600);
 Wire.begin();
//this line can be removed after setting the hours and minutes.
//SelecionaDataeHora(); //tradution = selectDataandhour

//type of the display.
int displayType = COMMON_CATHODE;

//definition of pines
 int digit1 = 10; //Pino Digito1 do display
 int digit2 = 11; //Pino Digito2 do display
 int digit3 = 12; //Pino Digito3 do display
 int digit4 = 13; //Pino Digito4 do display

//Pinos ligados aos segmentos A - H
 int segA = 2; //Pino segmento A
 int segB = 3; //Pino segmento B
 int segC = 4; //Pino segmento C
 int segD = 5; //Pino segmento D
 int segE = 6; //Pino segmento E
 int segF = 7; //Pino segmento F
 int segG = 8; //Pino segmento G
 int segDP= 9; //Pino segmento H
 
//Define o numero de digitos do display
 int numberOfDigits = 4;

//Inicializa o display
 display7seg.Begin(displayType, numberOfDigits, digit1, digit2, digit3, digit4, segA, segB, segC, segD, segE, segF, segG, segDP);
 
//Nivel de brilho do display
display7seg.SetBrightness(100);
timer = millis();
}

void loop()
{

 char tempString[10]; //Used for sprintf
 Wire.beginTransmission(DS1307_ADDRESS);
 Wire.write(zero);
 Wire.endTransmission();
 Wire.requestFrom(DS1307_ADDRESS, 7);
 int segundos = ConverteparaDecimal(Wire.read());
 int minutos = ConverteparaDecimal(Wire.read());
 int horas = ConverteparaDecimal(Wire.read() & 0b111111);
 sprintf(tempString, "%02d%02d", horas, minutos);  
 display7seg.DisplayString(tempString, 3);
 Serial.print(segundos);
 Serial.print("\n");  
}

void SelecionaDataeHora() //Seta a data e a hora do DS1307
{
 byte segundos = 10; //Valores de 0 a 59
 byte minutos = 37; //Valores de 0 a 59
 byte horas = 11; //Valores de 0 a 23
 Wire.beginTransmission(DS1307_ADDRESS);
 Wire.write(zero); //Stop no CI para que o mesmo possa receber os dados

//As linhas abaixo escrevem no CI os valores de
//data e hora que foram colocados nas variaveis acima
 Wire.write(ConverteParaBCD(segundos));
 Wire.write(ConverteParaBCD(minutos));
 Wire.write(ConverteParaBCD(horas));
 Wire.write(zero);
 Wire.endTransmission();
}

byte ConverteParaBCD(byte val)
{
//Converte o nĂºmero de decimal para BCD
 return ( (val/10*16) + (val%10) );
}

byte ConverteparaDecimal(byte val)
{
 //Converte de BCD para decimal
 return ( (val/16*10) + (val%16) );
}

that's it.
hope you can help me. thanks !

PaulS

Quote
1 button for select the hours or the minutes
Switches are so much easier.

Nothing in your code even hints at where the switches will be connected. Nothing even hints at reading the switch states. Nothing even hints at adding a value to minutes or seconds when a switch is (or better yet, becomes) pressed. Nothing even hints at setting the new time.

The code isn't even posted correctly.

What part is it you need help with?
The art of getting good answers lies in asking good questions.

_Kuga

Thanks for the reply.

I know that i don't have nothing for adding the buttons. That's why i asked for help, to see if anyone knows a solution for this. If anyone knows a code that do the same i can change...

But is there anything i can do to add the buttons ?

PaulS

Quote
But is there anything i can do to add the buttons ?
Yes, there is. At a minimum, connect the switches. Read the switch states. Determine whether you want to do something when the switch IS pressed or when the switch BECOMES pressed.

Determine what should happen when one switch is/becomes pressed.
Determine what should happen when the other switch is/becomes pressed.

Don't expect to find code that someone else has written that you can just drag and drop into your code.
The art of getting good answers lies in asking good questions.

_Kuga

I think i get it  :)

I will try to do that !

thanks

Go Up