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.
When event occurred slave will write the current hour and minute in Slave'S EEPROM.its also working fine.
when i reading this value,this showing on serial monitor like this 000135.I want to put "." decimal mark between hour and Minutes. {1.35}.
Another problem is that when i switch off and on the SLAVE arduino .Serial monitor shows zero reading.
Can someone explain me, how its possible?
MASTER CODE:
#include <EEPROM.h>
#include <Wire.h>
//#include <Wire.h>/////
#include "RTClib.h"///////////////////// rtc
RTC_DS3231 rtc;////////////////////////// rtc
#define PRESSED LOW
#define NOT_PRESSED HIGH
const unsigned long shortPress = 500;
const unsigned long longPress = 5000;
typedef struct Buttons {
const byte pin = 5;
const int debounce = 10;
unsigned long counter=0;
bool prevState = NOT_PRESSED;
bool currentState;
} Button;
Button button;
const uint8_t Toggle = 23;
const uint8_t maint = 3;
const uint8_t EngineCommand = 8;
float value1;
float diffrRead = 0.0;
float Mvalue2;
float Mval;
unsigned long time_now = 0;
boolean already = false;
int EventState = 0;
int addr_secnd = 0;
int addr_Minute = 2;
int addr_hrs1 = 3;
int addr_hrs2 = 4;
int addr_hrs3 = 5;
int addr_hrs4 = 6;
int addr_hrs5 = 7;
int addr_hrs7 = 8;
int addr_hrs8 = 9;
int addr_hrs9 = 10;
int addr_hrs10 = 11;
int secnd = 0;
int Minute = 0;
int hrs1 = 0;
int hrs2 = 0;
int hrs3 = 0;
int hrs4 = 0;
int hrs5 = 0;
int value_secnd;
int value_Minute;
int value_hrs1;
int value_hrs2;
int value_hrs3;
int value_hrs4;
int x6;
int x7;
int x8;
int x9;
int x10;
byte ok;
int period = 100;
unsigned long previousMillis = 0;
unsigned long currentMillis;
unsigned long EngoilPresdelay =0;//millis();
unsigned long Engoildelay=15000;
long lastTime = 0;
long seconds = EEPROM.read(addr_secnd);
long minutes = EEPROM.read(addr_Minute);
long hours = EEPROM.read(addr_hrs2);
long hours1= EEPROM.read(addr_hrs7);
long hours2= EEPROM.read(addr_hrs8);
long hours3= EEPROM.read(addr_hrs9);
bool button_State = false;//////////////////////////sd card
int TempState = 0;
int TempState1 = 0;
int TempState2 = 0;
int TempState3 = 0;
int TempState4 = 0;
int smsState = 0 ;
int smsState1 = 0 ;
int smsState2 = 0 ;
int oldsta=0;
void setup()
{
Serial.begin(9600);
Wire.begin();
pinMode(EngineCommand, INPUT);
pinMode(maint, INPUT);
// pinMode(lastmaint, INPUT);
pinMode(Toggle, INPUT_PULLUP);
pinMode(button.pin, INPUT_PULLUP);
//oled1.clearDisplay();
//oled1.display();
}
void loop() {
unsigned long startTimehmr = millis();
currentMillis = millis();
if (currentMillis - previousMillis >= period )
{
previousMillis += period;
if (digitalRead(EngineCommand) == LOW)
{
EventState = 0;
if (millis() - lastTime > 0)
{
seconds++;
lastTime = millis();
}
if (seconds >59)
{
minutes++;
seconds = 0;
}
if (minutes > 59)
{
hours++;
minutes = 0;
}
if (hours > 9)
{
hours1++;
hours = 0;
}
if (hours1 > 9)
{
hours2++;
hours1 = 0;
}
if (hours2 > 9)
{
hours3++;
hours2 = 0;
}
byte myTime[6];
myTime[0] =(hours3);
myTime[1] = (hours2);
myTime[2] = (hours1);
myTime[3] = (hours);
myTime[4] = (minutes);
myTime[5] = (seconds);
Wire.beginTransmission( 4 ); // transmit to device #4
Wire.write (myTime, sizeof(myTime)); // send data in natural binary to slave
Wire.endTransmission();
/*Serial.print(hours3);
Serial.print(":");
Serial.print(hours2);
Serial.print(":");
Serial.print(hours1);
Serial.print(":");
Serial.print(hours);
Serial.print(":");
Serial.print(minutes);
Serial.print(".");
Serial.println(seconds);
*/
}
}
if (digitalRead(EngineCommand) == HIGH)
{
if (EventState < 1)
{
EEPROM.update(addr_secnd, seconds);
//EEPROM.update(addr_secnd1, seconds1);
EEPROM.update(addr_Minute, minutes);
EEPROM.update(addr_hrs2, hours);
EEPROM.update(addr_hrs7, hours1);
EEPROM.update(addr_hrs8, hours2);
EEPROM.update(addr_hrs9, hours3);
EventState++;
}
/*byte myTime[6];
//myTime[0] = EEPROM.read(addr_hrs6);
// myTime[1] = EEPROM.read(addr_hrs5);
myTime[0] = EEPROM.read(addr_hrs9);
myTime[1] = EEPROM.read(addr_hrs8);
myTime[2] = EEPROM.read(addr_hrs7);
myTime[3] = EEPROM.read(addr_hrs2);
myTime[4] = EEPROM.read(addr_Minute);
myTime[5] = EEPROM.read(addr_secnd);
/* Serial.print(EEPROM.read(addr_hrs9));
Serial.print(EEPROM.read(addr_hrs8));
Serial.print(EEPROM.read(addr_hrs7));
Serial.print(EEPROM.read(addr_hrs2));
Serial.print(EEPROM.read(addr_Minute));
Serial.println(EEPROM.read(addr_secnd));*/
}
}
SLAVE CODE
#include <EEPROM.h>
#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;
byte buf[7] = {}; //;
//byte buf[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};//, 0x00, 0x00};
bool flag = false;
int addr_nhrs9 = 30;
const uint8_t EngineCommand = 8;
int eventstate=0;
float hmr=0.00;
int Eread;
void setup()
{
Serial.begin(9600);
Wire.begin(slaveAddress1);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Wire.onReceive(receiveEvent);
pinMode(EngineCommand, INPUT);
}
void loop()
{
if (flag == true){
display.setCursor(0, 0);
for (int r=0; r<5;r++){
byte y = buf[r];
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.print(y);
display.display();
}
flag == false;
}
delay(1000);
if (digitalRead(EngineCommand) == HIGH){
if( eventstate<1 )
{
addr_nhrs9=30;
for ( long int n = 0; n <5; n++)
{
//buf[n] = Wire.read();
EEPROM.update(addr_nhrs9,buf[n]);
addr_nhrs9++;
}
addr_nhrs9 = 30;
for (long int i = 0; i <5; i++)
{
long int Eread = EEPROM.read(addr_nhrs9);
addr_nhrs9++;
Serial.print(Eread);
}
eventstate++;
}
}
if (digitalRead(EngineCommand) == LOW){
eventstate=0;
}
}
void receiveEvent(int howMany)
{
for (int i = 0; i <=5; i++)
{
buf[i] = Wire.read(); //"buf"need to convert an integar value
byte x = buf[i];
/*if (x < 0x10)
{
Serial.print('0'); //print leading zero
}*/
Serial.print(x);
if (i == 3)
{
Serial.print('.');// show : as separator between Hrs and Min
}
else
{
if (i == 4)
{
Serial.print('.'); //show : as separator between Min and Sec
}
}
}
Serial.println();
flag = true;
}
TheMemberFormerlyKnownAsAWOL:
where you're only counting to 5?
I need only hrs and minutes value for being able to do something with it.
TheMemberFormerlyKnownAsAWOL:
use the IDE's auto-format tool before posting code.
sorry .
TheMemberFormerlyKnownAsAWOL:
flag == false;[/code]
Oops
why are you using longs for loop controls,
After changing ..problem is not solved.
TheMemberFormerlyKnownAsAWOL:
Please, do yourself (and us) a favour.
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.So please help me to solve this problem.
this is my code MASTER
#include <EEPROM.h>
#include <Wire.h>
//#include <Wire.h>/////
#include "RTClib.h"///////////////////// rtc
RTC_DS3231 rtc;////////////////////////// rtc
const uint8_t Toggle = 23;
const uint8_t maint = 3;
const uint8_t EngineCommand = 8;
float value1;
float diffrRead = 0.0;
float Mvalue2;
float Mval;
unsigned long time_now = 0;
boolean already = false;
int EventState = 0;
int addr_secnd = 0;
int addr_Minute = 2;
int addr_hrs1 = 3;
int addr_hrs2 = 4;
int addr_hrs3 = 5;
int addr_hrs4 = 6;
int addr_hrs5 = 7;
int addr_hrs7 = 8;
int addr_hrs8 = 9;
int addr_hrs9 = 10;
int addr_hrs10 = 11;
int secnd = 0;
int Minute = 0;
int hrs1 = 0;
int hrs2 = 0;
int hrs3 = 0;
int hrs4 = 0;
int hrs5 = 0;
int value_secnd;
int value_Minute;
int value_hrs1;
int value_hrs2;
int value_hrs3;
int value_hrs4;
int x6;
int x7;
int x8;
int x9;
int x10;
byte ok;
int period = 100;
unsigned long previousMillis = 0;
unsigned long currentMillis;
long lastTime = 0;
long seconds = EEPROM.read(addr_secnd);
long minutes = EEPROM.read(addr_Minute);
long hours = EEPROM.read(addr_hrs2);
long hours1 = EEPROM.read(addr_hrs7);
long hours2 = EEPROM.read(addr_hrs8);
long hours3 = EEPROM.read(addr_hrs9);
bool button_State = false;//////////////////////////sd card
void setup()
{
Serial.begin(9600);
Wire.begin();
pinMode(EngineCommand, INPUT);
}
void loop() {
unsigned long startTimehmr = millis();
currentMillis = millis();
if (currentMillis - previousMillis >= period )
{
previousMillis += period;
if (digitalRead(EngineCommand) == LOW)
{
EventState = 0;
if (millis() - lastTime > 0)
{
seconds++;
lastTime = millis();
}
if (seconds > 59)
{
minutes++;
seconds = 0;
}
if (minutes > 59)
{
hours++;
minutes = 0;
}
if (hours > 9)
{
hours1++;
hours = 0;
}
if (hours1 > 9)
{
hours2++;
hours1 = 0;
}
if (hours2 > 9)
{
hours3++;
hours2 = 0;
}
byte myTime[6];
myTime[0] = (hours3);
myTime[1] = (hours2);
myTime[2] = (hours1);
myTime[3] = (hours);
myTime[4] = (minutes);
myTime[5] = (seconds);
Wire.beginTransmission( 4 ); // transmit to device #4
Wire.write (myTime, sizeof(myTime)); // send data in natural binary to slave
Wire.endTransmission();
/*Serial.print(hours3);
Serial.print(":");
Serial.print(hours2);
Serial.print(":");
Serial.print(hours1);
Serial.print(":");
Serial.print(hours);
Serial.print(":");
Serial.print(minutes);
Serial.print(".");
Serial.println(seconds);
*/
}
}
if (digitalRead(EngineCommand) == HIGH)
{
if (EventState < 1)
{
EEPROM.update(addr_secnd, seconds);
//EEPROM.update(addr_secnd1, seconds1);
EEPROM.update(addr_Minute, minutes);
EEPROM.update(addr_hrs2, hours);
EEPROM.update(addr_hrs7, hours1);
EEPROM.update(addr_hrs8, hours2);
EEPROM.update(addr_hrs9, hours3);
EventState++;
}
}
}
SLAVE
#include <EEPROM.h>
#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;
byte buf[7] = {}; //;
//byte buf[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};//, 0x00, 0x00};
bool flag;// = false;
int addr_nhrs9 = 30;
const uint8_t EngineCommand = 8;
int eventstate = 0;
float hmr = 0.00;
int Eread;
void setup()
{
Serial.begin(9600);
Wire.begin(slaveAddress1);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Wire.onReceive(receiveEvent);
pinMode(EngineCommand, INPUT);
}
void loop()
{
if (flag == true) {
display.setCursor(0, 0);
for (int r = 0; r < 5; r++) {
byte y = buf[r];
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.print(y);
display.display();
}
flag == false;
}
delay(1000);
if (digitalRead(EngineCommand) == HIGH) {
if ( eventstate < 1 )
{
addr_nhrs9 = 30;
for (int n = 0; n < 5; n++)
{
EEPROM.update(addr_nhrs9, buf[n]);
addr_nhrs9++;
}
addr_nhrs9 = 30;
for (int i = 0; i < 5; i++)
{
int Eread = EEPROM.read(addr_nhrs9);
addr_nhrs9++;
Serial.print(Eread);
}
eventstate++;
}
}
if (digitalRead(EngineCommand) == LOW) {
eventstate = 0;
}
}
void receiveEvent(int howMany)
{
for (int i = 0; i <= 5; i++)
{
buf[i] = Wire.read(); //"buf"need to convert an integar value
byte x = buf[i];
Serial.print(x);
if (i == 3)
{
Serial.print('.');// show : as separator between Hrs and Min
}
else
{
if (i == 4)
{
Serial.print('.'); //show : as separator between Min and Sec
}
}
}
Serial.println();
flag = true;
}
I want to assign the slave's EEPROM stored value on a float variable. When i reading this value,this showing on serial monitor like this 000135 . I need to assign this value to a float variable this format (1.35) 1 hrs 35 minute.
Another problem is that when i switch off and on the SLAVE arduino .slave EEPROM value become Serial zero.
UKHeliBob:
Each EEPROM location can only store one byte, not a float
You can save a float to the EEPROM using EEPROM.put() and load it back with EEPROM.get()
Ok.
For example my meter readings is 1hr 35 minutes
Reading is 000135.
Write to eeprom address
0-> EEPROM address-30
0-> EEPROM address-31
0-> EEPROM address-32
1-> EEPROM address-33
3-> EEPROM address-34
5-> EEPROM address-35
When I read the value from those adress (eeprmRead)
Its showing 000135.
Now i want to assign this value to a float variable this format 0001.35 is it possible?
TheMemberFormerlyKnownAsAWOL:
Yes it is possible, but probably unnecessary (and even undesirable)
Assign the digit values to an integer.
I need this , because i want to send the running hours sms through gsm module on that format (0001.35).Apart from i want to substract the current running hrsfrom pre set value
Example :- pre set value is 10.00hrs
Substracting running hours ,=(10.00hrs - current running hours).
For all these purpose i want to change hrs 000135 format to 0001.35 format
Well, one way would be to assign it to an integer, and then divide by 100 and assign that value to a float.
But it is probably simpler to leave it as an integer.
TheMemberFormerlyKnownAsAWOL:
Well, one way would be to assign it to an integer, and then divide by 100 and assign that value to a float.
But it is probably simpler to leave it as an integer.
Now i have assigned this value to an integar (int hmr) and print in the serial monitor.But this converted the single value of an array in to integar one by one.in place of all array value in one time.
for example array value is 2.31 and its convert first value 2 and 31.
if i want to substract set value 1 from above integar, it will substract first from the value 2 and than 31.its not correct.
I need substraction like this (2.31-1=1.31)....still problem is existing ..please advice how to solve
this serial monitor shows integar value and array value (letters in Bold) in single line.First i need to print array value and integar value separate single line.