Things used in this project

Hardware components:
Ard nano
Arduino Nano R3
×1
R8326274 01
Raspberry Pi 2 Model B
×1
Ultrasonic Distance Sensor (HC-SR04)
×1
Servo (generic)
×1
Resistor (1K)
×3
Resistor (2K)
×3
11026 02
Jumper wires (generic)
×1
12002 04
Breadboard (generic)
×1
Software apps and online services:
Ide web
Arduino IDE
Vs2015logo
Microsoft Visual Studio 2015
10
Microsoft Windows 10 IoT Core

Schematics

Schematic (Fritzing)
Windwows IoT - UltraSonic Distance Mapper-Anurag-PC.fzz
Schematic
Windwows%20iot%20 %20ultrasonic%20distance%20mapper bb

Code

Gateway (Arduino Nano)C/C++
/*
  Gateway sketch for UltraSonic Distance Mapper (RPi2 + WinIoT)
  https://www.hackster.io/AnuragVasanwala/windows-10-iot-core-ultrasonic-distance-mapper-d94d63

  Objectives:
    + Initialize Servo
    + Initialize I2C Slave in address 0x40 (64 DEC)
    + Move servo to specified position when master (WinIoT) sends position byte
*/
#include <Wire.h>
#include <Servo.h> 

#define MyAddress 0x40
#define Pin_Servo 3

/* Create object for servo motor */
Servo MyServo;

/* Initial servo position */
byte Position = 70;

void setup()
{
  /* Initialize servo and move to initial position */
  MyServo.attach(Pin_Servo);
  MyServo.write(Position);

  /* Initialize I2C Slave & assign call-back function 'onReceive' */
  Wire.begin(MyAddress);
  Wire.onReceive(I2CReceived);
}

void loop(){ /* Do nothing. Just wait for call-back 'onReceive' */ }

/* This function will automatically be called when RPi2 sends data to this I2C slave */
void I2CReceived(int NumberOfBytes)
{
  /* WinIoT have sent position byte; read it */
  Position = Wire.read();

  /* Move servo to specified the position */
  MyServo.write(Position);
}
Universal Windows App (RPi2)
Improved Code (by Dave Glover)
Profile Ref: https://www.hackster.io/glovebox

Credits

Asv2
Anurag S. Vasanwala
8 projects • 345 followers
Contact
Thanks to Jerry Nixon.

Comments