so I'm facing a pretty large problem. I'm experimenting with I2C and using one Arduino that acts as a countdown timer to send the current reading on the timer to another Arduino. I'm pretty new to electronics in general so bear with me.
The way the countdown timer works is it stores the time as a String, and uses substring and toInt() to adjust the timer.
I know that with I2C you can only send some data, Strings not included, so I tried converting the String to an array of characters using toCharArray(). My conversion seems to be working well, as I have tested it with the Serial.println() and it displays the time correctly.
Therefore, I am convinced the problem lies with the Slave code. I'm not sure how to set up the Slave Arduino to receive the actual time. Below, I've pasted links to the code of both the Master and the Slave respectively.
Master: https://pastebin.com/AFjTDZB7
Slave: https://pastebin.com/viNGWn9c
How should I change my slave code so everything works?
Also, when I changed the datatype of "time" in the Slave code from char* to int, all I got in the Serial Monitor was 48. Weird.
Would really appreciate any help and guidance cause I'm pretty stuck. All the tutorials I find online are pretty complicated.
Thanks!
Master Code:
#include < Wire.h >
String time = "10:11";
boolean doubleDigits = false;
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
Serial.println(time);
sendTime(time);
int First2 = time.substring(0, 2).toInt();
if (First2 > 9) {
doubleDigits = true;
}
int Last2 = time.substring(3).toInt();
if ((time.substring(3) == "00") && (time != "00:00")) {
First2 = First2 - 1;
Last2 = 59;
if (doubleDigits == false) {
time = "0" + String(First2) + ":" + String(Last2);
} else {
time = String(First2) + ":" + String(Last2);
}
} else if (time != "00:00") {
Last2 = Last2 - 1;
if (Last2 < 10) {
if (doubleDigits == false) {
time = "0" + String(First2) + ":0" + String(Last2);
} else {
time = String(First2) + ":0" + String(Last2);
}
} else {
if (doubleDigits == false) {
time = "0" + String(First2) + ":" + String(Last2);
} else {
time = String(First2) + ":" + String(Last2);
}
}
}
if (time.substring(1, 2) == ":") {
time = "0" + time;
}
delay(1000);
}
void sendTime(String time) {
Wire.beginTransmission(9); //transmit to device 9
char buffer[32];
time.toCharArray(buffer, 32);
Wire.write(buffer);
Serial.println(buffer);
Wire.endTransmission(); //stops transmitting
}
//master
Slave Code:
#include <Wire.h>
char* time;
void setup() {
Wire.begin(9); //start as slave on address 9
Wire.onReceive(receiveEvent); //attach a function to trigger when something is received
Serial.begin(9600);
}
void receiveEvent(int bytes) {
time = Wire.read(); //read one character from the I2C
}
void loop() {
Serial.println(time);
delay(1000);
}
//slave
Edit: added code