Hello
I have one atmega 328 that I want to read some data from uart and send the result thought i2c
Atmega328 run uart and i2c as slave and one atmega2560 that run only i2c as master.
My problem is on atmega328 because Ι encounters some problems to run uart and i2c at the same time.
If i run only uart or only i2c everything works perfect.
When I run the whole code after 3-5 second the cpu freeze.
Can anyone help me, please
void setup()
{
delay(500);
Serial.begin(115200);
Wire.begin(15);
Wire.setClock(400000L);
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent);
}
void loop() {
ReadCommand("Example", "Response", 1000)
}
void requestEvent() {
Sen = "<" + String(Variables[1], VariableDigit[1]) + "," + String(Variables[5], VariableDigit[5]) + "," + String(Variables[2], VariableDigit[2]) + "," + String(Variables[VariableNumber1], VariableDigit[VariableNumber1]) + "," + String(Variables[VariableNumber2], VariableDigit[VariableNumber2]) + ">" ;
Sen.toCharArray(Send, 28);
Wire.write(Send);
}
void receiveEvent(int bytes) {
str = "";
while (Wire.available()) { // loop through all but the last
str = str + char(Wire.read());
}
if (str.substring(0, 1) == "<" && str.indexOf('>') == str.length() - 1) {
int Digit1;
int Digit2;
Digit1 = str.indexOf(',');
Protocol = str.substring(1, Digit1).toInt();
Digit2 = str.indexOf(',', Digit1 + 1);
VariableNumber1 = str.substring(Digit1 + 1, Digit2).toInt();
VariableNumber2 = str.substring(Digit2 + 1).toInt();
}
}
int ReadCommand(String Command, String Answer, int WTime)
{
Serial.println(Command);
String Str = "";
byte RinData;
char RinChar;
int b = 0;
unsigned long int t = millis();
while (millis() - t < WTime) {
while (Serial.available() > 0)
{
b++;
RinData = 0;
RinChar = 0;
RinData = Serial.read();
RinChar = char(RinData);
Str = Str + RinChar;
}
if (Str.indexOf(Answer) >= 0) {
delay(200);
return 1;
}
}
delay(200);
return 0;
}