Control your robot with your phone (joysticks app).
In this project, I control a robotic arm with a phone using HC-05 bluetooth module and an Android app (mit app inventor).
Components and supplies
Resistor 1k ohm
Jumper wires (generic)
Breadboard (generic)
9V battery (generic)
HC-05 Bluetooth Module
Arduino UNO
Apps and platforms
MIT App Inventor 2
Arduino IDE
Project description
Code
Joystick xy + joystick y
arduino
This code goes with the joysticks xy-y Android app.
1#include <TimedAction.h> //this library is outdated and it won't work with the newest versions of Arduino. After you downloaded the library - https://playground.arduino.cc/Code/TimedAction/ - you must open the libraries folder, open the TimedAction folder, open the TimedAction.h file and change the 33/34 line (from #include "TimedAction.h" to #include "Arduino.h"). If it is already changed, then you shouldn't have any problems. 2#include <Servo.h> 3 4Servo myservo1; 5Servo myservo2; 6Servo myservo3; 7Servo myservo4; 8 9int val,cnt=0,v[4]; 10int pos1=90,pos2=90,pos3=90,pos4=90; 11int val1,val2,val3,val4; 12int timer1,timer2,timer3; 13int Ok1=1,Ok2=1,Ok3=1; 14int delayy2,delayx1,delayy1; 15 16 17void readbt() { 18 val=Serial.read(); 19 cnt++; 20 v[cnt]=val; 21 if(v[1]==1 && cnt==3) { 22 cnt=0; 23 } 24 if(v[1]!=1 && cnt==2) { 25 cnt=0; 26 } 27} 28 29void program() { 30 timer1++; 31 timer2++; 32 timer3++; 33} 34 35TimedAction readbtThread = TimedAction(10,readbt); 36TimedAction timerThread = TimedAction(1,program); 37 38void setup() { 39 // put your setup code here, to run once: 40 Serial.begin(9600); 41 myservo1.attach(9); 42 myservo2.attach(10); 43 myservo3.attach(11); 44 myservo4.attach(6); 45} 46 47void loop() { 48 // put your main code here, to run repeatedly: 49 if(Serial.available()) { 50 while(Serial.available() == 0); 51 readbtThread.check(); 52 timerThread.check(); 53 if(v[1]==1) { 54 Ok1=0; 55 val1=v[2]; 56 val2=v[3]; 57 } 58 if(v[1]==2) { 59 Ok2=0; 60 val3=v[2]; 61 } 62 if(v[1]==3) { 63 Ok3=0; 64 val4=v[2]; 65 } 66 } 67 joystick1(); 68 joystick2(); 69 slider(); 70} 71 72void joystick1() { 73 if (Ok1==0) { 74 timerThread.check(); 75 if(v[1]==1 && (v[2]==100 && v[3]==100)) { 76 Ok1=1; 77 } 78 if(val1>100 && pos1<180) { 79 delayx1=((200-val1))+15; 80 if(timer1>=delayx1) { 81 pos1++; 82 myservo1.write(pos1); 83 timer1=0; 84 } 85 } 86 if(val1<100 && pos1>0) { 87 delayx1=val1+15; 88 if(timer1>=delayx1) { 89 pos1--; 90 myservo1.write(pos1); 91 timer1=0; 92 } 93 } 94 if(val2>100 && pos2<180) { 95 delayy1=((200-val2))+15; 96 if(timer2>=delayy1) { 97 pos2++; 98 myservo2.write(pos2); 99 timer2=0; 100 } 101 } 102 if(val2<100 && pos2>0) { 103 delayy1=val2+15; 104 if(timer2>=delayy1) { 105 pos2--; 106 myservo2.write(pos2); 107 timer2=0; 108 } 109 } 110 } 111} 112 113void joystick2() { 114 if (Ok2==0) { 115 timerThread.check(); 116 if(v[1]==2 && v[2]==100) { 117 Ok2=1; 118 } 119 if(val3>100 && pos3<180) { 120 delayy2=((200-val3))+15; 121 if(timer3>=delayy2) { 122 pos3++; 123 myservo3.write(pos3); 124 timer3=0; 125 } 126 } 127 if(val3<100 && pos3>0) { 128 delayy2=val3+15; 129 if(timer3>=delayy2) { 130 pos3--; 131 myservo3.write(pos3); 132 timer3=0; 133 } 134 } 135 } 136} 137 138void slider() { 139 if(Ok3==0) { 140 if(pos4==val4) Ok3=1; 141 if (val4>pos4) { 142 for(int i=pos4;i<=val4;i++) { 143 myservo4.write(i); 144 delay(15); 145 } 146 } 147 if(val4<pos4) { 148 for(int i=pos4;i>=val4;i--) { 149 myservo4.write(i); 150 delay(15); 151 } 152 } 153 pos4=val4; 154 } 155} 156
Joystick y + joystick y
arduino
This code goes with the joysticks y-y Android app.
1#include <TimedAction.h>//this library is outdated and it won't work with the newest versions of Arduino. After you downloaded the library - https://playground.arduino.cc/Code/TimedAction/ - you must open the libraries folder, open the TimedAction folder, open the TimedAction.h file and change the 33/34 line (from #include "TimedAction.h" to #include "Arduino.h"). If it is already changed, then you shouldn't have any problems. 2#include <Servo.h> 3 4Servo myservo1; 5Servo myservo2; 6Servo myservo3; 7 8int val,cnt=0,v[4]; 9int pos1=90,pos2=90,pos3=90; 10int val1,val2,val3; 11int timer1,timer2; 12int Ok1=1,Ok2=1,Ok3=1; 13int delayy1,delayy2; 14 15void readbt() { 16 val=Serial.read(); 17 cnt++; 18 v[cnt]=val; 19 if(cnt==2) { 20 cnt=0; 21 } 22} 23 24void program() { 25 timer1++; 26 timer2++; 27} 28 29TimedAction readbtThread = TimedAction(10,readbt); 30TimedAction timerThread = TimedAction(1,program); 31 32void setup() { 33 // put your setup code here, to run once: 34 Serial.begin(9600); 35 myservo1.attach(9); 36 myservo2.attach(10); 37 myservo3.attach(11); 38} 39 40void loop() { 41 // put your main code here, to run repeatedly: 42 if(Serial.available()) { 43 while(Serial.available() == 0); 44 readbtThread.check(); 45 timerThread.check(); 46 if(v[1]==1) { 47 Ok1=0; 48 val1=v[2]; 49 } 50 if(v[1]==2) { 51 Ok2=0; 52 val2=v[2]; 53 } 54 if(v[1]==3) { 55 Ok3=0; 56 val3=v[2]; 57 } 58 } 59 joystick1(); 60 joystick2(); 61 slider(); 62} 63 64void joystick1() { 65 if (Ok1==0) { 66 timerThread.check(); 67 if(v[1]==1 && v[2]==100) { 68 Ok1=1; 69 } 70 if(val1>100 && pos1>0) { 71 delayy1=((200-val1))+15; 72 if(timer1>=delayy1) { 73 pos1--; 74 myservo1.write(pos1); 75 timer1=0; 76 } 77 } 78 if(val1<100 && pos1<180) { 79 delayy1=val1+15; 80 if(timer1>=delayy1) { 81 pos1++; 82 myservo1.write(pos1); 83 timer1=0; 84 } 85 } 86 } 87} 88 89void joystick2() { 90 if (Ok2==0) { 91 timerThread.check(); 92 if(v[1]==2 && v[2]==100) { 93 Ok2=1; 94 } 95 if(val2>100 && pos2<180) { 96 delayy2=((200-val2))+15; 97 if(timer2>=delayy2) { 98 pos2++; 99 myservo2.write(pos2); 100 timer2=0; 101 } 102 } 103 if(val2<100 && pos2>0) { 104 delayy2=val2+15; 105 if(timer2>=delayy2) { 106 pos2--; 107 myservo2.write(pos2); 108 timer2=0; 109 } 110 } 111 } 112} 113 114void slider() { 115 if(Ok3==0) { 116 if(pos3==val3) Ok3=1; 117 if (val3>pos3) { 118 for(int i=pos3;i<=val3;i++) { 119 myservo3.write(i); 120 delay(15); 121 } 122 } 123 if(val3<pos3) { 124 for(int i=pos3;i>=val3;i--) { 125 myservo3.write(i); 126 delay(15); 127 } 128 } 129 pos3=val3; 130 } 131} 132
Joystick y + joystick y
arduino
This code goes with the joysticks y-y Android app.
1#include <TimedAction.h>//this library is outdated and it won't work with the newest versions of Arduino. After you downloaded the library - https://playground.arduino.cc/Code/TimedAction/ - you must open the libraries folder, open the TimedAction folder, open the TimedAction.h file and change the 33/34 line (from #include "TimedAction.h" to #include "Arduino.h"). If it is already changed, then you shouldn't have any problems. 2#include <Servo.h> 3 4Servo myservo1; 5Servo myservo2; 6Servo myservo3; 7 8int val,cnt=0,v[4]; 9int pos1=90,pos2=90,pos3=90; 10int val1,val2,val3; 11int timer1,timer2; 12int Ok1=1,Ok2=1,Ok3=1; 13int delayy1,delayy2; 14 15void readbt() { 16 val=Serial.read(); 17 cnt++; 18 v[cnt]=val; 19 if(cnt==2) { 20 cnt=0; 21 } 22} 23 24void program() { 25 timer1++; 26 timer2++; 27} 28 29TimedAction readbtThread = TimedAction(10,readbt); 30TimedAction timerThread = TimedAction(1,program); 31 32void setup() { 33 // put your setup code here, to run once: 34 Serial.begin(9600); 35 myservo1.attach(9); 36 myservo2.attach(10); 37 myservo3.attach(11); 38} 39 40void loop() { 41 // put your main code here, to run repeatedly: 42 if(Serial.available()) { 43 while(Serial.available() == 0); 44 readbtThread.check(); 45 timerThread.check(); 46 if(v[1]==1) { 47 Ok1=0; 48 val1=v[2]; 49 } 50 if(v[1]==2) { 51 Ok2=0; 52 val2=v[2]; 53 } 54 if(v[1]==3) { 55 Ok3=0; 56 val3=v[2]; 57 } 58 } 59 joystick1(); 60 joystick2(); 61 slider(); 62} 63 64void joystick1() { 65 if (Ok1==0) { 66 timerThread.check(); 67 if(v[1]==1 && v[2]==100) { 68 Ok1=1; 69 } 70 if(val1>100 && pos1>0) { 71 delayy1=((200-val1))+15; 72 if(timer1>=delayy1) { 73 pos1--; 74 myservo1.write(pos1); 75 timer1=0; 76 } 77 } 78 if(val1<100 && pos1<180) { 79 delayy1=val1+15; 80 if(timer1>=delayy1) { 81 pos1++; 82 myservo1.write(pos1); 83 timer1=0; 84 } 85 } 86 } 87} 88 89void joystick2() { 90 if (Ok2==0) { 91 timerThread.check(); 92 if(v[1]==2 && v[2]==100) { 93 Ok2=1; 94 } 95 if(val2>100 && pos2<180) { 96 delayy2=((200-val2))+15; 97 if(timer2>=delayy2) { 98 pos2++; 99 myservo2.write(pos2); 100 timer2=0; 101 } 102 } 103 if(val2<100 && pos2>0) { 104 delayy2=val2+15; 105 if(timer2>=delayy2) { 106 pos2--; 107 myservo2.write(pos2); 108 timer2=0; 109 } 110 } 111 } 112} 113 114void slider() { 115 if(Ok3==0) { 116 if(pos3==val3) Ok3=1; 117 if (val3>pos3) { 118 for(int i=pos3;i<=val3;i++) { 119 myservo3.write(i); 120 delay(15); 121 } 122 } 123 if(val3<pos3) { 124 for(int i=pos3;i>=val3;i--) { 125 myservo3.write(i); 126 delay(15); 127 } 128 } 129 pos3=val3; 130 } 131} 132
Joystick xy + joystick y
arduino
This code goes with the joysticks xy-y Android app.
1#include <TimedAction.h> //this library is outdated and it won't work with the newest versions of Arduino. After you downloaded the library - https://playground.arduino.cc/Code/TimedAction/ - you must open the libraries folder, open the TimedAction folder, open the TimedAction.h file and change the 33/34 line (from #include "TimedAction.h" to #include "Arduino.h"). If it is already changed, then you shouldn't have any problems. 2#include <Servo.h> 3 4Servo myservo1; 5Servo myservo2; 6Servo myservo3; 7Servo myservo4; 8 9int val,cnt=0,v[4]; 10int pos1=90,pos2=90,pos3=90,pos4=90; 11int val1,val2,val3,val4; 12int timer1,timer2,timer3; 13int Ok1=1,Ok2=1,Ok3=1; 14int delayy2,delayx1,delayy1; 15 16 17void readbt() { 18 val=Serial.read(); 19 cnt++; 20 v[cnt]=val; 21 if(v[1]==1 && cnt==3) { 22 cnt=0; 23 } 24 if(v[1]!=1 && cnt==2) { 25 cnt=0; 26 } 27} 28 29void program() { 30 timer1++; 31 timer2++; 32 timer3++; 33} 34 35TimedAction readbtThread = TimedAction(10,readbt); 36TimedAction timerThread = TimedAction(1,program); 37 38void setup() { 39 // put your setup code here, to run once: 40 Serial.begin(9600); 41 myservo1.attach(9); 42 myservo2.attach(10); 43 myservo3.attach(11); 44 myservo4.attach(6); 45} 46 47void loop() { 48 // put your main code here, to run repeatedly: 49 if(Serial.available()) { 50 while(Serial.available() == 0); 51 readbtThread.check(); 52 timerThread.check(); 53 if(v[1]==1) { 54 Ok1=0; 55 val1=v[2]; 56 val2=v[3]; 57 } 58 if(v[1]==2) { 59 Ok2=0; 60 val3=v[2]; 61 } 62 if(v[1]==3) { 63 Ok3=0; 64 val4=v[2]; 65 } 66 } 67 joystick1(); 68 joystick2(); 69 slider(); 70} 71 72void joystick1() { 73 if (Ok1==0) { 74 timerThread.check(); 75 if(v[1]==1 && (v[2]==100 && v[3]==100)) { 76 Ok1=1; 77 } 78 if(val1>100 && pos1<180) { 79 delayx1=((200-val1))+15; 80 if(timer1>=delayx1) { 81 pos1++; 82 myservo1.write(pos1); 83 timer1=0; 84 } 85 } 86 if(val1<100 && pos1>0) { 87 delayx1=val1+15; 88 if(timer1>=delayx1) { 89 pos1--; 90 myservo1.write(pos1); 91 timer1=0; 92 } 93 } 94 if(val2>100 && pos2<180) { 95 delayy1=((200-val2))+15; 96 if(timer2>=delayy1) { 97 pos2++; 98 myservo2.write(pos2); 99 timer2=0; 100 } 101 } 102 if(val2<100 && pos2>0) { 103 delayy1=val2+15; 104 if(timer2>=delayy1) { 105 pos2--; 106 myservo2.write(pos2); 107 timer2=0; 108 } 109 } 110 } 111} 112 113void joystick2() { 114 if (Ok2==0) { 115 timerThread.check(); 116 if(v[1]==2 && v[2]==100) { 117 Ok2=1; 118 } 119 if(val3>100 && pos3<180) { 120 delayy2=((200-val3))+15; 121 if(timer3>=delayy2) { 122 pos3++; 123 myservo3.write(pos3); 124 timer3=0; 125 } 126 } 127 if(val3<100 && pos3>0) { 128 delayy2=val3+15; 129 if(timer3>=delayy2) { 130 pos3--; 131 myservo3.write(pos3); 132 timer3=0; 133 } 134 } 135 } 136} 137 138void slider() { 139 if(Ok3==0) { 140 if(pos4==val4) Ok3=1; 141 if (val4>pos4) { 142 for(int i=pos4;i<=val4;i++) { 143 myservo4.write(i); 144 delay(15); 145 } 146 } 147 if(val4<pos4) { 148 for(int i=pos4;i>=val4;i--) { 149 myservo4.write(i); 150 delay(15); 151 } 152 } 153 pos4=val4; 154 } 155} 156
Downloadable files
Circuit schematic Arduino + HC-05 Bluetooth module
You can change the number of servos. I am using MG995 servos.
Circuit schematic Arduino + HC-05 Bluetooth module

Circuit schematic Arduino + HC-05 Bluetooth module
You can change the number of servos. I am using MG995 servos.
Circuit schematic Arduino + HC-05 Bluetooth module

Documentation
JoystickAPK
Unzip this package and open it with your phone. These are the apps I've built.
JoystickAPK
JoystickAPK
Unzip this package and open it with your phone. These are the apps I've built.
JoystickAPK
JoystickAIA
Unzip this package and open it with Mit App Inventor 2. There you can change the design or block section and then build the app.
JoystickAIA
Comments
Only logged in users can leave comments