Skip to content

13. Networking and communications

This week we had to:

  • Send a message between two projects

  • Document your work to the group work page and reflect on your individual page what you learned

This week for the group assignment we decided to use Elen’s board as the primary board and Shushanik’s as a secondary fee.

Connections

First we made connections between the boards via the I2C protocol.

Then a joystick was connected to the primary board, and two LEDs were connected to the secondary board.

alt text

Boards operation code

Primary board

The code on the primary board has the following logic:

When the X and Y coordinates of the joystick are non-zero, it transmits the X value to the secondary board. If both coordinates are zero, then the primary board sends the value 0.

#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);
    }
}

Since the Elen’s board does not have access to the SDA and SCL pins by default, we had to reassign them to pins 0 and 1.

The ATtiny1614 has internal pull-up resistors, which we enabled using the INPUT_PULLUP command.

Secondary board

The code on the secondary board has the following logic:

When the secondary board receives data from the main board and the data is greater than 0, the first LED lights up and the second one turns off. If the data is less than 0, then the second LED lights up and the first LED turns off.

#include <Wire.h>
#define I2C_SLAVE_ADDRESS 0x9  // the  adress of secondary board
bool ledState; 
int led1=1; 
int led2=0; 
byte recivedData=0;

void setup() {
    pinMode(led1, OUTPUT); 
    pinMode(led2, OUTPUT); 
    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);
    }
}

Result of work

Let’s look at the result of the work:


Last update: June 26, 2024