This is the warning:
warning: invalid conversion from 'void ()(int)' to 'void ()()' [-fpermissive] Wire.onRequest(requestEvent);
I am working on an I2C communications between a Raspberry Pi and an Arduino, however, whenever I run it I get the above warning. The only thing I am trying to do is to just send numbers to the Raspberry Pi whenever needed. This code is just a simplified version of that.
Now I'm not exactly sure as to why the error is even appearing, I have tried to use integers and integer set variables, e.g. using Wire.write(99) vs Wire.write(foo) where foo = int 99.
I'll attach relevant pieces of code:
void setup() {
// Join I2C bus as slave with address 8
Wire.begin(0x8);
// Call receiveEvent when data received and requestEvent when data is to be sent
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
//pinMode definitons for H bridge to be possibly used for later.
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENA, OUTPUT);
Serial.begin(9600);
}
void requestEvent(int msg) {
//int sensorValue = analogRead(A0); // Data from potentiometer to be sent to Pi as a Test
//Wire.write(sensorValue);
//Wire.write(msg);
Wire.write(1);
}
Really, you should post all of the code as we need to know the variable data types, at least.
Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.
#include <Wire.h>
// Definition of H Bridge, LED pins and integers
#define ENA 2
#define IN1 3
#define IN2 4
#define ledPin 13
String received_str = "";
void setup() {
// Join I2C bus as slave with address 8
Wire.begin(0x8);
// Call receiveEvent when data received and requestEvent when data is to be sent
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
//pinMode definitons for H bridge to be possibly used for later.
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENA, OUTPUT);
Serial.begin(9600);
}
// Function that executes whenever data is received from master
void receiveEvent(int howMany) {
while (Wire.available()) { // loop through all but the last
// receive byte as a character, char converts unicode to character and add these characters into string
received_str += (char)Wire.read();
}
}
// Function that sends data to Master (Pi). Unlike receiveEvent, the Wire.available confirmation loop cannot be used as it stops data being sent.
void requestEvent(int msg) {
//int sensorValue = analogRead(A0); // Data from potentiometer to be sent to Pi as a Test
Wire.write(msg);
}
void loop() {
delay(100);
/*
if (Wire.available()) {
requestEvent(1);
flag = true;
}
requestEvent(2);
*/
int sensorValue = analogRead(A0);
//Serial.println(sensorValue);
int n = received_str.toInt();
if (n > 0) { //Data Conversion utilised in a sense that when n is at 0, meaning that it is a character, does not get outputted, else print the number and reset.
Serial.println(received_str);
received_str = "";
requestEvent(sensorValue);
} else {
received_str = "";
}
}
You guys are correct in saying that it should just be void requestEvent() { but I need the function to be called elsewhere in the code to send a specific number over i2c. You could say that Wire.write() can be used but that also does not work because the onRequest function needs to trigger the communication over i2c.
So i'm just stuck here because I cannot for some reason use the function parameters to send over numbers, and I have been sitting at it for some time now, trying to figure out how I can send numbers to Pi without this error appearing and Python printing "4" (Related to this issue since doing requestEvent without parameter works really well)
F:\Users\c\Desktop\University Work\Third Year\ACE Project\I2CTest\I2CTest.ino: In function 'void setup()':
F:\Users\c\Desktop\University Work\Third Year\ACE Project\I2CTest\I2CTest.ino:31:30: warning: invalid conversion from 'void (*)(int)' to 'void (*)()' [-fpermissive]
Wire.onRequest(requestEvent);
^
In file included from F:\Users\c\Desktop\University Work\Third Year\ACE Project\I2CTest\I2CTest.ino:12:0:
C:\Users\j\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.4\libraries\Wire\src/Wire.h:77:10: note: initializing argument 1 of 'void TwoWire::onRequest(void (*)())'
void onRequest( void (*)(void) );
^~~~~~~~~
Sketch uses 4884 bytes (15%) of program storage space. Maximum is 32256 bytes.
Global variables use 390 bytes (19%) of dynamic memory, leaving 1658 bytes for local variables. Maximum is 2048 bytes.
Using the I2C bus to communicate between an Arduino board and a Raspberry Pi is a bad idea.
You have not told us which Arduino board and which Raspberry Pi you have.
If you connect a Arduino board with a USB cable to a Raspberry Pi, then you can use Serial communication.
For the I2C bus, there are about 20 things that you have to do just right. You have to know everything about voltage levels when using a 5V Arduino board; Pullup resistors; Cable length; Keeping SDA away from SCL; Using packages of data, not a stream of data; Keeping interrupt routines very short (no heap allocation as you do now with the String class).
Even if you do everything just right, then the Arduino board can not decide to send data to the Raspberry Pi. The Raspberry Pi can request data.
If you want to continue with the I2C bus, then this topic might get to 100 posts long and then it might still not work.
Turns out I just had to use the function as is, without any parameter and write a random variable. Meaning I could just sent a string or int, change it and it would be written whenever changed.
Communication is possible and nothing impossible to be made of, as I am just using it to transmit numbers, nothing complicated can come out of this.