Steve Massikker
Published © CC BY

The URB Unit – Making Arduinos Network

Your personal universal electronics kit. More I/O pins. More devices. More distances. Remote wireless control. No bunch wires.

IntermediateFull instructions provided30 minutes3,747
The URB Unit – Making Arduinos Network

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×2
Seeed URB 2.7 unit
×2
Servo Module (Generic)
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
Level Shifter Board (3.3V 5V)
×1
Resistor 10k ohm
Resistor 10k ohm
×2
Infrared Module (Generic)
×2

Software apps and online services

Arduino IDE
Arduino IDE
Arduino Train DEMO

Story

Read more

Schematics

Network between two and more Arduino's

Urb2 7 hackster fn3z23slhe

Code

Sketch for URB #1

Arduino
Sketch for URB #1 (Master I2C mode)
// WWW.ARDUINORAILWAYCONTROL.COM
// V.1.0  10/18/2018
// URB #1 
// Protocol V.2 & URB unit V.2.7 and higher
// Author: Steve Massikker
// --------------------------------------------

void(* resetFunc) (void) = 0; // RESET FUNCTION

#include <SoftwareSerial.h>
#include <Wire.h>

// I/O PINS

#define SENSOR_J1_PIN    9
#define SENSOR_J3_PIN    11

SoftwareSerial Bluetooth(12, 13); // RX, TX

// VARIABLES

  // SERIAL EVENT
  bool stringComplete = false;
  String inputString = "";

  // I2C
  int addressI2C;  
  byte dataToI2C = 0;


void setup() {

// Initialize Serial & I2C
  Serial.begin(9600);
  Bluetooth.begin(9600);
  Wire.begin();
  inputString.reserve(4);

// Initialize Sensors
  pinMode(SENSOR_J1_PIN, INPUT);
  pinMode(SENSOR_J3_PIN, INPUT);

}

void loop() {

// ----  PARSING INCOMING APP COMMANDS
  if (stringComplete) {

    // CONTROL FUNCTIONS
    if (inputString.charAt(0) =='a') {
      if (inputString.charAt(1) =='d') {
        if (inputString.charAt(2) =='b') {
          dataToI2C = 30;
          addressI2C = 2;
          sendDataViaI2C();
          delay(150); 
        }
        if (inputString.charAt(2) =='f') {
          dataToI2C = 31;
          addressI2C = 2;
          sendDataViaI2C();
          delay(150);           
        } 
      }  
    }  

    inputString = "";
    stringComplete = false;

  }

  // ---- COLLECT SENSORS STATE

  if (digitalRead(SENSOR_J1_PIN) == HIGH) {
    dataToI2C = 30;
    addressI2C = 2;
    sendDataViaI2C();
    delay(150);     
  }

  if (digitalRead(SENSOR_J3_PIN) == HIGH) {
    dataToI2C = 31;
    addressI2C = 2;
    sendDataViaI2C();
    delay(150); 
  }  


  bluetoothEvent();
}

// ----------- FUNCTIONS ----------- //

void serialEvent() {
  if (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == 'z') {
      stringComplete = true;
    }
  }
}

void bluetoothEvent() {
  if (Bluetooth.available()) {
    char inChar = (char)Bluetooth.read();
    inputString += inChar;
    if (inChar == 'z') {
      stringComplete = true;
    }
  }
}

void sendDataViaI2C() {
  Wire.beginTransmission(addressI2C);
  Wire.write(dataToI2C);
  Wire.endTransmission();
}

Sketch for URB #2

Arduino
Sketch for URB #2 (Slave I2C mode)
// WWW.ARDUINORAILWAYCONTROL.COM
// V.1.0  10/18/2018
// URB #2 
// Protocol V.2 & URB unit V.2.7 and higher
// Author: Steve Massikker
// --------------------------------------------

#include <Wire.h>
#include <Servo.h>

// I/O PINS

#define JUNCTION_EN 8
// Servo J1;
// Servo J2;
Servo J3;

// VARIABLES
unsigned long millisJunction;
byte dataFromI2C;

void setup() {

// Initialize I2C
  Wire.begin(2);
  Wire.onReceive(receiveI2C);

// Initialize Servos
  // J1.attach(9);
  // J2.attach(10);
  J3.attach(11); 

}

void loop() {
  // COMMAND PARSING
  if (dataFromI2C != 0) {
    switch (dataFromI2C) {

    // SERVO
    case 30: J3.write(180);
         delay(50);
         digitalWrite(JUNCTION_EN, HIGH);
         millisJunction = millis();
         break;

    case 31: J3.write(0);
         delay(50);
         digitalWrite(JUNCTION_EN, HIGH);
         millisJunction = millis();
         break;
    
    }

  dataFromI2C = 0;
  }
  
  if (millis() > (millisJunction + 800)) digitalWrite(JUNCTION_EN, LOW); // AUTO ON/OFF POWER ON SERVO
}

// ----------- FUNCTIONS ----------- //

void receiveI2C(int howMany) {
  while (Wire.available() > 0) {
    dataFromI2C = Wire.read();
  }
}

Credits

Steve Massikker

Steve Massikker

3 projects • 63 followers
I'm Steve Massikker, a graphic and visual designer and former engineer of civil aviation.

Comments

Add projectSign up / Login