What should I do? MAX7219_Clock & Set Time_ESP32-S3

Hi everyone, I have a project to do for my teacher. is to make clock from ESP32-S3 board and now i can make clock online by wifi connection. But my problem is that I don't know how to write a program to be able to set the alarm using 3-4 switches to press the set button. Please help me.

// My code //

// Header file includes
#include <WiFi.h>
#include <time.h>
#include <MD_Parola.h>
#include <SPI.h>

#include "Font_Data.h"

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

#define CLK_PIN   12 // or SCK
#define DATA_PIN  11 // or MOSI
#define CS_PIN    10 // or SS

// Arbitrary output pins
MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);

#define SPEED_TIME  75
#define PAUSE_TIME  0
#define MAX_MESG  20

/**********  User Config Setting   ******************************/
char* ssid = "gajhgahg";
char* password = "hehehh";
//calculate your timezone in seconds,1 hour = 3600 seconds and 5.30Hrs = 19800
const int timezoneinSeconds = 25200;
/***************************************************************/
int dst = 0;
uint16_t  h, m, s;
uint8_t dow;
int  day;
uint8_t month;
String  year;
// Global variables
char szTime[9];    // mm:ss\0
char szsecond[4];    // ss
char szMesg[MAX_MESG+1] = "";


void getsec(char *psz)
{
  sprintf(psz, "%02d", s);
}

void getTime(char *psz, bool f = true)
{
  time_t now = time(nullptr);
  struct tm* p_tm = localtime(&now);
      h = p_tm->tm_hour;
      m = p_tm->tm_min;
      s = p_tm->tm_sec;
  sprintf(psz, "%02d%c%02d", h, (f ? ':' : ' '), m);
  Serial.println(psz);
}

void setup(void)
{
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  delay(3000);
  WiFi.mode(WIFI_STA);
  getTimentp();

  P.begin(3);
  P.setInvert(false);

  P.setZone(0, 0, 0);
  P.setZone(1, 1, 3);
  P.setFont(0, numeric7Seg);
  P.setFont(1, numeric7Se);
  P.displayZoneText(0, szsecond, PA_LEFT, SPEED_TIME, 0, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(1, szTime, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);

  getTime(szTime);
}

void loop(void)
{
  static uint32_t lastTime = 0; // millis() memory
  static uint8_t  display = 0;  // current display mode
  static bool flasher = false;  // seconds passing flasher

  P.displayAnimate();

  if (millis() - lastTime >= 1000)
  {
    lastTime = millis();
    getsec(szsecond);
    getTime(szTime, flasher);
    flasher = !flasher;

    P.displayReset(0);
    P.displayReset(1);
  }
}

void getTimentp()
{
  configTime(timezoneinSeconds, dst, "pool.ntp.org","time.nist.gov");
  while(!time(nullptr)){
        delay(500);
        Serial.print(".");
  }
  Serial.print("Time Update");
}

Did You ask the teacher for tips? That's what he ought to get payed for.

He just explained a little and I still don't understand because I'm a beginner.

I've been trying to do it for the 2nd week.

what did he explained regarding how to read a button?

He just explained the working principle to me.

make simple sketch with one working button to print "Press" to the Serial Monitor.
When you are ready, post your code in code tags.

Do you want me to do this?


// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;  // the number of the pushbutton pin


// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
 
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(115200);

}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
 
 
    if (buttonState == LOW) {
     Serial.println("PRESS");
     delay(200);
    }
  }
  


Of course you MUST do this. It's your homework - not mine.

And now expand that sketch to 4 buttons

each button should print an individual word to the serial monitor

Press (-->the existing one)
Up
Down
Set

Is that right? @noiasca


#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

#define CLK_PIN 12
#define DATA_PIN 11
#define CS_PIN 10

MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;  // the number of the pushbutton pin
const int buttonPin2 = 42;
const int buttonPin3 = 41;
const int buttonPin4 = 40;
const uint16_t WAIT_TIME = 1000;

// variables will change:
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;  // variable for reading the pushbutton status

void setup() {
  P.begin();
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  Serial.begin(115200);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);



  if (buttonState == LOW) {
    P.print("Press");
    Serial.println("Press");
    delay(200);
  }
  else if (buttonState2 == LOW) {
    P.print("Up");
    Serial.println("Up");
    delay(200);
  }
  else if (buttonState3 == LOW) {
    P.print("Down");
    Serial.println("Down");
    delay(200);
  }
  else if (buttonState4 == LOW) {
    P.print("Set");
    Serial.println("Set");
    delay(200);
  }
}


Only you have the hardware. I don't see on your desk.

So:
does it compile?
does it work with real hardware?
does it print to serial?

Let's do it, brother. @noiasca

so now take a piece of paper and draw the program flow of the necessary steps how to enter an alarm time.
The diagram should show

  • how to enter the mode SETTING
  • how to enter the HOUR (how to increase / decrease the number, what should happen if the current Number is 0 and you press DOWN, what should happen if the current Number is 23 and you press UP,...)
  • how to jump to enter the MINUTE
  • how to enter the MINUTE
  • how to confirm the settings

please make a clear picture and upload it to the forum.

You mean let me draw on paper?

Every good program starts with a drawing.
Paper is just a fast way to do such kind of drawing.
This will help you to focus your ideas before you start writing code.

You mean let me write an algorithm?

I will reply to you tomorrow @noiasca

Is it like this? @noiasca
Presentation1.pdf (175.4 KB)

If I were you, I would make my life easier and omnit that "press for 3 seconds" actions currently.
Just use single button presses.

Furthermore I think your program has no "end" ... you just have to leave the alarm setting. and return to display the current time.
During the display of the current time you have to check if the Alarm time is met.

I don't know if this state diagram makes sense for you, but imho this represents better what your program should do:

so I would write the program as "finite state machine" (<-- google for it!), using 4 states IDLE, HOUR, MINUTE and ALARM.
The button presses either jump through the states or increase/decrease variables.

Think about it.
Google for finite state machine, even Wikipedia has a nice summary on that topic.
Read this post again.
Share your mind or ask.

thanks for the suggestion
What should I do next?
In my head now empty