Devices & Components
Arduino Nano
Infrared Module (Generic)
Servo Module (Generic)
Level Shifter Board (3.3V 5V)
URB 2.7 unit
HC-05 Bluetooth Module
Resistor 10k ohm
Software & Tools
Arduino Train DEMO
Arduino IDE
Project description
Code
Sketch for URB #1
arduino
Sketch for URB #1 (Master I2C mode)
1// WWW.ARDUINORAILWAYCONTROL.COM 2// V.1.0 10/18/2018 3// URB #1 4// Protocol V.2 & URB unit V.2.7 and higher 5// Author: Steve Massikker 6// -------------------------------------------- 7 8void(* resetFunc) (void) = 0; // RESET FUNCTION 9 10#include <SoftwareSerial.h> 11#include <Wire.h> 12 13// I/O PINS 14 15#define SENSOR_J1_PIN 9 16#define SENSOR_J3_PIN 11 17 18SoftwareSerial Bluetooth(12, 13); // RX, TX 19 20// VARIABLES 21 22 // SERIAL EVENT 23 bool stringComplete = false; 24 String inputString = ""; 25 26 // I2C 27 int addressI2C; 28 byte dataToI2C = 0; 29 30 31void setup() { 32 33// Initialize Serial & I2C 34 Serial.begin(9600); 35 Bluetooth.begin(9600); 36 Wire.begin(); 37 inputString.reserve(4); 38 39// Initialize Sensors 40 pinMode(SENSOR_J1_PIN, INPUT); 41 pinMode(SENSOR_J3_PIN, INPUT); 42 43} 44 45void loop() { 46 47// ---- PARSING INCOMING APP COMMANDS 48 if (stringComplete) { 49 50 // CONTROL FUNCTIONS 51 if (inputString.charAt(0) =='a') { 52 if (inputString.charAt(1) =='d') { 53 if (inputString.charAt(2) =='b') { 54 dataToI2C = 30; 55 addressI2C = 2; 56 sendDataViaI2C(); 57 delay(150); 58 } 59 if (inputString.charAt(2) =='f') { 60 dataToI2C = 31; 61 addressI2C = 2; 62 sendDataViaI2C(); 63 delay(150); 64 } 65 } 66 } 67 68 inputString = ""; 69 stringComplete = false; 70 71 } 72 73 // ---- COLLECT SENSORS STATE 74 75 if (digitalRead(SENSOR_J1_PIN) == HIGH) { 76 dataToI2C = 30; 77 addressI2C = 2; 78 sendDataViaI2C(); 79 delay(150); 80 } 81 82 if (digitalRead(SENSOR_J3_PIN) == HIGH) { 83 dataToI2C = 31; 84 addressI2C = 2; 85 sendDataViaI2C(); 86 delay(150); 87 } 88 89 90 bluetoothEvent(); 91} 92 93// ----------- FUNCTIONS ----------- // 94 95void serialEvent() { 96 if (Serial.available()) { 97 char inChar = (char)Serial.read(); 98 inputString += inChar; 99 if (inChar == 'z') { 100 stringComplete = true; 101 } 102 } 103} 104 105void bluetoothEvent() { 106 if (Bluetooth.available()) { 107 char inChar = (char)Bluetooth.read(); 108 inputString += inChar; 109 if (inChar == 'z') { 110 stringComplete = true; 111 } 112 } 113} 114 115void sendDataViaI2C() { 116 Wire.beginTransmission(addressI2C); 117 Wire.write(dataToI2C); 118 Wire.endTransmission(); 119}
Sketch for URB #2
arduino
Sketch for URB #2 (Slave I2C mode)
1// WWW.ARDUINORAILWAYCONTROL.COM 2// V.1.0 10/18/2018 3// URB #2 4// Protocol V.2 & URB unit V.2.7 and higher 5// Author: Steve Massikker 6// -------------------------------------------- 7 8#include <Wire.h> 9#include <Servo.h> 10 11// I/O PINS 12 13#define JUNCTION_EN 8 14// Servo J1; 15// Servo J2; 16Servo J3; 17 18// VARIABLES 19unsigned long millisJunction; 20byte dataFromI2C; 21 22void setup() { 23 24// Initialize I2C 25 Wire.begin(2); 26 Wire.onReceive(receiveI2C); 27 28// Initialize Servos 29 // J1.attach(9); 30 // J2.attach(10); 31 J3.attach(11); 32 33} 34 35void loop() { 36 // COMMAND PARSING 37 if (dataFromI2C != 0) { 38 switch (dataFromI2C) { 39 40 // SERVO 41 case 30: J3.write(180); 42 delay(50); 43 digitalWrite(JUNCTION_EN, HIGH); 44 millisJunction = millis(); 45 break; 46 47 case 31: J3.write(0); 48 delay(50); 49 digitalWrite(JUNCTION_EN, HIGH); 50 millisJunction = millis(); 51 break; 52 53 } 54 55 dataFromI2C = 0; 56 } 57 58 if (millis() > (millisJunction + 800)) digitalWrite(JUNCTION_EN, LOW); // AUTO ON/OFF POWER ON SERVO 59} 60 61// ----------- FUNCTIONS ----------- // 62 63void receiveI2C(int howMany) { 64 while (Wire.available() > 0) { 65 dataFromI2C = Wire.read(); 66 } 67}
Sketch for URB #2
arduino
Sketch for URB #2 (Slave I2C mode)
1// WWW.ARDUINORAILWAYCONTROL.COM 2// V.1.0 10/18/2018 3// URB #2 4// Protocol V.2 & URB unit V.2.7 and higher 5// Author: Steve Massikker 6// -------------------------------------------- 7 8#include <Wire.h> 9#include <Servo.h> 10 11// I/O PINS 12 13#define JUNCTION_EN 8 14// Servo J1; 15// Servo J2; 16Servo J3; 17 18// VARIABLES 19unsigned long millisJunction; 20byte dataFromI2C; 21 22void setup() { 23 24// Initialize I2C 25 Wire.begin(2); 26 Wire.onReceive(receiveI2C); 27 28// Initialize Servos 29 // J1.attach(9); 30 // J2.attach(10); 31 J3.attach(11); 32 33} 34 35void loop() { 36 // COMMAND PARSING 37 if (dataFromI2C != 0) { 38 switch (dataFromI2C) { 39 40 // SERVO 41 case 30: J3.write(180); 42 delay(50); 43 digitalWrite(JUNCTION_EN, HIGH); 44 millisJunction = millis(); 45 break; 46 47 case 31: J3.write(0); 48 delay(50); 49 digitalWrite(JUNCTION_EN, HIGH); 50 millisJunction = millis(); 51 break; 52 53 } 54 55 dataFromI2C = 0; 56 } 57 58 if (millis() > (millisJunction + 800)) digitalWrite(JUNCTION_EN, LOW); // AUTO ON/OFF POWER ON SERVO 59} 60 61// ----------- FUNCTIONS ----------- // 62 63void receiveI2C(int howMany) { 64 while (Wire.available() > 0) { 65 dataFromI2C = Wire.read(); 66 } 67}
Downloadable files
Network between two and more Arduino's
Network between two and more Arduino's

Network between two and more Arduino's
Network between two and more Arduino's

Comments
Only logged in users can leave comments