NFC (Near Field Communications)

If you would like to add some security to your Arduino projects, NFC or Near Field Communications is a low cost solution. We purchased a NFC module and card for under $5. This module is also supported by Raspberry Pi Python libraries.

To get things started you will need to load that Arduino MFRC522 library. You can do this by calling up the Arduino Library Manager, then search for 522.

mfrc522_lib

The default circuit wiring should be:

Uno_RFID_wiring

NFC tags can contain information besides just their tag ID number.

We modified one of the library examples, to include a check based on some “good” tag IDs. The code is below:

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

#define NUMCARDS 2
int gooduid[NUMCARDS][10] = {
  {0xD5, 0xF6, 0xA6, 0xA5, 0x0, 0x0, 0x0, 0x0, 0x0},
  {0x13, 0x2F, 0x4E, 0xE5, 0x0, 0x0, 0x0, 0x0, 0x0}
  };

void setup() {
	Serial.begin(9600);		// Initialize serial communications with the PC

	SPI.begin();			// Init SPI bus
	mfrc522.PCD_Init();		// Init MFRC522
	mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details
	Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop() {
	// Look for new cards
	if ( ! mfrc522.PICC_IsNewCardPresent()) {
		return;
	}

	// Select one of the cards
	if ( ! mfrc522.PICC_ReadCardSerial()) {
		return;
	}

	// Dump debug info about the card; PICC_HaltA() is automatically called
	mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

  // Reset the valid card checks
  bool anyok = false;
  bool cardok[NUMCARDS];
  for (int i ;  i < NUMCARDS; i++ ) {cardok[i] = false; }
  // Check the card ID   
  for (int j=0; j< NUMCARDS; j++) { 
    for (int i=0; i<mfrc522.uid.size; i++) {
      if (mfrc522.uid.uidByte[i] == gooduid[j][i]) {
        cardok[j] = true;
      } else {
        cardok[j] = false;
        break;
      }
    }
  }
  // Check status of card check
  for (int i=0 ;  i < NUMCARDS; i++ ) {
    if (cardok[i] == true) {
      anyok = true;
      break;
    }
  }
// Print the card check status  
  if (anyok == true) { 
    Serial.println ("Good Card -- do some action");
  } else {
    Serial.println ("Invalid Card");
  }
  
}

When the Arduino monitor is running the output should look something like:

nfc_output

Once we found our “good” tag Id we added a reference to it in our code:

int gooduid[NUMCARDS][10] = { {0xD5, 0xF6, 0xA6, 0xA5, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x13, 0x2F, 0x4E, 0xE5, 0x0, 0x0, 0x0, 0x0, 0x0} };

Once the basic code and setup is working there are a lot of potential projects to work on.  Below is a example where we used a PowerSwitch Tail II with an NFC tag to control a power connection.

nfc_2_power

Tow Truck

The goal for our tow truck was to have a 4-axis crane and a movable vehicle that we could remotely control with an Android smart phone.

tow_truck2

The parts we used for this project were:

Hardware Setup

The tow truck project used a camera mount for up/down/left/right crane motion and an Arduino car chassis for mobility. The controls were done using Bluetooth.

Meccano was used to build a box for the main structure. Wire was used to secure everything together. We laid a folded piece of paper under the Arduino Mega to ensure that none of the Arduino solder connections shorted on the metal Meccano base.

tow_truck_inside

The motor and servo shield that we used did not expose any of the extra Arduino pins, so we needed to use the Mega board. We then wired the Bluetooth module to the exposed pins on the end of the Mega.

tow_truck_circuit

Arduino Code

The Arduino code will vary a little based on the motor/servo shield that is used. Our shield was an older version 1 (V1) board that used direct pin connections (no I2C or SDA/SCL connections).

Also because Tx/Rx (Tx0/Rx)) were not available once our motor/servo shield was installed we used Tx1/Rx1 and so our Bluetooth connection was on Serial1 and not Serial.

For the Bluetooth communications we used the following command letters:

  • R = drive right
  • L = drive left
  • f = drive forwards
  • b = drive backwards
  • s = stop driving
  • r = move crane right
  • l = move crane left
  • u= move crane up
  • d = move crane down

Our Arduino code is below:

#include <Servo.h> 

Servo servo1;
Servo servo2;

char thecmd;
int xpos = 90;
int ypos = 90;

AF_DCMotor motor1(1); 
AF_DCMotor motor2(2);

void setup() {
  pinMode( 19, INPUT_PULLUP );
  Serial1.begin(9600);
  Serial1.println("Crane Controls");
  Serial1.println("r = right, l = left, u= up, d = down");
  Serial1.println("Driving Controls");
  Serial1.println("R = right, L = left, f = forwards, b = backwards, s = stop");
  servo1.attach(9); // attaches the servo on pin 9 to the servo object 
  servo2.attach(10); // attaches the servo on pin 9 to the servo object 
  
  servo1.write(xpos);
  servo2.write(ypos);

  motor1.setSpeed(255);
  motor2.setSpeed(255);

}
void loop() {
  
  if (Serial1.available() > 0) {
        // read the incoming byte: 
       thecmd = Serial1.read();
       Serial1.println(thecmd);
       if (thecmd =='l') { move_crane(servo1, 5); }
       if (thecmd =='r') { move_crane(servo1, -5); }
       if (thecmd =='d') { move_crane(servo2, 5); }
       if (thecmd =='u') { move_crane(servo2, -5); }
       if (thecmd =='f') { 
            motor1.run(FORWARD); 
            motor2.run(FORWARD); 
       }
        if (thecmd =='b') { 
            motor1.run(BACKWARD); 
            motor2.run(BACKWARD); 
       }
       if (thecmd =='L') { 
           motor1.run(BACKWARD); 
           motor2.run(FORWARD); 
       }
       if (thecmd =='R') { 
            motor1.run(FORWARD); 
            motor2.run(BACKWARD); 
       }
       if (thecmd =='s') { 
           motor1.run(RELEASE); 
           motor2.run(RELEASE); 
       }
  }
}

void move_crane(Servo theservo, int direction) {
  int minpos = 50;
  int maxpos = 220;
  if (direction < 0) {
    if (ypos > minpos) {
      ypos = ypos + direction;
      theservo.write(ypos);
    }
  }
  else {
    if (ypos < maxpos) {
      ypos = ypos + direction;
      theservo.write(ypos); 
    }   
  }
}

Android Program

To communication to an Android smart phone we used MIT’s App inventor. This is a free Web based Android development tool.

There are many ways to layout a control screen, for us we used a 10×3 table and then populated it with buttons. Our layout is shown below:

ai_layout

The button logic will pass the required letter command to the Bluetooth component:

ai_logic

Our final running App looked like:

Screenshot_tow_truck

Arduino Crane

There are a lot of fun ways to build cranes. For this project we used:

  • 1 – Arduino Mega (Uno or equivalent could also be used).
  • 1 – generic motor/servo shield
  • 1 – breadboard
  • 1 – Webcam mount (with 2 servos)
  • 4 buttons and 4 resistors (10KΩ  whatever you have).
  • jumpers
  • K’Nex pieces
  • String or wire to support the servos to the structure

We used string and wire to secure the Webcam mount to our structure.

crane2

For the servos that we used we found that they had “jitter” if they were connected directly to the Arduino. Depending on the servos that you use you may or may not experience “jitter”. To remove “jitter”  a motor/servo shield can be used, these shields have from filtering to remove  noise on the power and data pins to the servos.

There is no difference in the Arduino code if you are or are not using a servo shield. Typically a servo shield will use data pins 9 and 10.

For this project we used 4 buttons to control the 2 servos. The circuit that we used is shown below.

circuit0

The code is:

// 4 button crane project
//
#include 

Servo servo1;
Servo servo2;

char thecmd;
int xpos = 90;
int ypos = 90;

int uPin = 31;
int dPin = 33;
int lPin = 35;
int rPin = 37;

void setup() {
Serial.begin(9600);
Serial.println("Crane Controls");
Serial.println("Push a button to control...");
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo2.attach(10); // attaches the servo on pin 9 to the servo object

servo1.write(xpos); // left and right servo
servo2.write(ypos); //up and down servo

pinMode(uPin, INPUT);
pinMode(dPin, INPUT);
//pinMode(rPin, INPUT);
//pinMode(lPin, INPUT);
}
void loop() {

if (digitalRead(uPin) == HIGH) { move_crane(servo2, 5); }
if (digitalRead(dPin) == HIGH) { move_crane(servo2, -5); }
if (digitalRead(lPin) == HIGH) { move_crane(servo1, 5); }
if (digitalRead(rPin) == HIGH) { move_crane(servo1, -5); }
}

void move_crane(Servo theservo, int direction) {
Serial.println("Button pushed");
int minpos = 50;
int maxpos = 220;
if (direction  minpos) {
ypos = ypos + direction;
theservo.write(ypos);
}
}
else {
if (ypos < maxpos) {
ypos = ypos + direction;
theservo.write(ypos);
}
}
delay(500);
}

Zen Waterfalls

We created a zen waterfalls that:

  • Had water flowing over the rocks
  • Had an MP3 music player
  • Had a rotating light show

Part of the fun on this project was that we could play with different rotating light patterns that could be used in either normal lighting or in a dark room.

OLYMPUS DIGITAL CAMERA

For this project we tried it with a Raspberry Pi, littleBits and Arduino.  We found that the Arduino offered the best overall solution.

The parts that we used were:

We placed the rocks into the large container and then we mounted the water pump in one of the back corners. It will take a bit of trial and error to ensure that you keep the water flowing down the rocks and not shooting out everywhere. We put a rock on the very top to secure the plastic tube and to block the water spray so that the water flows “quietly” down the lower rocks.

OLYMPUS DIGITAL CAMERA

The motor shield was stacked on top of the UNO and the protyping shield was used for mounting the IR receiver and serial MP3 player.

OLYMPUS DIGITAL CAMERA

The first important step is to read your IR codes. Each manufacturers TV remote will give different IR codes. Below is some simple code that will show the IR code for each button press. We used this code and then we recorded the button codes that we wanted to use.

/*

IR TEST PROGRAM

PINOUTS:

lEFT = DATA PIN
MIDDLE = gnd
RIGHT = 3.3 volts

*/

#include 

int RECV_PIN = 11; //11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
Serial.println("Setup Complete");
}

void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
delay(500);

}
}

Once we got our IR button codes we were able to pull everything together. For our project we wanted the following buttons:

  • Pump ON
  • Pump OFF
  • Lights ON
  • Lights OFF
  • Play music
  • Stop music
  • Volume UP
  • Volume DOWN

Below is our final code:

//Arduino PWM Speed Control

// Neopixel setup
#include 
#define PIN 8
Adafruit_NeoPixel strip = Adafruit_NeoPixel(100, PIN, NEO_GRB + NEO_KHZ800);
bool leds_on = false;
int thecolor = 0;
int thepixel = 0;

// IR setup
#include 
int RECV_PIN = 11; //11;
IRrecv irrecv(RECV_PIN);
decode_results results;

// Pump setup
char thekey;
int E1 = 5; // motor speed pin
int M1 = 4; // motor enable pin

// Music Setup
#include 
static int8_t Send_buf[8] = {0} ;
#define CMD_VOLUME_UP 0X04
#define CMD_VOLUME_DOWN 0X05
#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY 0X0D
#define CMD_PLAY_NEXT 0X01
#define CMD_PAUSE 0X0E
#define CMD_FOLDER_CYCLE 0X17

#define ARDUINO_RX 5//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);

//-----------------------------------------------------
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the IR receiver

// Enable the Neopixel strip
strip.begin();
strip.setBrightness(20);

// Setup the pump
pinMode(M1, OUTPUT);
digitalWrite(M1, HIGH);
analogWrite(E1, 0); //Set pump speed to zero (off)

// Setup the music
mySerial.begin(9600);
delay(500);//Wait chip initialization is complete
sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
delay(200);//wait for 200ms
sendCommand(CMD_FOLDER_CYCLE, 0X0001);
}
//-----------------------------------------------------
void loop()
{
if (irrecv.decode(&results)) {
switch (results.value) {
case 0xDAEA83EC: //Stop Pump 'Red Button'
Serial.println("Stop Pump");
analogWrite(E1, 0);
break;

case 0x2BAFCEEC: //Start Pump 'Green Button'
Serial.println("Run Pump");
analogWrite(E1, 255);
break;

case 0xB5210DA6: //LEDs ON 'Yellow Button'
Serial.println("Turn LEDs ON");
leds_on = true;
break;

case 0x71A1FE88: //LEDs ON 'Yellow Button'
Serial.println("Turn LEDs OFF");
leds_on = false;
color_off();
break;
case 0x68733A46: //Volume Up
sendCommand(CMD_VOLUME_UP, 0);
Serial.println("Turning Volume Up");
break;

case 0x83B19366: //Volume Down
sendCommand(CMD_VOLUME_DOWN, 0);
Serial.println("Turning Volume Down");
break;

case 0x6A618E02: //Play
//sendCommand(CMD_PLAY_NEXT, 0);
sendCommand(CMD_PLAY,0);
Serial.println("Play Music");
break;

case 0xED45D744: //Play Next
sendCommand(CMD_PLAY_NEXT, 0);
Serial.println("Play Next SongMusic");
break;

case 0xC863D6C8: //Stop
sendCommand(CMD_PAUSE, 0);
Serial.println("Pause Music");
delay(1000); // delay so that you don't jump ahead 2+ songs
break;

default:
Serial.println(results.value, HEX);
}
irrecv.resume(); // Receive the next value
}
// Do colors
if (leds_on) { color_on(); }

}
//----------------------------------------------------
void color_on()
{
thecolor = thecolor + 5;
if (thecolor > 255) { thecolor = 0;}
for (int i=0; i  strip.numPixels()) {thepixel = 0;}
strip.setPixelColor(thepixel, 238, 201, 0);
strip.setPixelColor((thepixel+1), 205, 173, 0);
strip.setPixelColor((thepixel+2), 238, 201, 0);
strip.show();
delay (100);
}
//----------------------------------------------------
void color_off()
{
for (int i=0; i > 8);//datah
Send_buf[6] = (int8_t)(dat); //datal
Send_buf[7] = 0xef; //ending byte
for(uint8_t i=0; i<8; i++)//
{
mySerial.write(Send_buf[i]) ;
}
}

 

 

Bluetooth Controlled Arduino Airboat

By using plastic bottles, K’Nex and some duct tape we built a boat frame. The fans on the Arduino module are controlled with Bluetooth from a phone.

OLYMPUS DIGITAL CAMERA
Airboat making a Left Turn

For this project the electrical parts that you need are:

– Arduino Uno
– 3x Fan Modules for Arduino ($6 each)
– 6x AA Battery Case w/ Power Plug ($4)
– Prototype Shield w/ Breadboard ($5)
JY-MCU Bluetooth Module ($7)

The fans are powered directly from the Arduino 5V pins, so no extra batteries are needed. With 3 fan modules the airboat moves quite well, 2 fans will also work. The fans will spin in either direction depending on the inputs used (INA or INB).

For flotation we used two medium sized plastic bottles, and for the frame we use K’Nex pieces. To connect the K’Nex frame to the bottles duct tape works well. The fans can be attached to the frame with wire, string or bolts and screws. To protect the Arduino some Tupperware can be taped to the middle of the frame.

boat_frame2

For the JY-MCU Bluetooth Module, you need to cross the TX and RX pins. TX on the module goes to RX on the Arduino, and RX on the module goes to TX on Arduino.

arduino-bluetooth

To connect your phone to the Arduino project your phone will need a Bluetooth Terminal program. For our Android phone we use the “Bluetooth Terminal” from Qwerty it’s free and easy to use.

You will also need to pair your phone with the JY-MCU Bluetooth Module. When you scan for Bluetooth devices the Arduino Bluetooth module will probably be called HC-06 .The pairing code will be: 1234 .

To control the airboat we use the following letters:
g – go forward
s – stop
l – left turn
r – right turn

phone_test

For the wiring of the fans we use pins 6 to 11. The Arduino and the battery pack will need to be balanced in the tupperware, otherwise the fans might touch the water.

The final code that we used is below.

// Airboat with 3 fans controlled by a bluetooth phone

int INA1 = 6; // back fan
int INB1 = 7; // back fan
int INA2 = 8; // right side fan
int INB2 = 9; // right side fan
int INA3 = 10; // left side fan
int INB3 = 11; // left side fan

char thekey; // input from Bluetooth phone

void setup()
{
// initialize the serial communication:
Serial.begin(9600); //baud rate of Bluetooth Module
// define the pins of the 3 fans as outputs
pinMode(INA1,OUTPUT);
pinMode(INB1,OUTPUT);
pinMode(INA2,OUTPUT);
pinMode(INB2,OUTPUT);
pinMode(INA3,OUTPUT);
pinMode(INB3,OUTPUT);
// start with all fans turned off
digitalWrite(INA1,LOW);
digitalWrite(INB1,LOW);
digitalWrite(INA2,LOW);
digitalWrite(INB2,LOW);
digitalWrite(INA3,LOW);
digitalWrite(INB3,LOW);
}

void loop() {

if (Serial.available() > 0) {
thekey = Serial.read(); // get the key from the phone

// "s" stops all fans
if (thekey == 's') {
Serial.println("Fans are stopped");
digitalWrite(INB1,LOW);
digitalWrite(INB2,LOW);
digitalWrite(INB3,LOW);
delay(1500);
}
// "g" runs all fans
if (thekey == 'g') {
Serial.println("Fans are going");
digitalWrite(INB1,HIGH);
digitalWrite(INB2,HIGH);
digitalWrite(INB3,HIGH);
}
// "l" only run right fan, turn left
if (thekey == 'l') {
Serial.println("Turn left");
digitalWrite(INB1,LOW);
digitalWrite(INB2,HIGH);
digitalWrite(INB3,LOW);
}
// "r" only run left fan, turn right
if (thekey == 'r') {
Serial.println("Turn right");
digitalWrite(INB1,LOW);
digitalWrite(INB2,LOW);
digitalWrite(INB3,HIGH);
}
}
}

 

Arduino Sailboat

To build the sailboat we used

  • Arduino Uno [1]
  • Prototype shield with bread board ($5)
  • Wireless RF module and remote ($6)
  • Small servo ($6)
  • 6x AA batteries with case and plug
  • K’Nex building pieces
  • 4 plastic bottles
  • 1 small plastic container with a lid
  • String, Duct tape, and a garbage bag (white if you have it)

The boat construction used the 4 bottles for flotation and a K’Nex frame.

structure

Mounting the rudder will probably require some trial and error, our design used some Lego pieces, duct tape and wire to hold it in place.

servo1

For the electronics we had some good success with a wireless RF (Radio Frequency) module. The wireless RF modules are relatively low cost ($6), and they do not require any fancy programming. remote1

wiring

The final Arduino code is :

//
// sailboat1.ino - use a wireless RF motor to move a servo on a sailboat
// RF buttons:
// A = straight (90deg), B = turn left (10deg), C = turn right (10deg)
// Limit rudder turning from 60-120 degrees
//
#include <Servo.h> 
 
Servo myservo; 
int pos;

void setup() {
 pinMode(5, INPUT); // A button - D0 pin on RF module
 pinMode(6, INPUT); // B button - D1 pin on RF module 
 pinMode(7, INPUT); // C button - D2 pin on RF module 
 pinMode(8, INPUT); // D button - D3 pin on RF module 


void loop(){

if (digitalRead(5) == HIGH) { 
 Serial.println("Button A");
 pos = pos + 10;
 if (pos > 120) { pos = 120;} 
 myservo.write(pos); 
 delay(250);
 } 
 if (digitalRead(6) == HIGH) { 
 Serial.println("Button B"); 
 pos = 90;
 myservo.write(pos); 
 delay(250);
 } 
 if (digitalRead(7) == HIGH) { 
 Serial.println("Button C");
 pos = pos - 10;
 if (pos < 60) { pos = 60;} 
 myservo.write(pos); 
 delay(250); 
 } 
 if (digitalRead(8) == HIGH) { 
 Serial.println("Button D"); 
 delay(250);
 }

}

Android Controlled Robot Arm

Create a mobile robot arm that you can control from an Android smart phone.

We found a used robot arm (OWI-535) on Kijiji, but you could buy a new one for $30-$60.  We then mounted it on an Arduino car chassis ($20) with some duct tape. Luckily the OW-535 robot arm has all its wiring exposed and it comes with a good wiring document.

Parts

For this project we used:

  • 1- OWI-535 robot arm
  • 1- Car chassis ($17)
  • 1- Four motor Arduino shield ($10)
  • 1- Two motor Arduino shield ($10)
  • 1- JY-MCU Bluetooth module ($7)
  • 1- Arduino Mega
  • Duct tape, jumpers and 4 alligator clips
  • 1- Small USB charger
  • 1- Small Box (to house all the components)

OLYMPUS DIGITAL CAMERA

Wiring

The biggest challenge is to control the 5 robot motors and 2 wheel motors. For this we used an Arduino Mega and 2 motors shields. Depending on the motor shields that you use you probably won’t be able stack the motor shields. We stacked one motor shield and we had the second motor shield floating.

robot-arm-circuit

Some of the key wiring connections were:

  • Mega Pin 44 -> Floating Motor Shield pin 4
  • Mega Pin 45 -> Floating Motor Shield pin 5
  • Mega Pin 46 ->Floating Motor Shield pin 6
  • Mega Pin 47 -> Floating Motor Shield pin 7
  • Mega Pin 5V -> Floating Motor Shield pin 5V and VIN
  • Mega Pin 19 RX1 -> Bluetooth module TX
  • Mega Pin 18 TX1 -> Bluetooth module RX

To power this project we found that a portable USB charger was enough. If however you find that a USB charger does not deliver enough power the OWI-535 robot arm has a built in power pack with exposed power connections.

The motor terminals on the car chassis are a little fragile, so rather than soldering wires we used alligator clips. We also duct taped the wires under the car chassis to keep them secure.

The Arduino wiring was pretty ugly so we stuffed all the loose components into a small box. The box was duct taped to the back of the robot arm, and 2 cuts were made to feed the wires through.

inside_box.png

Code

For the Arduino code you’ll need to add the appropriate motor libraries. For our motor shields  we used the Adafruit V1 motor shield (https://github.com/adafruit/Adafruit-Motor-Shield-library).

Because the first motor shield blocked the RX0/TX0 pins, we used RX1 and TX1 on pins 19 and 18. For the Bluetooth communications it meant the Serial1 was used in the Anduino code instead of Serial.

For our commands we used:

  • 0 = Hand open (motor 1 forward)
  • 1 = Hand closed (motor 1 backward)
  • 2 = Wrist down (motor 2 forward)
  • 3 = Wrist up (motor 2 backward)
  • 4 = Elbow down (motor 3 forward)
  • 5 = Elbow up (motor 3 backward)
  • 6 = Shoulder up (motor 4 forward)
  • 7 = Shoulder down (motor 4 backward)
  • g = Go forward (pins: 44/46=HIGH, 45/47=255)
  • s = Stop (pins: 44/46=HIGH, 45/47 =0)
  • r = Right turn (pins: 44/46=HIGH, 45=0, 47 =255)
  • l = Left turn (pins: 44/46=HIGH, 45=255, 47 =0)
  • b = Go backward (pins 44/46= LOW, pins 45/47 =255)
  • o = Light ON, (pin 50 = HIGH)
  • f = Light OFF, (pin 50 = LOW)

Our final Arduino code is:

//
// Bluetooth control of a mobile robot arm
//
#include 

char inByte;
// Define remapped pins for 'floating' motor shield
int E1 = 45; 
int M1 = 44; 
int E2 = 46; 
int M2 = 47; 
int LIGHTpin = 50;

// DC motor on M1
AF_DCMotor motor1(1); // hand
AF_DCMotor motor2(2); // wrist
AF_DCMotor motor3(3); // elbow
AF_DCMotor motor4(4); // shoulder


void setup() {
 pinMode(M1, OUTPUT); 
 pinMode(M2, OUTPUT);

pinMode( 19, INPUT_PULLUP ); // For better Bluetooth stability
 Serial1.begin(9600); 
 Serial1.println("Robot Commands");
 Serial1.println("Menu...");
 Serial1.println("Enter: 0-7 for robot arm, g/s/l/r/b for wheels ");
}

void loop() {
 
 if (Serial1.available() > 0) {

// read the incoming byte:
 inByte = Serial1.read();
 if (inByte == '0') { runmotor(motor1,FORWARD); }
 if (inByte == '1') { runmotor(motor1,BACKWARD); }
 if (inByte == '2') { runmotor(motor2,FORWARD); }
 if (inByte == '3') { runmotor(motor2,BACKWARD); }
 if (inByte == '4') { runmotor(motor3,FORWARD); }
 if (inByte == '5') { runmotor(motor3,BACKWARD); }
 if (inByte == '6') { runmotor(motor4,FORWARD); }
 if (inByte == '7') { runmotor(motor4,BACKWARD); }
 if (inByte == 'g') { runwheels(HIGH,HIGH,255,255); } 
 if (inByte == 's') { runwheels(HIGH,HIGH,0,0); } 
 if (inByte == 'r') { runwheels(HIGH,HIGH,0,255); } 
 if (inByte == 'l') { runwheels(HIGH,HIGH,255,0); } 
 if (inByte == 'b') { runwheels(LOW,LOW,255,255); } 
 if (inByte == 'o') { digitalWrite(LIGHTpin, HIGH); }
 if (inByte == 'f') { digitalWrite(LIGHTpin, LOW); } 
 }
}
//--------------------------------------------------
void runwheels (int M1dir,int M2dir,int E1speed, int E2speed) {
 // For control of wheels direction and speed
 digitalWrite(M1, M1dir); 
 digitalWrite(M2, M2dir); 
 analogWrite(E1, E1speed);
 analogWrite(E2, E2speed); 
}
//--------------------------------------------------
void runmotor(AF_DCMotor themotor, int direction) {
 // Robot Arm motor control
 themotor.setSpeed(250);
 themotor.run(direction);
 delay(250);
 themotor.run(RELEASE); 
}

Android App

MIT’s App Inventor is free and it’s a great way to quickly make Android apps: http://ai2.appinventor.mit.edu/.

For this app we used:

  • 1 TableArrangement
  • 1 Listpicker
  • 15 Buttons
  • 7 Labels
  • 1 BluetoothClient (non-visible)

app1.PNG

A TableArrangement component with 3 columns and 11 rows is used to position all of the buttons and labels. After a component is positioned into the table, select the component and then use the Properties window to change its text, color, or sizing. The Components window is used to rename the component.

In the Blocks screen, blocks are dragged from the left Blocks section to the main viewer section. The following key blocks were used:

  • when Screen1.Initialize. This is called when the app is opened and it will show a list of all the phone’s paired Bluetooth device. This list is generated by connecting the blocks: BluetoothClient1.AddressesAndNames to set ListPicker1.Elements
  • when ListPicker1.BeforePicking. This block is called after the JY-MCU Bluetooth module (typically HC-06) is selected. Inside this block ListPicker1.Selection is an input to the Call BluetoothClient1.Connect block.
  • when .Click. This block is called when a “command” button is clicked. This block will send the required Bluetooth text command, so for example “g” is sent to go forward, and “s” is sent to stop.

app3

The full AppInventor code is:

full_logic

Once all the logic is created, you can download the app (APK file) or use a QR quick code to put it on your phone.

app2qr

The final step is to power up your Arduino project. When you do this the JY-MCU Bluetooth module will start to blink. This is telling you the module is ready to pair with your phone. On your phone’s SETUP->BLUETOOTH SETTINGS scan for new devices and you should see the JY-MCU Bluetooth module (it’ll probably be called HC-06). The pairing code is: 1234.

Now you are ready to open your new Android app. A dialog will ask you which device to connect to. Select your HC-06 device.

select

For this point on you will be able to control the robot with your phone.

screenshot