Background: I am trying to test the communication of 2 raspberry pi pico using i2c. Firstly, i sent a string from one and receive it from the second one. Both of them print the results in the serial monitor. I am planning to use 2 usb cables and open 2 serials to see the results. I am writing in the Arduino IDE using the Arduino-pico core.
However, when i upload a sketch, the raspberry pi disappears from my computer. If i plug it out and then plug it it, the computer doesn't recognize it. I have to reset it.
Question: I want to understand why this happens to find a work-around and avoid the same problem in the future.
Programms:
The slave code is as follows:
#include <Wire.h>
void setup() {
Serial.begin(115200);
while(!Serial)
Wire1.setSDA(4);
Wire1.setSCL(5);
Wire1.begin(0x30);
Wire1.onReceive(recv);
}
static char buff[100];
static int printNow =0;
void loop() {
if (printNow){
Serial.println("Slave Received : ");
Serial.println(buff);
printNow= 0;
}
delay(1);
}
// These are called in an **INTERRUPT CONTEXT** which means NO serial port
// access (i.e. Serial.print is illegal) and no memory allocations, etc.
//// Called when the I2C slave gets written to
void recv(int len) {
int i;
// Just stuff the sent bytes into a global the main routine can pick up and use
for (i = 0; i < len; i++) {
buff[i] = Wire1.read();
}
buff[i] = 0;
printNow = 1;
}
The master code is as follows:
#include <Wire.h>
void setup() {
Serial.begin(115200);
while(!Serial)
Wire.setSDA(4);
Wire.setSCL(5);
Wire.begin();
}
static int buff[100];
void loop() {
int i = 0;
char txt[50];
int printNow = 0;
while(Serial.available() ){
txt[i] = Serial.read(); //int
if ( txt[i] == '\n' ){
printNow = 1;
txt[i] = '\0';
}
i++;
}
i--; //reset the last increment
if (printNow){
Serial.println("Master sent this text: ");
Serial.println(txt);
}
// Write a value over I2C to the slave
Serial.println("Sending...");
Wire.beginTransmission(0x30);
Wire.write(txt, (i-1) ); // not sending the terminating 0
Wire.endTransmission();
}