Just started to get into Arduino to give my projects more life.
I'm working on making a portable digital pocket watch, using an Arduino, RTC and a TM1637
using this code from the project hub, I got it all working.
// Reloj digital mostrado a través de un display de 4 dígitos (digitaltube), y con el tiempo
// proporcionado por un Tiny RTC de Catalex.
// Software programado por PentiumCadiz 7-1-17, basado en el ejemplo de la librería DS1307RTC
// y en el ejemplo de uso del display de Prometec http://www.prometec.net/display-con-interface/
#include <TM1637.h>
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#define CLK 9
#define DIO 8
TM1637 Display1(CLK, DIO);
int8_t Digitos[] = {0,1,2,3};
int horas;
int minutos;
boolean alterna;
void setup() {
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
Display1.set();
Display1.init();
}
void loop() {
tmElements_t tm;
if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
horas = tm.Hour;
minutos = tm.Minute;
CalculaDigitos(horas, minutos);
if (alterna)
{
Display1.point(POINT_OFF);
alterna = false;
}
else
{
Display1.point(POINT_ON);
alterna = true;
}
} 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();
}
delay(9000);
}
delay(1000);
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
void CalculaDigitos( int hor, int minu)
{
int8_t Digit0 = minu %10 ;
int8_t Digit1 = (minu % 100) / 10 ;
int8_t Digit2 = hor % 10 ;
int8_t Digit3 = (hor % 100) / 10 ;
Digitos[3] = Digit0 ;
Digitos[2] = Digit1 ;
Digitos[1] = Digit2 ;
Digitos[0] = Digit3 ;
Display1.display(Digitos);
}
What I would like to do if possible, is to control the brightness of the TM1637 using two switches to increase and decrease the brightness. I believe this ranges from 1 to 7 with 7 being max.
I found some code in another project for a 7 segment display and tried to copy it into the working code.
this does not work. I am new to programming and read a book, but that was not much more then copy typing into the IDE.
My code below and then the faults.
#include <TM1637.h>
#include <Bounce2.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#define CLK 9
#define DIO 8
TM1637 display(CLK, DIO);
int8_t Digitos[] = {0, 1, 2, 3};
int horas;
int minutos;
boolean alterna;
const int Switch = 4;
const int Switch2 = 5;
Bounce brightnessUp ( 4, 25 );
Bounce brightnessDown( 5, 25 );
int BRIGHTEST = 7; // starting brightness
void setup()
{
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
display.set();
display.init();
// Use the Bounce2 library for the brightness switch|
brightnessUp.attach( Switch, INPUT_PULLUP );
brightnessDown.attach( Switch2, INPUT_PULLUP );
brightnessUp.interval( 25 ); // how quickly to sample
brightnessDown.interval( 25 ); // how quickly to sample
}
void loop()
{
// Process button presses
if (brightnessUp.update()) {
if (BRIGHTEST < 7) {
BRIGHTEST == 1;
}
display.BRIGHTEST( BRIGHTEST );
}
if (brightnessDown.update()) {
if (BRIGHTEST > 0) {
BRIGHTEST == 1;
}
display.BRIGHTEST( BRIGHTEST );
}
tmElements_t tm;
}
{ if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
horas = tm.Hour;
minutos = tm.Minute;
CalculaDigitos(horas, minutos);
if (alterna)
{
display.point(POINT_OFF);
alterna = false;
}
else
{
display.point(POINT_ON);
alterna = true;
}
} 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();
}
delay(9000);
}
delay(1000);
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
void CalculaDigitos( int hor, int minu)
{
int8_t Digit0 = minu % 10 ;
int8_t Digit1 = (minu % 100) / 10 ;
int8_t Digit2 = hor % 10 ;
int8_t Digit3 = (hor % 100) / 10 ;
Digitos[3] = Digit0 ;
Digitos[2] = Digit1 ;
Digitos[1] = Digit2 ;
Digitos[0] = Digit3 ;
display.display(Digitos);
Arduino: 1.8.7 (Windows 10), Board: "Arduino/Genuino Uno"
In file included from F:\Arduino_Bits\Colck1mod\Colck1mod.ino:2:0:
F:\Arduino_Bits\libraries\Grove_4-Digit_Display/TM1637.h:47:25: error: expected unqualified-id before numeric constant
#define BRIGHTEST 7
^
F:\Arduino_Bits\Colck1mod\Colck1mod.ino:21:5: note: in expansion of macro 'BRIGHTEST'
int BRIGHTEST = 7; // starting brightness
^
F:\Arduino_Bits\Colck1mod\Colck1mod.ino: In function 'void loop()':
F:\Arduino_Bits\libraries\Grove_4-Digit_Display/TM1637.h:47:25: error: expected unqualified-id before numeric constant
#define BRIGHTEST 7
^
F:\Arduino_Bits\Colck1mod\Colck1mod.ino:50:13: note: in expansion of macro 'BRIGHTEST'
display.BRIGHTEST( BRIGHTEST );
^
F:\Arduino_Bits\libraries\Grove_4-Digit_Display/TM1637.h:47:25: error: expected unqualified-id before numeric constant
#define BRIGHTEST 7
^
F:\Arduino_Bits\Colck1mod\Colck1mod.ino:56:13: note: in expansion of macro 'BRIGHTEST'
display.BRIGHTEST( BRIGHTEST );
^
F:\Arduino_Bits\Colck1mod\Colck1mod.ino: At global scope:
Colck1mod:60:1: error: expected unqualified-id before '{' token
{ if (RTC.read(tm)) {
^
exit status 1
expected unqualified-id before '{' token
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.