Notifications
Clear all

I2C Part One tutorial and Slave demo sketch for PlatformIO

1 Posts
1 Users
0 Likes
1,387 Views
(@beebot)
Member
Joined: 3 years ago
Posts: 2
Topic starter  

I have been working through the I2C tutorial and I wanted to get it to work in PlatformIO.  In doing so I am learning about forward declarations and "argument of type “void()()" is incompatible with parameter of type "void()(int)”.  I finally got the slave demo to compile correctly but it does warn me about an unused x variable.  The master demo compiled for me without any changes.  I don't pretend to completely understand what is going on, but my Uno and Nano are communicating as expected.  I thought I would share.  Thank you. 

/*
I2C Slave Demo
i2c-slave-demo.ino
Demonstrate use of I2C bus
Slave receives character from Master and responds
DroneBot Workshop 2019
https://dronebotworkshop.com
*/

#include <Arduino.h>

// Include Arduino Wire library for I2C
#include <Wire.h>

// Define Slave I2C Address
#define SLAVE_ADDR 9

// Define Slave answer size
#define ANSWERSIZE 5

// Define string with response to Master
String answer = "Hello";

// Logic for using Void receiveEvent(int); https://community.particle.io/t/argument-of-type-void-is-incompatible/58078

void receiveEvent(int);
void requestEvent();

void setup() {

// Initialize I2C communications as Slave
Wire.begin(SLAVE_ADDR);

// Function to run when data requested from master
Wire.onRequest(requestEvent);

// Function to run when data received from master
Wire.onReceive(receiveEvent);

// Setup Serial Monitor
Serial.begin(9600);
Serial.println("I2C Slave Demonstration");
}

void receiveEvent(int) {

// Read while data received
while (0 < Wire.available()) {
byte x = Wire.read();
}

// Print to Serial Monitor
Serial.println("Receive event");
}

void requestEvent() {

// Setup byte variable in the correct size
byte response[ANSWERSIZE];

// Format answer as array
for (byte i=0;i<ANSWERSIZE;i++) {
response[i] = (byte)answer.charAt(i);
}

// Send response back to Master
Wire.write(response,sizeof(response));

// Print to Serial Monitor
Serial.println("Request event");
}

void loop() {

// Time delay in loop
delay(50);
}

 


   
Quote