Hello everybody,
Hope you are doing well,
I have a question about how to split a string,
I'm using Arduino and ESP to send data from Arduino (see the wiring Figure 1).
I want to separate the numbers 1234567890 one by one i.e. 1 2 3 4 .....
Is there an idea to do this?
Thankyou
#include <Wire.h>
void setup() {
Wire.begin(8); /* join i2c bus with address 8 */
Wire.onReceive(receiveEvent); /* register receive event */
Wire.onRequest(requestEvent); /* register request event */
Serial.begin(9600); /* start serial for debug */
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
void receiveEvent(int howMany) {
while (0 <Wire.available()) {
char c = Wire.read(); /* receive byte as a character */
Serial.print(c); /* print the character */
}
Serial.println(); /* to newline */
}
// function that executes whenever data is requested from master
void requestEvent() {
Wire.write("1234567890"); /*send string on request */
}
And the ESP code is :
#include <Wire.h>
void setup() {
Serial.begin(9600); /* begin serial for debug */
Wire.begin(D1, D2); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}
void loop() {
Wire.beginTransmission(8); /* begin with device address 8 */
Wire.requestFrom(8, 13); /* request & read data of size 13 from slave */
while(Wire.available()){
char c = Wire.read();
Serial.print(c);
}
Serial.println();
delay(1000);
}
Why do you need the number split?
If you want each character separated by a space or newline, then print the space or newline after each received character.
First I developed a code in fuzzy logic and it worked well in Arduino, when I tested the same code on ESP it didn't work, so I thought maybe it's good to use arduino for the processing and an ESP for the sending.