12. Networking and communication¶
Group assignment:¶
- Send a message between two projects
- Document your work to the group work page and reflect on your individual page what you learned
Chaining all the boards together.¶
We decided to get the boards from our assignments to talk together in one chain. Each board is a Button and an LED.
Here are the boards:
Evert’s Board: (Node 8)
Albert’s Board: (Node 2)
Magnús’ Board: (Node 3)
Magnús’ “Master” Board: (Master)
The I2C headers were oriented the same between Albert and Magnús But Evert’s board had the header rotated 90°.
The idea was to keep the code on the board as originally intended. (We did have to reflash Magnús board to change the address from 2 to 3).
We used chatGPT to program the master. Here is the full conversation. The end result was using the button on Node 3 to toggle the LEDs on nodes 2 and 8 on and off.
Here’s the final master code:
#include <Wire.h>
const byte NODE_BUTTON = 3; // Node with button (for triggering)
const byte NODE_LED = 2; // Node that blinks on master signal
const byte NODE_TOGGLE = 8; // New node that toggles LED via I2C
bool ledState = false; // Current LED state to send
int lastButtonState = 0; // Last button reading to detect changes
void setup() {
Serial.begin(115200);
Wire.begin(); // Master doesn't need an address
}
void loop() {
delay(100); // Polling delay
Wire.requestFrom(NODE_BUTTON, 1);
int buttonState = 0;
if (Wire.available()) {
buttonState = Wire.read();
}
// Detect button press event (transition from 0 to >0)
if (buttonState > 0 && lastButtonState == 0) {
ledState = !ledState; // Toggle LED state
Serial.print("Button pressed on Node 3! Toggling LEDs to: ");
Serial.println(ledState ? "ON" : "OFF");
// Send new LED state to Node 2
Wire.beginTransmission(NODE_LED);
Wire.write(ledState ? 1 : 0);
Wire.endTransmission();
// Send new LED state to Node 8
Wire.beginTransmission(NODE_TOGGLE);
Wire.write(ledState ? 1 : 0);
Wire.endTransmission();
}
lastButtonState = buttonState; // Save for next loop
}
And here’s a video showing everything working: