Get text input inside another function

  arduino, arduino-uno, c++, i2c

I am trying to let Raspberry pi and Arduino send and receive text via input. My problem is getting the input from arduino and sending it.

I am VERY new to C++, this is my first program in it, so I don’t know exactly how the code is reading everything. What I want to be able to do is in function requestEvents(), I want to get the text input from Serial.read() and send it via Wire.write().

What is happening is basicaly nothing it receives the first letter then just does nothing.

#include <Wire.h>
#define I2CAddress 11

void setup() {
  Serial.begin(9600);
  // Start the I2C Bus as Slave on address 11
  Wire.begin(I2CAddress);

  // Attach a function to trigger when something is received.
  Wire.onRequest(requestEvents);
  Wire.onReceive(receiveString);
}

// Read in text from RPi
void receiveString(int bytes) {
  char text;
  while (Wire.available()) {
    char ch = Wire.read();    // receive a byte as character
    Serial.print(ch);
  }

}

// Send text to RPi
void requestEvents() {

  // Get text input
  Serial.println("Enter something to send");
  while (!Serial.available()) {
    // wait for input
  }
  String text = Serial.readStringUntil(10);
  Serial.println(text);

  // Send text
  Wire.write(text);
}

void loop() {
  delay(10);
}

Source: Windows Questions C++

LEAVE A COMMENT