need help to modify a clock code

Hello

I sucess (with difficulty) to run this beautiful clock ... I would like to make some light changes on the code:

1- add two pushbuttons to manually change hours and minutes

2- use a variable resistance (photoresist type) in order to decrease the luminosity automatically in the absence of light

If you can help me ... I start under arduino

Thank you

I don’t know that you can change the brightness with that shift register circuit. Perhaps consider changing to a dedicated LED driver such as a MAX6971?

Start with playing with some buttons. Load up the “StateChangeDetection” example in “02.Digital”.

MorganS: I don't know that you can change the brightness with that shift register circuit. Perhaps consider changing to a dedicated LED driver such as a MAX6971?

Start with playing with some buttons. Load up the "StateChangeDetection" example in "02.Digital".

:(

OK

go to analyse the 02.digital example

I also want to modify the display time delay ... I would like it to be much longer than the delay to display of temperature and humidity (I made it ! .. work fine) ... and the wait time between the displays

I change : delay(500); to delay(100); between displays .. but only the last delay take effect ... strange no ?

OK
I Try to add push button to change hours … i make some add in the original code … no problem in compilation … but not work correctly …

#include <TimeLib.h>        //Time Library
#include <DS1307RTC.h>   //Real Time Clock Library
#include <Wire.h>        //Auxiliary Library for DS1307RTC (Real-Time Clock) - Pins to Arduino UNO: A4 (SDA), A5 (SCL)

#include <DHT.h>          //Temperature and Humidity Library
#define DHTPIN 11         //Sensor DHT11 conected to the pin 11 on Arduino

// Definition of what DHT sensor type you are using

#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

int clockPin = 8; // Pin 8 of Arduino connected in the pin 11 of 74HC595 (Clock)
int latchPin = 9; // Pin 9 of Arduino connected in the pin 12 of 74HC595 (Latch)
int dataPin = 10; // Pin 10 of Arduino connected in the pin 14 of 74HC595 (Data)
//---------------------------------------------------
const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status
//temporary variables for time changing
int varMin = 0;
int varHr = 0;
//---------------------------------------------------
int hora, minuto, temp, umid;
int unidadeHora, unidadeMinuto, dezenaHora, dezenaMinuto;
int unidadeTemp, dezenaTemp, unidadeUmid, dezenaUmid;
unsigned long ti;

//Digits Matrix - 0 a 9
byte num[] = {
  B01111110, // Zero
  B00110000, // One
  B01101101, // Two
  B01111001, // Three
  B00110011, // Four
  B01011011, // Five
  B01011111, // Six
  B01110000, // Seven
  B01111111, // Eight
  B01111011, // Nine
};


void setup() {

  pinMode(latchPin, OUTPUT); // Define the 3 digital pins as output
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  dht.begin();

  setSyncProvider(RTC.get);   // Update the time with data of RTC (Real Time Clock)

  //setTime(15, 05, 00, 13, 02, 2016);

}


void loop() {
//------------------
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
      time_t t = now();
      varHr = hour(t);
      varMin = minute(t);
      varMin++;   //add 1 minute from the current tine
      setTime(varHr, varMin, 0, 0, 0, 0);
    }
//---------------
  ti = millis(); // Initial time for the Timer of Hour/Time

  while ((millis() - ti) < 8000) { //Timer of 8 seconds to show the Hour

    hora = hour();
    minuto = minute();
    unidadeHora = hora % 10;
    dezenaHora = hora / 10;
    unidadeMinuto = minuto % 10;
    dezenaMinuto = minuto / 10;

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 8);                  //Set DISPLAY 1 (top view from left to right)
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaHora]);   //Set the Hour (ten)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 4);                  //Set DISPLAY 2
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[unidadeHora]);  //Set the Hour (unit)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 2);                  //Set DISPLAY 3
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaMinuto]); //Set the Minute (ten)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                   //Set DISPLAY 4
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[unidadeMinuto]); //Set the Minute (unit)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 4);                  //Set LED of dots
    shiftOut(dataPin, clockPin, LSBFIRST, ~B10000000);         //Set LEDs of colon (two points)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                  //Set DISPLAY 4
    shiftOut(dataPin, clockPin, LSBFIRST, 255);                //Reset the DISPLAY 4 (to avoid some flicking)
    digitalWrite(latchPin, HIGH);
  }
  delay(500);  //Wait for half second before go ahead to show the next feature

  ti = millis(); //Initial time for the Timer of Temperature

  temp = dht.readTemperature();   //Reading the Temperature in Celsius degree (ºC)

  //Optional Temperature in Fahrenheit (ºF). Remove the comments ("//") of following statement before use it.
  //temp=dht.readTemperature(true);

  while ((millis() - ti) < 3000) { //Timer of 3 seconds for the Temperature

    unidadeTemp = temp % 10;
    dezenaTemp = temp / 10;

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 8);                  //Set DISPLAY 1 (top view from left to right)
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaTemp]);   //Set the Temperature (ten)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 4);                  //Set DISPLAY 2
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[unidadeTemp]);  //Set the Temperature (unit)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 2);                  //Set DISPLAY 3
    shiftOut(dataPin, clockPin, LSBFIRST, ~B01100011);         //Set the degree symbol [º]
    digitalWrite(latchPin, HIGH);

    //Show the symbol of Celsius degrees (ºC)
    //Set the following statements as comments with "//" to show the Temperature in Fahrenheit (ºF)
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                  //Set DISPLAY 4
    shiftOut(dataPin, clockPin, LSBFIRST, ~B01001110);         //Set the symbol of Celsius [C]
    digitalWrite(latchPin, HIGH);

    //Show the symbol of Fahrenheit degrees (ºF)
    //Remove the indication of comments "//" on following statements to show the Temperature in Fahrenheit (ºF)
    //digitalWrite(latchPin, LOW);
    //shiftOut(dataPin, clockPin, LSBFIRST, 1);                 //Set DISPLAY 4
    //shiftOut(dataPin, clockPin, LSBFIRST, ~B01000111);        //Set the symbol of Fahrenheit [F]
    //digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                   //Set DISPLAY 4
    shiftOut(dataPin, clockPin, LSBFIRST, 255);                 //Reset the DISPLAY 4 (to avoid some flicking)
    digitalWrite(latchPin, HIGH);
  }
  delay(500);  //Wait for half second before go ahead to show the next feature

  ti = millis(); //Initial time for the Timer of Humidity

  umid = dht.readHumidity();    //Reading the Humidity

  while ((millis() - ti) < 3000) { //Timer of 3 seconds for the Humidity

    unidadeUmid = umid % 10;
    dezenaUmid = umid / 10;

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 8);                  //Set DISPLAY 1
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaUmid]);   //Set the Humidity (ten)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 4);                  //Set DISPLAY 2
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[unidadeUmid]);  //Set the Humidity (unit)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 2);                  //Set DISPLAY 3
    shiftOut(dataPin, clockPin, LSBFIRST, ~B01100011);         //Set the upper symbol of percentage [%] of Humidity
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                  //Set DISPLAY 4
    shiftOut(dataPin, clockPin, LSBFIRST, ~B00011101);         //Set the lower symbol of percentage [%] of Humidity
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                  //Set Display 4
    shiftOut(dataPin, clockPin, LSBFIRST, 255);                //Reset the DISPLAY 4 (to avoid some flicking)
    digitalWrite(latchPin, HIGH);
  }
  delay(500);  //Wait for half second before to restart

}

You want us to guess what part of it doesn't work?

Set the time on the RTC, then the Time library will update from that.

You also increment the hour when the button is held down. This will happen about twice per second, faster if you remove the delay(). You probably want it to just increment once when the button is pushed. Look at the StateChangeDetection example again.

I'm lost ... Simple request: suppose that I am in loop (), and I would like to leave this loop (with a pushbutton) in order to make changes ... how can I do this ...

OK OK ...

I try use while loop :

  while (buttonState1 == HIGH) {

    hora = hour();
    minuto = minute();
    unidadeHora = hora % 10;
    dezenaHora = hora / 10;
    unidadeMinuto = minuto % 10;
    dezenaMinuto = minuto / 10;

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 8);                  //Set DISPLAY 1 (top view from left to right)
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaHora]);   //Set the Hour (ten)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 4);                  //Set DISPLAY 2
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[unidadeHora]);  //Set the Hour (unit)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 2);                  //Set DISPLAY 3
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaMinuto]); //Set the Minute (ten)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                   //Set DISPLAY 4
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[unidadeMinuto]); //Set the Minute (unit)
    digitalWrite(latchPin, HIGH);

 // read the state of the pushbutton1 value:
  buttonState2 = digitalRead(buttonPin2);

 // read the state of the pushbutton1 value:
  buttonState3 = digitalRead(buttonPin3);


      time_t t = now();
      varHr = hour(t);
      varMin = minute(t);
      varMin++;   //add 1 minute from the current tine
      setTime(varHr, varMin, 0, 0, 0, 0);

   buttonState1 = digitalRead(buttonPin1);
  }

the probleme : when I enter in the while loop I wan't exit :/

Edit : I can exit but after a long time ... I try add delay ... but it's not work correctly :(

could you not toggle the LATCH pin via PWM to control perceived brightness? The higher the duty cycle...the brighter the display will seem.

esloch:
I also want to modify the display time delay … I would like it to be much longer than the delay to display of temperature and humidity (I made it ! … work fine) … and the wait time between the displays

I change : delay(500); to delay(100); between displays … but only the last delay take effect … strange no ?

The display time is controlled by the comparison in the while() loop not by any delays.
The comment is a clue:

 while ((millis() - ti) < 3000) { //Timer of 3 seconds to show the Hour

That 3000 is the the milliseconds for that loop which is posting the time.

Also, because the display is multiplexed in that loop, you should be able to dim the display by inserting a small delay at the bottom of the loop. maybe a delay of just a few ms or perhaps even shorter using delayMicroSeconds()
I’m assuming the segments are fall off based on the comment of resetting display 4 at the bottom of the loop.
If not, all the segments will be dim other then the one that is not turned off and you will have to do a small tweak to fix that.

— bill

Thanks bperrybap ... but I ask for a hide time between display time that i can"t control .. :/

I have nearly succeeded the time change code using pushbuttons with this code placed in the top of void loop () :

  buttonStateS = digitalRead(buttonPinS);
  
 while (buttonStateS == HIGH) {

    hora = hour();
    minuto = minute();
    unidadeHora = hora % 10;
    dezenaHora = hora / 10;
    unidadeMinuto = minuto % 10;
    dezenaMinuto = minuto / 10;

  

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 8);                  //Set DISPLAY 1 (top view from left to right)
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaHora]);   //Set the Hour (ten)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 4);                  //Set DISPLAY 2
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[unidadeHora]);  //Set the Hour (unit)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 2);                  //Set DISPLAY 3
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaMinuto]); //Set the Minute (ten)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                   //Set DISPLAY 4
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[unidadeMinuto]); //Set the Minute (unit)
    digitalWrite(latchPin, HIGH);


  // check if the pushbuttonH  is pressed.
    buttonStateH = digitalRead(buttonPinH);

      if (buttonStateH == HIGH) {
      t = now();
      varHr = hour(t);
      varMin = minute(t);
      varHr++;   //add 1 hour from the current time
      setTime(varHr, varMin, 0, 0, 0, 0);

      delay (100);
 }
     buttonStateS = digitalRead(buttonPinS);
   
  // check if the pushbuttonM  is pressed.
    buttonStateM = digitalRead(buttonPinM);

      if (buttonStateM == HIGH) {
      t = now();
      varHr = hour(t);
      varMin = minute(t);
      varMin++;   //add 1 minute from the current time
      setTime(varHr, varMin, 0, 0, 0, 0);

      delay (100);
 }

     buttonStateS = digitalRead(buttonPinS);
 }

I just have a little problem: I have to wait the restart of the void loop () (which takes a bit of time with the delay and millis commands) so that the while command takes effect

No way to make the while loop active at any time of the void loop () ?

Thanks

esloch:
No way to make the while loop active at any time of the void loop () ?

Thanks

The generic solution is to change the while() to an if(). Then loop() provides the iteration.

esloch: Thanks bperrybap ... but I ask for a hide time between display time that i can"t control .. :/

In post #1 you asked:

2- use a variable resistance (photoresist type) in order to decrease the luminosity automatically in the absence of light

which is dimming the display. That is the second part of what I described.

--- bill

Finally all works as it should

Thank you all for your assistance