This article demonstrates how to use App Inventor’s BLE (Bluetooth Low Energy) component to control Arduino 101 robotarm (It’s the same structure with mearm, four freedom degrees to a desktop machine arm). Please note that BLE component hasn’t been published, please use the test server http://ble-test.apinventor.mit.edu
Related Articles
Part List
2. Android phone (Must have BLE hardware, but I think it's not a problem for most Android devices today).
3. MeArm or any similar structure of 4-axis Robot Arm
App Inventor Designer: In addition to BLE , the most frequently used components are buttons and labels. Just something you can arrange your button in your viewer. The concept of operating the App is that when you press a button, it will send the signal to make Arduino robotarm to do an action, when you release the button, the action will immediately stop, which use the button events, TouchUp and TouchDown.
App Inventor Blocks:
Initialization: When the program starts, the program will ask BLE device for scanning.
Connection and disconnection: When you press the connect button, it will initiate the connection to the specified BLE device (Arduino 101). If successful, you'll see a message in the [Screen1.Title]. When disconnected, the reverse operation will be performed on the screen, all the buttons will become inoperable (Enabled = false).
Press the button: we only describes ButtonArmB1 (the button which hand out the front arm). Please note that we use the buttons in this TouchUp and Touchdown and other two events. There are eight buttons on the screen, with the button events. The viewer is very full!
Actually, this wording is too long, you can use the subroutine to manage your program.In fact, it just sent the different parameters, it is easy to make the program more cleaner!
Arduino 101 Code
#include <Servo.h>#include <SoftwareSerial.h>#include <CurieBLE.h>Servo Bottom;Servo Clamp;Servo ArmA;Servo ArmB;BLEPeripheral blePeripheral;BLEService MeArm("19B10010-E8F2-537E-4F6C-D104768A1214");BLEUnsignedIntCharacteristic readchar("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite | BLENotify);#define Bottom_pin 3#define Clamp_pin 9#define ArmA_pin 5#define ArmB_pin 6#define Bottom_max 170#define Bottom_min 0#define Clamp_max 140#define Clamp_min 50#define ArmA_max 150#define ArmA_min 50#define ArmB_max 210#define ArmB_min 120int Clamp_pos = Clamp_max;int Bottom_pos = (Bottom_max + Bottom_min)/2;int ArmA_pos = ArmA_min;int ArmB_pos = ArmB_max;int delta = 10;void setup() { Serial.begin(9600); blePeripheral.setLocalName("MeArm"); blePeripheral.setAdvertisedServiceUuid(MeArm.uuid()); blePeripheral.addAttribute(MeArm); blePeripheral.addAttribute(readchar); readchar.setValue(0); blePeripheral.begin(); Init_Pin();}void loop() { BLECentral central = blePeripheral.central(); if (central) { Serial.print("Connected to central: "); Serial.println(central.address()); while(central.connected()) { if(readchar.written()) { Serial.println(readchar.value()); switch (readchar.value()){ case 1: Serial.println("Turn Left"); stopp(); delay(100); BottomLeft(); delay(100); break; case 3: Serial.println("Turn Right"); stopp(); BottomRight(); delay(100); break; case 8: Serial.println("Clamp Open"); stopp(); delay(100); ClampOpen(); delay(100); break; case 5: Serial.println("Clamp Close"); stopp(); delay(100); ClampClose(); delay(100); break; case 7: Serial.println("Arm A Up"); stopp(); delay(100); ArmAUp(); delay(100); break; case 4: Serial.println("Arm A Down"); stopp(); delay(100); ArmADown(); delay(100); break; case 9: Serial.println("Arm B Up"); stopp(); delay(100); ArmBUp(); delay(100); break; case 6: Serial.println("Arm B Down"); stopp(); delay(100); ArmBDown(); delay(100); break; case 0: Serial.println("Stop"); stopp(); delay(100); } }//if }//while Serial.print(F("Disconnected from central: ")); Serial.println(central.address()); }//if } void Init_Pin() { Bottom.attach(Bottom_pin); Clamp.attach(Clamp_pin); ArmA.attach(ArmA_pin); ArmB.attach(ArmB_pin);}void ClampOpen() { Clamp_pos = Clamp_min; Clamp.write(Clamp_pos); }void ClampClose() { Clamp_pos = Clamp_max; Clamp.write(Clamp_pos); }void BottomRight() { if(Bottom_pos - delta > Bottom_min) Bottom_pos -= delta; Bottom.write(Bottom_pos); }void BottomLeft() { if(Bottom_pos + delta < Bottom_max) Bottom_pos += delta; Bottom.write(Bottom_pos); }void ArmAUp() { if(ArmA_pos + delta < ArmA_max) ArmA_pos += delta; ArmA.write(ArmA_pos); } void ArmADown() { if(ArmA_pos - delta > ArmA_min) ArmA_pos -= delta; ArmA.write(ArmA_pos);}void ArmBUp() { if(ArmB_pos - delta > ArmB_min) ArmB_pos -= delta; ArmB.write(ArmB_pos);}void ArmBDown() { if(ArmB_pos + delta < ArmB_max) ArmB_pos += delta; ArmB.write(ArmB_pos);}void stopp() { Bottom.write(Bottom_pos); Clamp.write(Clamp_pos); ArmA.write(ArmA_pos); ArmB.write(ArmB_pos);}
|
|