Relógio Binário
Autor: Fellipe Couto | Publicado: 2013-08-25T15:30:00-03:00




Vamos exercitar a cachola e visualizar as horas no relógio binário!

Como já está mostrando no vídeo, as horas são exibidas pela primeira fileira de leds, no meio exibe os minutos, e a última os segundos.

Temos dois push-buttons no circuito para o ajuste de horas e minutos.

Segue o esquema elétrico e o código fonte.


&nbsp;


&nbsp;/*--------------
&nbsp;RELOGIO BINARIO
&nbsp;Fellipe Couto
&nbsp;11/05/2013
&nbsp;--------------*/

/*----- VARIAVEIS -----*/
long previousMillis = 0;
long interval = 1000;
int hour = 0;
int minute = 0;
int second = 0;
int ledHour[5] = {1,2,3,4,5}; //Leds das horas
int ledMinute[6] = {6,7,8,9,10,11}; //Leds dos minutos
int ledSecond[6] = {12,13,14,15,16,17}; //leds dos segundos

/*----- SETUP -----*/
void setup(){
&nbsp; for (int i=0; i&lt;5; i++){
&nbsp;&nbsp;&nbsp; pinMode(ledHour[i], OUTPUT);
&nbsp; }
&nbsp; for (int i=0; i&lt;6; i++){
&nbsp;&nbsp;&nbsp; pinMode(ledMinute[i], OUTPUT);
&nbsp;&nbsp;&nbsp; pinMode(ledSecond[i], OUTPUT);
&nbsp; }
}

/*----- LOOP PRINCIPAL -----*/
void loop(){
&nbsp; unsigned long currentMillis = millis();

&nbsp; //Intervalo de 1 segundo
&nbsp; if (currentMillis - previousMillis &gt; interval){
&nbsp;&nbsp;&nbsp; previousMillis = currentMillis;

&nbsp;&nbsp;&nbsp; clockDec(); //Relógio decimal
&nbsp;&nbsp;&nbsp; clockBin(); //Relogio binario

&nbsp;&nbsp;&nbsp; if (analogRead(4) == 1023){ //Ajuste de horas
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hour++;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; second = 0;
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; if (analogRead(5) == 1023){ //Ajuste de minutos
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minute++;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; second = 0;
&nbsp;&nbsp;&nbsp; }
&nbsp; }
}

/*----- RELOGIO DECIMAL -----*/
void clockDec(){
&nbsp; second++;&nbsp;&nbsp; 
&nbsp; if (second == 60){
&nbsp;&nbsp;&nbsp; second = 0;
&nbsp;&nbsp;&nbsp; minute++;
&nbsp; }
&nbsp; if (minute == 60){
&nbsp;&nbsp;&nbsp; minute = 0;
&nbsp;&nbsp;&nbsp; hour++;
&nbsp; }
&nbsp; if (hour == 24){
&nbsp;&nbsp;&nbsp; hour = 0;
&nbsp; }
}

/*----- RELOGIO BINARIO -----*/
void clockBin(){
&nbsp; //Segundos
&nbsp; for (int j=0; j&lt;6; j++){
&nbsp;&nbsp;&nbsp; if (decToBin(second, 6).substring(j,j+1) == "0"){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(ledSecond[j], LOW);
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; else{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(ledSecond[j], HIGH);
&nbsp;&nbsp;&nbsp; }
&nbsp; }
&nbsp; //Minutos
&nbsp; for (int j=0; j&lt;6; j++){
&nbsp;&nbsp;&nbsp; if (decToBin(minute, 6).substring(j,j+1) == "0"){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(ledMinute[j], LOW);
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; else{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(ledMinute[j], HIGH);
&nbsp;&nbsp;&nbsp; }
&nbsp; }
&nbsp; //Horas
&nbsp; for (int j=0; j&lt;5; j++){
&nbsp;&nbsp;&nbsp; if (decToBin(hour, 5).substring(j,j+1) == "0"){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(ledHour[j], LOW);
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; else{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(ledHour[j], HIGH);
&nbsp;&nbsp;&nbsp; }
&nbsp; }
}

/*----- CONVERTE DECIMAL PARA BINARIO -----*/
String decToBin(int num, int qtd){
&nbsp; //Retorna uma string de 'qtd' posições.
&nbsp; int zeros = qtd - String(num, BIN).length();
&nbsp; String str;
&nbsp; for (int i=0; i&lt;zeros; i++){
&nbsp;&nbsp;&nbsp; str = str + "0";
&nbsp; }
&nbsp; return str = str + String(num, BIN);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}