Howdy Ya’ll
I have a project that requires using multiple controllers together. I had theorized with my limited experience that I could program one as a controller and the other as a responder and then use the Qwiic to easily connect everything.
I tried this code to test my theory but I’m so new at this I’m not sure what I’ve done wrong.
#include <Wire.h>
#define LED_PIN 13 // Built-in LED pin
#define RESPONDER_ADDR 0x10
volatile bool isConnected = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Wire.begin(RESPONDER_ADDR); // Initialize as responder
Wire.onRequest(onRequest); // Register callback for when the controller requests data
Wire.onReceive(onReceive); // Register callback for when data is received
}
void loop() {
if (!isConnected) {
// Blink rapidly while waiting for a connection
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
} else {
// Wait for ping-pong from the controller
delay(200);
}
}
// Callback: Respond to controller’s request
void onRequest() {
Wire.write(1); // Send acknowledgment
}
// Callback: Receive data from controller
void onReceive(int numBytes) {
if (numBytes > 0) {
isConnected = true; // Connection established
digitalWrite(LED_PIN, HIGH); // Set LED steady
}
}
and this for the Controller
#include <Wire.h>
#define LED_PIN 13 // Built-in LED pin
#define RESPONDER_ADDR 0x10 // I2C address of the responder
bool isConnected = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Wire.begin(); // Initialize as controller
}
void loop() {
if (!isConnected) {
// Blink slowly while waiting for a connection
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
// Check for responder presence
Wire.beginTransmission(RESPONDER_ADDR);
if (Wire.endTransmission() == 0) {
isConnected = true; // Connection successful
digitalWrite(LED_PIN, HIGH); // Set LED steady
delay(1000); // Wait to stabilize
}
} else {
// Ping the responder and alternate the LED
digitalWrite(LED_PIN, HIGH);
delay(200);
Wire.beginTransmission(RESPONDER_ADDR);
Wire.write(1); // Send ping
Wire.endTransmission();
digitalWrite(LED_PIN, LOW);
delay(200);
// Wait for the responder's turn
Wire.requestFrom(RESPONDER_ADDR, 1);
if (Wire.available() > 0 && Wire.read() == 1) {
delay(200); // Allow ping-pong timing
}
}
}
So the lights blink at different timing until it detects a connection and then they should ping pong the light back and forth but they don’t actually connect when I have them connected with the qwiic cables, ive tried powering 1 through USB and both and they just blink and never go solid or ping pong.
This is possible yeah?