// setting a string variable containing the corrected value:
if (s_mode == 1) {
c_par = «Y:» + String (Set_year);
}
else if (s_mode == 2) {
c_par = «M:» + String (Set_month);
}
else if (s_mode == 3) {
c_par = «D:» + String (Set_day);
}
else if (s_mode == 4) {
c_par = «H:»)+ String (Set_Hours);
}
else if (s_mode == 5) {
c_par = «M:» + String (Set_minutes);
}
else if (s_mode == 6) {
c_par = «Kp» + String (int(Corr_time));
}
change_param(c_par); // we display the corrected value
/////////////////////////
//we determine that there was a rotation of the encoder
if ( rDir != 0 ) {
// CW
if (s_mode == 1) { //with each rotation of the encoder, we will increase or decrease (depending on the direction of rotation of the encoder) the initial value of the year. If you increase the value of the year to «51», reset the value of the year to «20». When reducing the year value to «19», reset the year value to «50».
if ( rDir == 1 ) {
if (Set_year > 50)
{
Set_year = 20;
}
else
{
Set_year ++;
}
}
if ( rDir == 2 ) {
if (Set_year < 20)
{
Set_year = 50;
}
else
{
Set_year —;
}
}
delay(100);
}
else if (s_mode == 2) { //with each rotation of the encoder, we will increase or decrease (depending on the direction of rotation of the encoder) the initial value of the month. When increasing the month value to «12», reset the month value to «1». When reducing the month value to «1», reset the month value to «12».
if ( rDir == 1 ) {
if (Set_month == 12)
{
Set_month = 1;
}
else
{
Set_month ++;
}
}
if ( rDir == 2 ) {
if (Set_month == 1)
{
Set_month = 12;
}
else
{
Set_month —;
}
}
delay(100);
}
else
if (s_mode == 3) {//It should be taken into account that each month has a different number of days. Including in February, there may be 28 and 29 days. Therefore, we will determine the sign of a leap year and take into account the previously set month:
if (Set_month == 2) {
LeapYear = 0; //first you need to determine whether the current year is a leap year or not
if ( (((2000 + int(Set_year)) % 4) == 0) & (((2000 + int(Set_year)) % 100) != 0) || (((2000 + int(Set_year)) % 400) == 0) ) {
LeapYear = 1; // a sign of a leap year
}
else {
LeapYear = 0;// a sign of a non-leap year
}
}
if ( rDir == 1 ) { //with each rotation of the encoder, we will increase or decrease (depending on the direction of rotation of the encoder) the initial value of the day. If we increase the value of the day to the last one in this month, we will reset the value of the day to «1». When reducing the value of the month to «1», we reset the value of the day to the last value in this month.
if ((Set_day == 31) & (Set_month == 1 || Set_month == 3 || Set_month == 5 || Set_month == 7 || Set_month == 8 || Set_month == 10 || Set_month == 12)) {
Set_day = 1;
}
else if ((Set_day == 30) & (Set_month == 4 || Set_month == 6 || Set_month == 9 || Set_month == 11)) {
Set_day = 1;
}
else if ((Set_day == 29) & (Set_month == 2)) {
Set_day = 1;
}
else if ((Set_day == 28) & (Set_month == 2) & (LeapYear == 0) ) {
Set_day = 1;
}
else
{
Set_day ++;
}
}
if ( rDir == 2 ) {
if (Set_day == 1)
{
if ((Set_month == 1 || Set_month == 3 || Set_month == 5 || Set_month == 7 || Set_month == 8 || Set_month == 10 || Set_month == 12)) {
Set_day = 31;
}
else if ((Set_month == 4 || Set_month == 6 || Set_month == 9 || Set_month == 11)) {
Set_day = 30;
}
else if (Set_month == 2) {
Set_day = 29;
}
else if ((Set_month == 2) & (LeapYear == 0) ) {
Set_day = 28;
}
}
else
{
Set_day —;
}
}
//In this part of the subroutine, the calculation of the day of the week:
byte a = f_div((14 — int(Set_month)), 12);
unsigned int y = (2000 + int(Set_year)) — a;
byte m = int(Set_month) + 12 * a — 2;
unsigned int y4 = f_div(y, 4);
byte y100 = f_div(y, 100);
byte y400 = f_div(y, 400);
byte x = f_div(31 * m, 12);
ned = (int(Set_day) + y + y4 — y100 + y400 + x) % 7; //the result in the «ned» variable
delay(100);
}
//////////////////
unsigned int f_div(unsigned int x, unsigned int y) {
unsigned int result;
result = (x — (x % y)) / y;
return result;
}
///////////////////
else if (s_mode == 4) {//with each rotation of the encoder, we will increase or decrease (depending on the direction of rotation of the encoder) the initial value of the hour. When increasing the hour value to «23», reset the hour value to «1». When reducing the hour value to «0», reset the hour value to «23».
if ( rDir == 1 ) {
if (Set_Hours == 23)
{
Set_Hours = 0;
}
else
{
Set_Hours ++;
}
}
if ( rDir == 2 ) {
if (Set_Hours == 0)
{
Set_Hours = 23;
}
else
{
Set_Hours —;
}
}
delay(100);
}
else if (s_mode == 5) {//with each rotation of the encoder, we will increase or decrease (depending on the direction of rotation of the encoder) the initial value of minutes. When increasing the minutes value to «59», reset the minutes value to «1». When reducing the minutes value to «0», reset the minutes value to «59».
if ( rDir == 1 ) {
if (Set_minutes == 59)
{
Set_minutes = 0;
}
else
{
Set_minutes ++;
}
}
if ( rDir == 2 ) {
if (Set_minutes == 0)
{
Set_minutes = 59;
}
else
{
Set_minutes —;
}
}
delay(100);
}
else if (s_mode == 6) {//with each rotation of the encoder, we will increase or decrease (depending on the direction of rotation of the encoder) the initial value of the clock stroke correction. When increasing the value to «59», reset the value to «1». When reducing the value to «0», reset the value to «59».
if ( rDir == 1 ) {
if (Corr_time == 59)
{
Corr_time = -59;
}
else
{
Corr_time++;
}
}
if ( rDir == 2 ) {
if (Corr_time == -59)
{
Corr_time = 59;
}
else
{
Corr_time —;
}
}
delay(100);
}
}
/////////////////////////
}//end while
}