w25Q flash in arduino nano 33 Ble - help required

HI members ,
I am new to arduino and learning day by day, i setup w25q memory and ds3231 memory module as per pin no . i want to save the time and date in flash memory whenever i send a variable in serial monitor , this is code -

type or paste code here
#include <SPI.h>
#include <Wire.h>
#include <DS3231.h>

DS3231 myRTC;
#define SPI_SPEED_HZ    800000
#define BLINK_INTERVAL  400
#define LED_PWR         13

int userVariable = 0; // User-defined variable
bool century = false;
bool h12Flag;
bool pmFlag;

const int CS_PIN = 7; // Chip Select pin for the W25Q64Mb flash module

void setup() {
  Serial.begin(115200);
  while (!Serial);

  SPI.begin();
  pinMode(CS_PIN, OUTPUT);
  digitalWrite(CS_PIN, HIGH); // Deselect the flash module initially

  Wire.begin();

  // Set the initial date and time in the RTC module
  myRTC.setYear(24); // Set the year (e.g., 2021)
  myRTC.setMonth(04); // Set the month (e.g., September)
  myRTC.setDate(15); // Set the day (e.g., 15th)
  myRTC.setHour(20); // Set the hour (e.g., 12 PM)
  myRTC.setMinute(0); // Set the minute
  myRTC.setSecond(0); // Set the second

  // Display the initial date and time set in the RTC module
  Serial.print("Initial Date and Time: ");
  Serial.print(myRTC.getYear(), DEC);
  Serial.print("-");
  Serial.print(myRTC.getMonth(century), DEC);
  Serial.print("-");
  Serial.print(myRTC.getDate(), DEC);
  Serial.print(" ");
  Serial.print(myRTC.getHour(h12Flag, pmFlag), DEC); // 24-hr
  Serial.print(":");
  Serial.print(myRTC.getMinute(), DEC);
  Serial.print(":");
  Serial.println(myRTC.getSecond(), DEC);
}
void writeTimeToFlash(int year, int month, int day, int hour, int minute, int second) {
  digitalWrite(CS_PIN, LOW); // Select the flash module

  // Write the current time and date data to the flash memory
  SPI.transfer(year);
  SPI.transfer(month);
  SPI.transfer(day);
  SPI.transfer(hour);
  SPI.transfer(minute);
  SPI.transfer(second);

  digitalWrite(CS_PIN, HIGH); // Deselect the flash module
}

void loop() {
  if (Serial.available() > 0) {
    int userVariable = Serial.parseInt();
    DateTime now = myRTC.getDate();

    // Display the variable sending time
    Serial.print("Variable ");
    Serial.print(userVariable);
    Serial.print(" sent at: ");
    Serial.print(now.year(), DEC);
    Serial.print("-");
    bool century = false;
    Serial.print(myRTC.getMonth(century), DEC);
    Serial.print("-");
    Serial.print(now.day(), DEC);
    Serial.print(" ");
    Serial.print(now.hour(), DEC);
    Serial.print(":");
    Serial.print(now.minute(), DEC);
    Serial.print(":");
    Serial.println(now.second(), DEC);

    if (userVariable == 85) {
      // Update the current time in the RTC module
      myRTC.setHour(now.hour());
      myRTC.setMinute(now.minute());
      myRTC.setSecond(now.second());
      writeTimeToFlash(now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
      Serial.println("Current time and date written to flash memory.");
    }
  }
}

output of my code is -
Initial Date and Time: 24-4-15 20:0:0
Variable 85 sent at: 2106-9-7 6:28:31
Current time and date written to flash memory.
Variable 0 sent at: 2106-9-7 6:28:31

what is happening , i am unable to store time and date , why its shows 2106? help appreciated..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.