Skip to content

14. Networking and communications

This week’s assignment was to design, build, and connect wired or wireless node(s) with network or bus addresses and local input &/or output device(s) At first I thought that I could use my previous designed board for this week, but decided create board for general use. First I made schematic and after that pcb.

Primary board code

#include <Wire.h>
const int buttonPin =3;     
int buttonState = 0;   
void setup() {
  Wire.begin();
 pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
   // digitalWrite(ledPin, HIGH);
    Wire.beginTransmission(0x9); 
    Wire.write(1);  
    Wire.endTransmission(); 
    delay(200);
    // print out the state of the button:
  //Serial.println(buttonState);
  } else {
    Wire.beginTransmission(0x9); 
    Wire.write(0);  
    Wire.endTransmission();
    delay(200);

  }
}

Secondary board code

#include <Wire.h>
#define I2C_Secondary_ADDRESS 0x9 
bool ledState; 
int led=3; 
void setup() {
  pinMode(led, OUTPUT); 
  pinMode(7, INPUT); 
  pinMode(6, INPUT); 
  Wire.begin(I2C_Secondary_ADDRESS);                
  Wire.onReceive(receiveEvent);     
}
void loop() {
  if(ledState==true){ 
    digitalWrite(led,LOW);
    delay(200); 
    digitalWrite(led,HIGH);
    delay(200);
  }else{  
    digitalWrite(led,HIGH); 
  }
  delay(100);
}
void receiveEvent(int numBytes) {
  byte recivedData=0;
  if(Wire.available()){
    recivedData=Wire.read();
    if(recivedData==1){ 
       ledState=true;      
    }else if(recivedData==0){
      ledState=false;
    }
  }
}

After that I decided connect 3 board together with I2C protocol.

I changed the code of primary board.

#include <Wire.h>
const int buttonPin =3;     
int buttonState = 0;   
void setup() {
  Wire.begin();
 pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
   // digitalWrite(ledPin, HIGH);
    Wire.beginTransmission(0x9); 
    Wire.write(1);  
    Wire.endTransmission(); 
    delay(200);
    // print out the state of the button:
  //Serial.println(buttonState);
  } else {
    Wire.beginTransmission(0x9); 
    Wire.write(0);  
    Wire.endTransmission();
    delay(200);

  }
}

Primary board code

#include <Wire.h>

const int endSwitchPin = 3;
int switchState = 0;

void setup() {
  Wire.begin(); // Initialize I2C communication
  pinMode(endSwitchPin, INPUT_PULLUP);
}

void loop() {
  switchState = digitalRead(endSwitchPin);

  if (switchState == LOW) {
    // Send command to Slave 1 (address 0x9)
    Wire.beginTransmission(0x9);
    Wire.write(1); // Command for Slave 1
    Wire.endTransmission();
    // Send command to Slave 2 (address 0x8)
    Wire.beginTransmission(0x8);
    Wire.write(1); // Command for Slave 2
    Wire.endTransmission();

  } else {
    // Turn off both slaves
    Wire.beginTransmission(0x9);
    Wire.write(0);
    Wire.endTransmission();
    Wire.beginTransmission(0x8);
    Wire.write(0);
    Wire.endTransmission();
    delay(200);
  }
}

First Secondary board (led ) code

#include <Wire.h>
#define I2C_Secondary1_ADDRESS 0x9 
bool ledState; 
int led=3; 
void setup() {
  pinMode(led, OUTPUT); 
  pinMode(7, INPUT); 
  pinMode(6, INPUT); 
  Wire.begin(I2C_Secondary1_ADDRESS);                
  Wire.onReceive(receiveEvent);     
}
void loop() {
  if(ledState==true){ 
    digitalWrite(led,LOW);
    delay(200); 
    digitalWrite(led,HIGH);
    delay(200);
  }else{  
    digitalWrite(led,HIGH); 
  }
  delay(100);
}
void receiveEvent(int numBytes) {
  byte recivedData=0;
  if(Wire.available()){
    recivedData=Wire.read();
    if(recivedData==1){ 
       ledState=true;      
    }else if(recivedData==0){
      ledState=false;
    }
  }
}

Second Secondary board (led ) code

#include <Servo_megaTinyCore.h>
#include <Wire.h>
#define I2C_Secondary2_ADDRESS 0x8
Servo motor;
bool motorState; 
int motorPin=3; 
void setup() {
  motor.attach(motorPin);
  pinMode(7, INPUT); 
  pinMode(6, INPUT); 
  Wire.begin(I2C_Secondary2_ADDRESS);                
  Wire.onReceive(receiveEvent);     
}
void loop() {
  if(motorState==true){ 
      motor.write(180);
      delay(200);
    }
  else{ 
    motor.write(90);
    delay(200);
  }
  delay(100);
}
void receiveEvent(int numBytes) {
  byte recivedData=0;
  if(Wire.available()){
    recivedData=Wire.read();
    if(recivedData==1){ 
       motorState=true;      
    }
    if(recivedData==0){
      motorState=false;
    }
  }
}