#include <Wire.h>
String shape_sequence;
void setup()
{
Wire.begin(1); // join i2c bus with address #1
Wire.onReceive(receiveEvent); // register event
Serial.begin(115200);
while (!Serial); // Needed for native USB port only,
// wait for serial port to connect.
delay(500); // Necessary for clean Serial Monitor print
Serial.print("this");
}
void loop()
{
}
void receiveEvent()
{
shape_sequence = Wire.read();
Serial.println(shape_sequence);
}
I am having trouble sending the string as chars. Any simple fixes?
The idea is to put numbers in an array for the slave Arduino device to execute. And so "1" will execute one thing, "2" will execute another and so forth.
This was my attempt to re-create a larger program with minimal code.
A string of varying length is sent to the master Arduino device. Then, I want that to be sent via I2C. The original (large) program does not have any scope errors.
#include <Wire.h>
String shape_sequence;
void setup()
{
Wire.begin(1); // join i2c bus with address #1
Wire.onReceive(receiveEvent); // register event
Serial.begin(115200);
while (!Serial); // Needed for native USB port only,
// wait for serial port to connect.
delay(500); // Necessary for clean Serial Monitor print
Serial.print("this");
}
void loop()
{
}
void receiveEvent()
{
shape_sequence = Wire.read();
Serial.println(shape_sequence);
}
J-M-L:
Put this aside for the moment and read about cString and arrays in C++
"Use string. cstring is so 1970's. string is a modern way to represent strings in c++. you'll need to learn cstring because you will run into code that uses it. but cstring is responsible for lots of unsafe code."
I think the issue is in the receiving or slave program, but there are multiple things that could be wrong anywhere:
How is the length of the string found? I tried with .length() and strlen(). Always amazed by the lack of examples about this basic stuff. I mean, spent days now just trying to send any string using I2C!
I2Cslave
#include <Wire.h>
String shape_squence = "";
void setup()
{
Wire.begin(1); // join i2c bus with address #1
Wire.onReceive(receiveEvent); // register event
Serial.begin(115200);
while (!Serial); // Needed for native USB port only,
// wait for serial port to connect.
delay(500); // Necessary for clean Serial Monitor print
Serial.print("This");
}
void loop()
{
}
void receiveEvent()
{
shape_sequence += (char)Wire.read();
Serial.println(shape_sequence);
// Serial.println("Event - Recieve");
// while(Wire.available() > 0) // loop through all but the last
// {
// char c = Wire.read(); // receive byte as a character
// shape_squence.concat(c);
// delay(10);
// Serial.print("Data in - "); // print the character
// }
}
Error: "'shape_sequence' was not declared in this scope"
@adamelli, the ESP32 can not have signals with 5V on its pins. That means you may not connect a ESP32 with a Uno or Nano. Either I2C or UART. A level shifter will solve that, but a level shifter will also make the signal a little weaker.