Created
January 8, 2023 17:57
-
-
Save Ajak58a/7406b6199c3aad67de9983699343b871 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include "RTClib.h" | |
#define IIC_MEMORY_ADD 0x50 | |
unsigned int a,data_address = 0; | |
int analog_value; | |
char c; | |
RTC_DS1307 rtc; | |
void setup(void) | |
{ | |
Serial.begin(9600); | |
Wire.begin(); | |
Serial.println("I2C memory writing and reading program"); | |
if (! rtc.begin()) | |
{ | |
Serial.println("Couldn't find RTC"); | |
while (1); | |
} | |
if (! rtc.isrunning()) | |
{ | |
Serial.println("RTC is NOT running!"); | |
} | |
Serial.println("writing data to memory "); | |
//rtc.adjust(DateTime(2021, 2, 9, 21, 47, 00)); | |
} | |
void loop() | |
{ | |
analog_value = analogRead(A0); | |
save_sensor_readings_to_memory_with_timestamp(); | |
if(Serial.available()) | |
{ | |
c = Serial.read(); | |
if(c == 'r') | |
{ | |
Serial.println("read data: "); | |
retrive_sensor_data(); | |
} | |
} | |
delay(5000); | |
} | |
void retrive_sensor_data() | |
{ | |
for(a=0;a<data_address;a+=7) | |
{ | |
Serial.print("Sensor value: "); | |
Serial.print(read_data_from_IIC_MEMORY(IIC_MEMORY_ADD, a), DEC); | |
Serial.print("\tTime: "); | |
Serial.print(read_data_from_IIC_MEMORY(IIC_MEMORY_ADD, a+1), DEC); | |
Serial.print(':'); | |
Serial.print(read_data_from_IIC_MEMORY(IIC_MEMORY_ADD, a+2), DEC); | |
Serial.print(':'); | |
Serial.print(read_data_from_IIC_MEMORY(IIC_MEMORY_ADD, a+3), DEC); | |
Serial.print("\tDate: "); | |
Serial.print(read_data_from_IIC_MEMORY(IIC_MEMORY_ADD, a+4), DEC); | |
Serial.print('/'); | |
Serial.print(read_data_from_IIC_MEMORY(IIC_MEMORY_ADD, a+5), DEC); | |
Serial.print('/'); | |
Serial.println(read_data_from_IIC_MEMORY(IIC_MEMORY_ADD, a+6), DEC); | |
} | |
} | |
void save_sensor_readings_to_memory_with_timestamp() | |
{ | |
DateTime now = rtc.now(); | |
write_data_to_IIC_MEMORY(IIC_MEMORY_ADD, data_address, analog_value); | |
data_address++; | |
write_data_to_IIC_MEMORY(IIC_MEMORY_ADD, data_address, now.hour()); | |
data_address++; | |
write_data_to_IIC_MEMORY(IIC_MEMORY_ADD, data_address, now.minute()); | |
data_address++; | |
write_data_to_IIC_MEMORY(IIC_MEMORY_ADD, data_address, now.second()); | |
data_address++; | |
write_data_to_IIC_MEMORY(IIC_MEMORY_ADD, data_address, now.day()); | |
data_address++; | |
write_data_to_IIC_MEMORY(IIC_MEMORY_ADD, data_address, now.month()); | |
data_address++; | |
write_data_to_IIC_MEMORY(IIC_MEMORY_ADD, data_address, now.year()%100); | |
data_address++; | |
} | |
void write_data_to_IIC_MEMORY(int device_add, unsigned int data_add, byte data ) | |
{ | |
Wire.beginTransmission(device_add); | |
Wire.write((int)(data_add >> 8)); // MSB | |
Wire.write((int)(data_add & 0xFF)); // LSB | |
Wire.write(data); | |
Wire.endTransmission(); | |
delay(5); | |
} | |
byte read_data_from_IIC_MEMORY(int device_add, unsigned int data_add ) | |
{ | |
byte recv_data = 0xFF; | |
Wire.beginTransmission(device_add); | |
Wire.write((int)(data_add >> 8)); // MSB | |
Wire.write((int)(data_add & 0xFF)); // LSB | |
Wire.endTransmission(); | |
Wire.requestFrom(device_add,1); | |
if (Wire.available()) recv_data = Wire.read(); | |
return recv_data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment