Convert char (arrays of chars) to int

Hi,

I have 2 arduinos which change data using I2C connection between one master and one slave arduino. Actually the master measure a engine working hours and send it to the slave and same has been displayed on OLED display which is connected to Slave arduino. Its working perfectly.

I need some help to convert chars i get on slave into an integer value for being able to do something with it.

Can someone explain me, how its possible?

This is my code

Master:

#include <Wire.h>
#include <EEPROM.h>


int slaveAddress = 4;
int period = 1000;
unsigned long time_now = 0;
int EngineLed = 13;
String val;


int addr_secnd = 0;
int addr_minute = 1;
int addr_hrs1 = 2;
int addr_hrs2 = 3;
int addr_hrs3 = 4;
int addr_hrs4 = 5;
int addr_hrs5 = 6;
int addr_hrs6 = 7;

int secnd = 0;
int minute = 0;
int hrs1 = 0;
int hrs2 = 0;
int hrs3 = 0;
int hrs4 = 0;
int hrs5 = 0;
int hrs6 = 0;

int value_secnd;
int value_minute;
int value_hrs1;
int value_hrs2;
int value_hrs3;
int value_hrs4;
int value_hrs5;
int value_hrs6;


void setup() {

  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
}

void loop() {

  value_secnd = value_secnd + 1;
  EEPROM.update(addr_secnd, value_secnd);

  if (value_secnd == 60) {
    secnd = 0;
    EEPROM.update(addr_secnd, secnd);
    value_minute = value_minute + 1;
    EEPROM.update(addr_minute, value_minute);
  }
  value_secnd = EEPROM.read(addr_secnd);
  //Serial.print("secnd:");
  //Serial.println(value_secnd);
  //Serial.print("\t");

  if (value_minute == 60) {
    minute = 0;
    EEPROM.update(addr_minute, minute);
    value_hrs1 = value_hrs1 + 1;
    EEPROM.update(addr_hrs1, value_hrs1);
  }
  value_minute = EEPROM.read(addr_minute);
  //Serial.print("minute:");
  //Serial.println(value_minute);
  //Serial.print("\t");

  if (value_hrs1 == 10) {
    hrs1 = 0;
    EEPROM.update(addr_hrs1, hrs1);
    value_hrs2 = value_hrs2 + 1;
    EEPROM.update(addr_hrs2, value_hrs2);
  }
  value_hrs1 = EEPROM.read(addr_hrs1);
  //Serial.print("hrs (1):");
  //Serial.println(value_hrs1);
  //Serial.print("\t");

  if (value_hrs2 == 10) {
    hrs2 = 0;
    EEPROM.update(addr_hrs2, hrs2);
    value_hrs3 = value_hrs3 + 1;
    EEPROM.update(addr_hrs3, value_hrs3);
  }
  value_hrs2 = EEPROM.read(addr_hrs2);
  //Serial.print("hrs (2):");
  //Serial.println(value_hrs2);
  //Serial.print("\t");

  if (value_hrs3 == 10) {
    hrs3 = 0;
    EEPROM.update(addr_hrs3, hrs3);
    value_hrs4 = value_hrs4 + 1;
    EEPROM.update(addr_hrs4, value_hrs4);
  }
  value_hrs3 = EEPROM.read(addr_hrs3);
  //Serial.print("hrs (3):");
  //Serial.println(value_hrs3);
  //Serial.print("\t");

  if (value_hrs4 == 10) {
    hrs4 = 0;
    EEPROM.update(addr_hrs4, hrs4);
    value_hrs5 = value_hrs5 + 1;
    EEPROM.update(addr_hrs5, value_hrs5);
  }
  value_hrs4 = EEPROM.read(addr_hrs4);
  //Serial.print("hrs (4):");
  //Serial.println(value_hrs4);
  //Serial.print("\t");

  if (value_hrs5 == 10) {
    hrs5 = 0;
    EEPROM.update(addr_hrs5, hrs5);
    value_hrs6 = value_hrs6 + 1;
    EEPROM.update(addr_hrs6, value_hrs6);
  }
  value_hrs5 = EEPROM.read(addr_hrs5);
  //Serial.print("hrs (5):");
  //Serial.println(value_hrs5);
  //Serial.print("\t");

  if (value_hrs6 == 10) {
    hrs6 = 0;
    EEPROM.update(addr_hrs6, hrs6);

  }
  value_hrs6 = EEPROM.read(addr_hrs6);
  //Serial.print("hrs(6):");
  //Serial.println(value_hrs6);
  //Serial.print("\t");


  int x1 = EEPROM.read(addr_hrs6);
  int x2 = EEPROM.read(addr_hrs5);
  int x3 = EEPROM.read(addr_hrs4);
  int x4 = EEPROM.read(addr_hrs3);
  int x5 = EEPROM.read(addr_hrs2);
  int x6 = EEPROM.read(addr_hrs1);
  int x7 = EEPROM.read(addr_minute);
  int x8 = EEPROM.read(addr_secnd);
  String val = String(x1) + String(x2) + String(x3) + String(x4) + String(x5) + String(x6) + String(".") + String(x7) + String(".") + String(x8);
  //Serial.print(val);
  //int str_len = val.length() + 1;
  char char_array[13];
  val.toCharArray(char_array, 13); ///string converted int "char"
  //Serial.println(char_array);
  /////////////////////////////////////////////////////////////////
  Wire.beginTransmission( 4 );                  // transmit to device #4
  Wire.write (char_array, sizeof(char_array)); // send "char_array" to slave
  Wire.endTransmission();                      // stop transmitting

  time_now = millis();
  digitalWrite(EngineLed, HIGH);
  while (millis() < time_now + period)
  {
    //wait approx. [period] ms
  }
  digitalWrite(EngineLed, LOW);

}

Slave:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

//#define VCCSTATE SH1106_SWITCHCAPVCC
#define WIDTH     128
#define HEIGHT     64
#define NUM_PAGE    8
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16

const byte slaveAddress1 = 4;
char buf[15] = ""; //;
void setup()
{
  Serial.begin(9600);
  Wire.begin(slaveAddress1);        
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  Wire.onReceive(receiveEvent);
}

void loop()
{
  Serial.println(buf);  //d
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);

  display.println(buf);
  display.display();
  delay(1000);
}
void receiveEvent(int howMany)
{
  while  (Wire.available())
  {
    for (int i = 0; i < howMany; i++)
    {

      buf[i] = Wire.read();  //arduino buf[8] = Wire.read();  //arduino

    }
  }
}

serial monitor slave arduino: (Eg:-6hrs.14minut.25 Second.)

000006.14.25
000006.14.26
000006.14.27
000006.14.28
000006.14.29
000006.14.30
000006.14.32
000006.14.33
000006.14.34
000006.14.35
000006.14.36
000006.14.37
000006.14.39
000006.14.40
000006.14.41
000006.14.42
000006.14.43
000006.14.44
000006.14.46

Hi,
I have 2 arduinos which change data using I2C. Actually the master monitor the engine working houes (Hrs/Min/Sec) and send it to the slave.It will display the OLED Display which is attached to the slave arduino.

I need some help to convert chars i get on slave into an integer value for being able to do something with it.

This is my code

Master:

#include <Wire.h>
#include <EEPROM.h>


int slaveAddress = 4;
int period = 1000;
unsigned long time_now = 0;
int EngineLed = 13;
String val;


int addr_secnd = 0;
int addr_minute = 1;
int addr_hrs1 = 2;
int addr_hrs2 = 3;
int addr_hrs3 = 4;
int addr_hrs4 = 5;
int addr_hrs5 = 6;
int addr_hrs6 = 7;

int secnd = 0;
int minute = 0;
int hrs1 = 0;
int hrs2 = 0;
int hrs3 = 0;
int hrs4 = 0;
int hrs5 = 0;
int hrs6 = 0;

int value_secnd;
int value_minute;
int value_hrs1;
int value_hrs2;
int value_hrs3;
int value_hrs4;
int value_hrs5;
int value_hrs6;


void setup() {

  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
}

void loop() {

  value_secnd = value_secnd + 1;
  EEPROM.update(addr_secnd, value_secnd);

  if (value_secnd == 60) {
    secnd = 0;
    EEPROM.update(addr_secnd, secnd);
    value_minute = value_minute + 1;
    EEPROM.update(addr_minute, value_minute);
  }
  value_secnd = EEPROM.read(addr_secnd);
  //Serial.print("secnd:");
  //Serial.println(value_secnd);
  //Serial.print("\t");

  if (value_minute == 60) {
    minute = 0;
    EEPROM.update(addr_minute, minute);
    value_hrs1 = value_hrs1 + 1;
    EEPROM.update(addr_hrs1, value_hrs1);
  }
  value_minute = EEPROM.read(addr_minute);
  //Serial.print("minute:");
  //Serial.println(value_minute);
  //Serial.print("\t");

  if (value_hrs1 == 10) {
    hrs1 = 0;
    EEPROM.update(addr_hrs1, hrs1);
    value_hrs2 = value_hrs2 + 1;
    EEPROM.update(addr_hrs2, value_hrs2);
  }
  value_hrs1 = EEPROM.read(addr_hrs1);
  //Serial.print("hrs (1):");
  //Serial.println(value_hrs1);
  //Serial.print("\t");

  if (value_hrs2 == 10) {
    hrs2 = 0;
    EEPROM.update(addr_hrs2, hrs2);
    value_hrs3 = value_hrs3 + 1;
    EEPROM.update(addr_hrs3, value_hrs3);
  }
  value_hrs2 = EEPROM.read(addr_hrs2);
  //Serial.print("hrs (2):");
  //Serial.println(value_hrs2);
  //Serial.print("\t");

  if (value_hrs3 == 10) {
    hrs3 = 0;
    EEPROM.update(addr_hrs3, hrs3);
    value_hrs4 = value_hrs4 + 1;
    EEPROM.update(addr_hrs4, value_hrs4);
  }
  value_hrs3 = EEPROM.read(addr_hrs3);
  //Serial.print("hrs (3):");
  //Serial.println(value_hrs3);
  //Serial.print("\t");

  if (value_hrs4 == 10) {
    hrs4 = 0;
    EEPROM.update(addr_hrs4, hrs4);
    value_hrs5 = value_hrs5 + 1;
    EEPROM.update(addr_hrs5, value_hrs5);
  }
  value_hrs4 = EEPROM.read(addr_hrs4);
  //Serial.print("hrs (4):");
  //Serial.println(value_hrs4);
  //Serial.print("\t");

  if (value_hrs5 == 10) {
    hrs5 = 0;
    EEPROM.update(addr_hrs5, hrs5);
    value_hrs6 = value_hrs6 + 1;
    EEPROM.update(addr_hrs6, value_hrs6);
  }
  value_hrs5 = EEPROM.read(addr_hrs5);
  //Serial.print("hrs (5):");
  //Serial.println(value_hrs5);
  //Serial.print("\t");

  if (value_hrs6 == 10) {
    hrs6 = 0;
    EEPROM.update(addr_hrs6, hrs6);

  }
  value_hrs6 = EEPROM.read(addr_hrs6);
  //Serial.print("hrs(6):");
  //Serial.println(value_hrs6);
  //Serial.print("\t");


  int x1 = EEPROM.read(addr_hrs6);
  int x2 = EEPROM.read(addr_hrs5);
  int x3 = EEPROM.read(addr_hrs4);
  int x4 = EEPROM.read(addr_hrs3);
  int x5 = EEPROM.read(addr_hrs2);
  int x6 = EEPROM.read(addr_hrs1);
  int x7 = EEPROM.read(addr_minute);
  int x8 = EEPROM.read(addr_secnd);
  String val = String(x1) + String(x2) + String(x3) + String(x4) + String(x5) + String(x6) + String(".") + String(x7) + String(".") + String(x8);
  //Serial.print(val);
  //int str_len = val.length() + 1;
  char char_array[13];
  val.toCharArray(char_array, 13); ///string converted int "char"
  //Serial.println(char_array);
  /////////////////////////////////////////////////////////////////
  Wire.beginTransmission( 4 );                  // transmit to device #4
  Wire.write (char_array, sizeof(char_array)); // send "char_array" to slave
  Wire.endTransmission();                      // stop transmitting

  time_now = millis();
  digitalWrite(EngineLed, HIGH);
  while (millis() < time_now + period)
  {
    //wait approx. [period] ms
  }
  digitalWrite(EngineLed, LOW);

}

Slave;

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

//#define VCCSTATE SH1106_SWITCHCAPVCC
#define WIDTH     128
#define HEIGHT     64
#define NUM_PAGE    8
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16

const byte slaveAddress1 = 4;
char buf[15] = ""; //;

void setup()
{
  Serial.begin(9600);
  Wire.begin(slaveAddress1);        
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  Wire.onReceive(receiveEvent);
}

void loop()
{
  Serial.println(buf);  
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);

  display.println(buf);
  display.display();
  delay(1000);
}
void receiveEvent(int howMany)
{
  while  (Wire.available())
  {
    for (int i = 0; i < howMany; i++)
    {

      buf[i] = Wire.read();   //"buf"need to convert an integar value

    }
  }
}

So what i want is to get “buf” as an integer value.

Serial monitor of Slave:(Ex:-000006Hrs.56Mts.14Sec)

000006.56.14
000006.56.15
000006.56.16
000006.56.17
000006.56.18
000006.56.19
000006.56.21
000006.56.22

Thanks.

It is much easier to just send integer values directly, as binary rather than a formatted character array. Otherwise, use atoi(), strtoul(), sscanf(), etc.

Have you calculated how long the program will run, until you exceed the write limit on EEPROM?

1. You have following 8-byte integer data in natural binary (probably BCD format):

  byte myTime[8];
  myTime[0] = EEPROM.read(addr_hrs6);
  myTime[1] = EEPROM.read(addr_hrs5);
  myTime[2] = EEPROM.read(addr_hrs4);
  myTime[3] = EEPROM.read(addr_hrs3);
  myTime[4] = EEPROM.read(addr_hrs2);
  myTime[5] = EEPROM.read(addr_hrs1);
  myTime[6] = EEPROM.read(addr_minute);
  myTime[7] = EEPROM.read(addr_secnd);

2. Send the data of Step-1 to Slave using I2C Bus:

Wire.beginTransmission( 4 );                  // transmit to device #4
  Wire.write (myTime, sizeof(myTime));            // send data in natural binary to slave
  Wire.endTransmission();                      // stop transmitting

3. Reception at the Slave side.

void receiveEvent(int howMany)
{
    for (int i = 0; i < howMany; i++)
    {

      buf[i] = Wire.read();   //"buf" contains natural binary data

    }
    flag = true;     //use this flag in loop() function to process/display hrs:min:sec
}

Actually the master measure a engine working hours and send it to the slave and same has been displayed on OLED display which is connected to Slave arduino.

Bad idea. The first time the master tries to send something while the slave is actually sending data to the display your setup will fail.
Why did you choose I2C for the connection of the two Arduinos? Why do you have two Arduinos at all? The first (master) Arduino could drive the display itself.

Your working hours meter will wear out at about 800 hours as the EEPROM cells will be dead about then.

The function you're looking for is probably atoi(). Although that conversion isn't that hard as you have only a few digits and a clear format.

BTW: you should eliminate the use of the String class, sooner or later your code will fail because the String class fragments the memory and once no big enough block is available the result will be random chaos.

Thank you for your reply.First of all i am not well expert in programming .i have made this program after referring several program from the google.

GolamMostafa:
1. You have following 8-byte integer data in natural binary (probably BCD format):

  byte myTime[8];

myTime[0] = EEPROM.read(addr_hrs6);
 myTime[1] = EEPROM.read(addr_hrs5);
 myTime[2] = EEPROM.read(addr_hrs4);
 myTime[3] = EEPROM.read(addr_hrs3);
 myTime[4] = EEPROM.read(addr_hrs2);
 myTime[5] = EEPROM.read(addr_hrs1);
 myTime[6] = EEPROM.read(addr_minute);
 myTime[7] = EEPROM.read(addr_secnd);

Modified Master

#include <Wire.h>
#include <EEPROM.h>


int slaveAddress = 4;
int period = 1000;
unsigned long time_now = 0;
int EngineLed = 13;
String val;


int addr_secnd = 0;
int addr_minute = 1;
int addr_hrs1 = 2;
int addr_hrs2 = 3;
int addr_hrs3 = 4;
int addr_hrs4 = 5;
int addr_hrs5 = 6;
int addr_hrs6 = 7;

int secnd = 0;
int minute = 0;
int hrs1 = 0;
int hrs2 = 0;
int hrs3 = 0;
int hrs4 = 0;
int hrs5 = 0;
int hrs6 = 0;

int value_secnd;
int value_minute;
int value_hrs1;
int value_hrs2;
int value_hrs3;
int value_hrs4;
int value_hrs5;
int value_hrs6;


void setup() {

  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
}

void loop() {

  value_secnd = value_secnd + 1;
  EEPROM.update(addr_secnd, value_secnd);

  if (value_secnd == 60) {
    secnd = 0;
    EEPROM.update(addr_secnd, secnd);
    value_minute = value_minute + 1;
    EEPROM.update(addr_minute, value_minute);
  }
  value_secnd = EEPROM.read(addr_secnd);
  //Serial.print("secnd:");
  //Serial.println(value_secnd);
  //Serial.print("\t");

  if (value_minute == 60) {
    minute = 0;
    EEPROM.update(addr_minute, minute);
    value_hrs1 = value_hrs1 + 1;
    EEPROM.update(addr_hrs1, value_hrs1);
  }
  value_minute = EEPROM.read(addr_minute);
  //Serial.print("minute:");
  //Serial.println(value_minute);
  //Serial.print("\t");

  if (value_hrs1 == 10) {
    hrs1 = 0;
    EEPROM.update(addr_hrs1, hrs1);
    value_hrs2 = value_hrs2 + 1;
    EEPROM.update(addr_hrs2, value_hrs2);
  }
  value_hrs1 = EEPROM.read(addr_hrs1);
  //Serial.print("hrs (1):");
  //Serial.println(value_hrs1);
  //Serial.print("\t");

  if (value_hrs2 == 10) {
    hrs2 = 0;
    EEPROM.update(addr_hrs2, hrs2);
    value_hrs3 = value_hrs3 + 1;
    EEPROM.update(addr_hrs3, value_hrs3);
  }
  value_hrs2 = EEPROM.read(addr_hrs2);
  //Serial.print("hrs (2):");
  //Serial.println(value_hrs2);
  //Serial.print("\t");

  if (value_hrs3 == 10) {
    hrs3 = 0;
    EEPROM.update(addr_hrs3, hrs3);
    value_hrs4 = value_hrs4 + 1;
    EEPROM.update(addr_hrs4, value_hrs4);
  }
  value_hrs3 = EEPROM.read(addr_hrs3);
  //Serial.print("hrs (3):");
  //Serial.println(value_hrs3);
  //Serial.print("\t");

  if (value_hrs4 == 10) {
    hrs4 = 0;
    EEPROM.update(addr_hrs4, hrs4);
    value_hrs5 = value_hrs5 + 1;
    EEPROM.update(addr_hrs5, value_hrs5);
  }
  value_hrs4 = EEPROM.read(addr_hrs4);
  //Serial.print("hrs (4):");
  //Serial.println(value_hrs4);
  //Serial.print("\t");

  if (value_hrs5 == 10) {
    hrs5 = 0;
    EEPROM.update(addr_hrs5, hrs5);
    value_hrs6 = value_hrs6 + 1;
    EEPROM.update(addr_hrs6, value_hrs6);
  }
  value_hrs5 = EEPROM.read(addr_hrs5);
  //Serial.print("hrs (5):");
  //Serial.println(value_hrs5);
  //Serial.print("\t");

  if (value_hrs6 == 10) {
    hrs6 = 0;
    EEPROM.update(addr_hrs6, hrs6);

  }
  value_hrs6 = EEPROM.read(addr_hrs6);
  //Serial.print("hrs(6):");
  //Serial.println(value_hrs6);
  //Serial.print("\t");

byte myTime[8];
  myTime[0] = EEPROM.read(addr_hrs6);
  myTime[1] = EEPROM.read(addr_hrs5);
  myTime[2] = EEPROM.read(addr_hrs4);
  myTime[3] = EEPROM.read(addr_hrs3);
  myTime[4] = EEPROM.read(addr_hrs2);
  myTime[5] = EEPROM.read(addr_hrs1);
  myTime[6] = EEPROM.read(addr_minute);
  myTime[7] = EEPROM.read(addr_secnd);

Wire.beginTransmission( 4 );                  // transmit to device #4
  Wire.write (myTime, sizeof(myTime));            // send data in natural binary to slave
  Wire.endTransmission();                      // stop transmitting                 
 /* int x1 = EEPROM.read(addr_hrs6);
  int x2 = EEPROM.read(addr_hrs5);
  int x3 = EEPROM.read(addr_hrs4);
  int x4 = EEPROM.read(addr_hrs3);
  int x5 = EEPROM.read(addr_hrs2);
  int x6 = EEPROM.read(addr_hrs1);
  int x7 = EEPROM.read(addr_minute);
  int x8 = EEPROM.read(addr_secnd);
  String val = String(x1) + String(x2) + String(x3) + String(x4) + String(x5) + String(x6) + String(".") + String(x7) + String(".") + String(x8);
  //Serial.print(val);
  //int str_len = val.length() + 1;
  char char_array[13];
  val.toCharArray(char_array, 13); ///string converted int "char"
  //Serial.println(char_array);
  /////////////////////////////////////////////////////////////////
  Wire.beginTransmission( 4 );                  // transmit to device #4
  Wire.write (char_array, sizeof(char_array)); // send "char_array" to slave
  Wire.endTransmission();    */                  // stop transmitting

  time_now = millis();
  digitalWrite(EngineLed, HIGH);
  while (millis() < time_now + period)
  {
    //wait approx. [period] ms
  }
  digitalWrite(EngineLed, LOW);

}

GolamMostafa:
3. Reception at the Slave side.

void receiveEvent(int howMany)

{
    for (int i = 0; i < howMany; i++)
    {

buf[i] = Wire.read();  //“buf” contains natural binary data

}
    flag = true;    //use this flag in loop() function to process/display hrs:min:sec
}

Modified Slave side

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

//#define VCCSTATE SH1106_SWITCHCAPVCC
#define WIDTH     128
#define HEIGHT     64
#define NUM_PAGE    8
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16

const byte slaveAddress1 = 4;
char buf[15] = ""; //;
bool flag = false; 
void setup()
{
  Serial.begin(9600);
  Wire.begin(slaveAddress1);        
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  Wire.onReceive(receiveEvent);
}

void loop()
{
  if (flag == true)
  {
  Serial.print(buf);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);

  display.println(buf);
  display.display();
  delay(1000);
flag == false;
}
}
void receiveEvent(int howMany)
{
  
  
    for (int i = 0; i < howMany; i++)
    {

      buf[i] = Wire.read();   //"buf"need to convert an integar value
 
    }
   flag = true;
}

Nothing was appeared on slave’s Serial monitor.

At least serial monitor should show the Hrs&Minute(“hrs.minute” this format)

pylon:
Your working hours meter will wear out at about 800 hours as the EEPROM cells will be dead about then.

First of all very new to program as well as Arduino.i have made this program after referring several program from the Google.
Please explain how the meter not work after 800 hrs.
I think meter will work up to 999999.hrs.

pylon:
Bad idea. The first time the master tries to send something while the slave is actually sending data to the display your setup will fail.
Why did you choose I2C for the connection of the two Arduinos? Why do you have two Arduinos at all? The first (master) Arduino could drive the display itself.

Actually first i made the program like that only.that time loop() getting delayed as well as time counting getting delayed.So that i think use two Arduinos.One only for time counting and another one for temperature reading, display and other executions.
Thanks

byte myTime[8];
  myTime[0] = EEPROM.read(addr_hrs6);
  myTime[1] = EEPROM.read(addr_hrs5);
  myTime[2] = EEPROM.read(addr_hrs4);
  myTime[3] = EEPROM.read(addr_hrs3);
  myTime[4] = EEPROM.read(addr_hrs2);
  myTime[5] = EEPROM.read(addr_hrs1);
  myTime[6] = EEPROM.read(addr_minute);
  myTime[7] = EEPROM.read(addr_secnd);

Wire.beginTransmission( 4 );                  // transmit to device #4
  Wire.write (myTime, sizeof(myTime));            // send data in natural binary to slave
  Wire.endTransmission();

You are sending numerical byte values, none of which are in the range of a printable char, but on the receiving end you have

char buf[15] = "";

Try changing the receiving buffer to

byte buf[15] = {0};

And print it out with a for loop

for(i = 0; i < 15, i++)
 { Serial.println(buf[i]);}

Duplicate topics merged

@sreekanthmp - do not post the same question in 2 sections of the forum as it wastes the time of those replying

UKHeliBob:
Duplicate topics merged

@sreekanthmp - do not post the same question in 2 sections of the forum as it wastes the time of those replying

Sorry for that!

cattledog:

byte myTime[8];

myTime[0] = EEPROM.read(addr_hrs6);
 myTime[1] = EEPROM.read(addr_hrs5);
 myTime[2] = EEPROM.read(addr_hrs4);
 myTime[3] = EEPROM.read(addr_hrs3);
 myTime[4] = EEPROM.read(addr_hrs2);
 myTime[5] = EEPROM.read(addr_hrs1);
 myTime[6] = EEPROM.read(addr_minute);
 myTime[7] = EEPROM.read(addr_secnd);

Wire.beginTransmission( 4 );                  // transmit to device #4
 Wire.write (myTime, sizeof(myTime));            // send data in natural binary to slave
 Wire.endTransmission();





You are sending numerical byte values, none of which are in the range of a printable char, but on the receiving end you have



char buf[15] = “”;




Try changing the receiving buffer to 



byte buf[15] = {0};




And print it out with a for loop



for(i = 0; i < 15, i++)
{ Serial.println(buf[i]);}

After modification now i have receiving slave’s serial monitor Out put.Here is this:-

00008241000008242000008243000008244000008245000008246000008247000008248000008249

But nothing was displaying on OLED and there is no hrs and minute separation mark( “.”) in the S.monitor out put.

when i transffered data from master to slave as array of chars format it is comparatively better than transfer the bytes(integar) format.Because it is in integer data transferring, it is happend one by one.

Clean up the anonymous printing. What do you see with this

for(i = 0; i < 15, i++)
 { 
    Serial.print(i);
    Serial.print('\t');
    Serial.println(buf[i],DEC);
 }

You have received only 8-byte data (probably BCD format) consisting of:
Hrs: xxxxxxxxxxxx (12-digit) //6-byte
Min: xx (2-digit) //1-byte
Sec: xx (2-digit) //1-byte

Example code:

byte buf[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x45, 0x37};
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  for (int i = 0; i < 8; i++)
  {
    byte x = buf[i];
    if (x < 0x10)
    {
      Serial.print('0');     //print leading zero
    }
    Serial.print(x, HEX);
    if (i == 5)
    {
      Serial.print(':');// show : as separator between Hrs and Min
    }
    else
    {
      if (i == 6)
      {
        Serial.print(':'); //show : as separator between Min and Sec
      }
    }
  }
  Serial.println();
  delay(1000);
}

smz.png

smz.png

sreekanthmp:
Please explain how the meter not work after 800 hrs.
I think meter will work up to 999999.hrs.

You write to the addr_secnd address of EEPROM once per second. The EEPROM on the AVR Arduinos (Uno, Nano, Mega, Leonardo, etc.) is rated for 100000 writes per cell.

pert:
You write to the addr_secnd address of EEPROM once per second. The EEPROM on the AVR Arduinos (Uno, Nano, Mega, Leonardo, etc.) is rated for 100000 writes per cell.

How many cells are there in the 1 kByte EEPROM of Atmega328? Is one EEPROM location a cell? I have the understanding that one write cycle in one location is considered as written into all locations of the EEPROM from the view point of endurance. Please, clarify.

GolamMostafa:
How many cells are there in the 1 kByte EEPROM of Atmega328?

  1. Each cell stores a byte.

GolamMostafa:
I have the understanding that one write cycle in one location is considered as written into all locations of the EEPROM from the view point of endurance. Please, clarify.

No, each cell is rated for 100000. So you can write to address 0 100000 times, address 1 100000 times, and so on. This is why people use wear leveling to extend the life of the EEPROM. In a case like this, where there is much more EEPROM capacity available than is needed to store sreekanthmp's data, you can rotate the writes across many cells and multiply the life expectancy of the device before it wears out the EEPROM.

pert:
You write to the addr_secnd address of EEPROM once per second. The EEPROM on the AVR Arduinos (Uno, Nano, Mega, Leonardo, etc.) is rated for 100000 writes per cell.

This is very dissapointing information for me.Is there any other way to accomplish my goal Hour meter.Please help me.

Well, first of all, do you really need to record the run time to the second? That seems a bit excessive to me.

There is also the idea of adding components to allow you to detecting the loss of the Arduino board's power and writing the data to EEPROM only then. That will require a backup power source. I believe doing this with a capacitor has been discussed quite a few times here on the forum.

If that's not to your liking, there's the concept of wear leveling I mentioned, where you rotate between multiple addresses. That will multiply the life expectancy.

pert:
Well, first of all, do you really need to record the run time to the second? That seems a bit excessive to me.

  1. Recording the seconds is not necessary but hrs and minute is compulsory.

pert:
There is also the idea of adding components to allow you to detecting the loss of the Arduino board's power and writing the data to EEPROM only then.

2)In my project there is a battery back up for when vehicle ignition switch off condition,if there is any possibility to write the data to EEPROM , once engine get switch off.Please let me know.

  1. This hour meter having following feature:-
    a) it will send the SMS of engine current working hour on pre determine time on daily basis.
    (GSM900A and RTC Module used)
    b) It will send the sms at the time of engine maintenance due.

c) Display oled shows the current working hours once vehicle key switch to a ON position.After
starting the engine it will start to count hrs and minute

sreekanthmp:
I think meter will work up to 999999.hrs.

Somewhat over a century.
Hope you’ve got a good test plan to pass on to your descendants.