13. Networking and communications¶
Group assignment¶
For group assignment we tried I2C communication interface between our boards.
We used
- ATtiny1614 boards
- UPDI programmer with RP2040
- Joistick
- 2 LEDs
=== “Primary code”
```c++
#include <SoftwareWire.h>
const int sda=0, scl=1; // replaced sda and scl pins on board
SoftwareWire Wire(sda,scl);
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#define SW 10 // Joistick pins
#define VRY 9
#define VRX 8
int SwState = 0; // joistick state
int Xposition = 0;
int Yposition = 0;
void setup() {
Wire.begin();
Serial.begin(9600);
pinMode(sda, INPUT_PULLUP); // software level resistors for SDA SCL pins
pinMode(scl, INPUT_PULLUP);
}
void loop() {
Xposition = analogRead(VRX);
Yposition = analogRead(VRY);
SwState = digitalRead(SW);
Xposition=map(Xposition,0, 1023, -512, 512);
Yposition=map(Yposition,0, 1023, -512, 512);
Serial.print("x: ");
Serial.println(Xposition);
Serial.print("y: ");
Serial.println(Yposition);
if (Xposition !=0 || Yposition !=0) {
Wire.beginTransmission(0x9);
Wire.write(Xposition);
Wire.endTransmission();
delay(200);
}
else {
Wire.beginTransmission(0x9);
Wire.write(0);
Wire.endTransmission();
delay(200);
}
}
```
As on my board I've not provided access to default SDA SCL pins, that's why we needed to chande these pins to 0 and 1. Included the following library for replacing the pins.
[Library](../files/Week%2013/hd44780-master.zip)
ATtiny1614 has internal pullup software resistors which we activated with INPUT_PULLUP command.
=== “Secondary code”
```c++
#include <Wire.h>
#define I2C_SLAVE_ADDRESS 0x9 // given adress to secondary board
bool ledState;
int led1=1;
int led2=0;
byte recivedData=0;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(7, INPUT);
pinMode(6, INPUT);
Serial.begin(9600);
Wire.begin(I2C_SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
}
void loop() {
if((int)recivedData > 0) {
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
} else {
digitalWrite(led1, LOW);
digitalWrite(led2,HIGH);
}
}
void receiveEvent(int numBytes) {
while(Wire.available()){
recivedData=Wire.read();
Serial.println(recivedData);
}
}
```
Last update:
May 14, 2024