Devices & Components
Arduino Uno Rev3
Li-Polymer Battery, 150mAh
DC Motor, 12 V
NodeMCU ESP8266 Breakout Board
Dual H-Bridge motor drivers L298
Jumper wires (generic)
Hardware & Tools
Mastech MS8217 Autorange Digital Multimeter
Multitool, Screwdriver
Software & Tools
Arduino IDE
MIT App Inventor 2
Project description
Code
Arduino Uno Code
arduino
Code for receiving incoming data from NodeMCU and giving input to the motor drivers to vary the speed and direction of spin of motors.
1#include <Wire.h> 2 3//motors 4int motor1pin1 = 2; 5int motor1pin2 = 3; 6int motor2pin1 = 4; 7int motor2pin2 = 5; 8 9int speed9 = 40; 10int speed10 = 40; 11 12int slider; 13int valx; 14int valy; 15 16void setup() { 17 Wire.begin(8); /* join i2c bus with address 8 */ 18 Wire.onReceive(receiveEvent); /* register receive event */ 19 Wire.onRequest(requestEvent); /* register request event */ 20 Serial.begin(9600); /* start serial for debug */ 21 22 pinMode(motor1pin1, OUTPUT); 23 pinMode(motor1pin2, OUTPUT); 24 pinMode(motor2pin1, OUTPUT); 25 pinMode(motor2pin2, OUTPUT); 26 27 pinMode(9, OUTPUT); 28 pinMode(10, OUTPUT); 29 30 analogWrite(9,speed9); //left 31 analogWrite(10, speed10);//Right 32 33 //anticlockwise 34 digitalWrite(motor1pin1, HIGH); //right motor going front 35 digitalWrite(motor1pin2, LOW); //right motor going front 36 digitalWrite(motor2pin1, LOW); //left motor going front 37 digitalWrite(motor2pin2, HIGH); //left motor going front 38 39 /*temporary setup code*/ 40 analogWrite(9, 60); 41 analogWrite(10, 60); 42} 43 44void loop() { 45 46} 47 48// function that executes whenever data is received from master 49void receiveEvent(int howMany) { 50 while (0 <Wire.available()) { 51 byte low = Wire.read(); 52 byte high = Wire.read(); 53 int val = word(high, low); 54 55 56 if(val>200 && val<=400){ 57 //Serial.print("Y: "); 58 valy = 400 - val; 59 //Serial.println(valy); 60 if(valy<=100){ 61 digitalWrite(motor1pin1, HIGH); //right motor going front 62 digitalWrite(motor1pin2, LOW); //right motor going front 63 digitalWrite(motor2pin1, LOW); //left motor going front 64 digitalWrite(motor2pin2, HIGH); //left motor going front 65 Serial.print("Moving Forward"); 66 67 } 68 else if(valy>100){ 69 digitalWrite(motor1pin1, LOW); //right motor going front 70 digitalWrite(motor1pin2, HIGH); //right motor going front 71 digitalWrite(motor2pin1, HIGH); //left motor going front 72 digitalWrite(motor2pin2, LOW); //left motor going front 73 Serial.print("Moving Backward"); 74 } 75 } 76 if(val>1000){ 77 //Serial.print("Speed: "); 78 slider = val - 1000; 79 speed10 = slider/5; 80 speed9 = slider/5; 81 //Serial.println(slider); 82 analogWrite(10, speed10);//right 83 analogWrite(9, speed9);//left 84 Serial.print("Left speed: "); 85 Serial.println(speed9); 86 Serial.print("Right speed: "); 87 Serial.println(speed10); 88 } 89 if(val<=200){ 90 //Serial.print("X: "); 91 valx = 200-val; 92 //Serial.println(valx); 93 if(valx<100){ 94 speed9 = speed9 - ((100-valx)/10); 95 speed10 = speed10 + ((100-valx)/10); 96 analogWrite(9, speed9); 97 analogWrite(10, speed10); 98 if(speed9<0){ 99 speed9 = 0; 100 speed10 = 100; 101 analogWrite(9, speed9); 102 analogWrite(10, speed10); 103 } 104 Serial.print("Left speed: "); 105 Serial.println(speed9); 106 Serial.print("Right speed: "); 107 Serial.println(speed10); 108 } 109 else if(valx>100){ 110 speed9 = speed9 + ((valx-100)/10); 111 speed10 = speed10 - ((valx-100)/10); 112 analogWrite(9, speed9); 113 analogWrite(10, speed10); 114 if(speed10<0){ 115 speed9 = 100; 116 speed10 = 0; 117 analogWrite(9, speed9); 118 analogWrite(10, speed10); 119 } 120 Serial.print("Left speed: "); 121 Serial.println(speed9); 122 Serial.print("Right speed: "); 123 Serial.println(speed10); 124 } 125 else if(valx==100){ 126 speed9 = 40; 127 speed10 = 40; 128 analogWrite(9, speed9); 129 analogWrite(10, speed10); 130 Serial.print("Left speed: "); 131 Serial.println(speed9); 132 Serial.print("Right speed: "); 133 Serial.println(speed10); 134 } 135 } 136 Serial.println(); /* to newline */ 137} 138} 139
NodeMCU Code
arduino
Code for taking input data from your Joystick application on the phone and sending the data to Arduino via I2C communication protocol.
1#include <ESP8266WiFi.h> 2#include <WiFiClient.h> 3#include <Wire.h> 4 5const char* ssid = "Wi-Fi name"; 6const char* password = "password"; 7 8WiFiServer server(80); 9 10void setup() { 11 Serial.begin(115200); 12 delay(10); 13 14 Wire.begin(D1, D2); 15 16 Serial.println(); 17 Serial.print("Connecting to "); 18 Serial.print(ssid); 19 20 WiFi.begin(ssid, password); 21 22 while(WiFi.status() != WL_CONNECTED){ 23 delay(500); 24 Serial.print("."); 25 } 26 Serial.println(""); 27 Serial.println("WiFi connected"); 28 29 server.begin(); 30 Serial.println("Server started"); 31 32 Serial.println(WiFi.localIP()); 33} 34 35void loop() { 36 WiFiClient client = server.available(); 37 if(!client){ 38 return; 39 } 40 41 Serial.println("New Client"); 42 while(!client.available()){ 43 delay(1); 44 } 45 46 String req = client.readStringUntil('\r'); 47 req.replace("+", " "); // Spaces without + 48 req.replace(" HTTP/1.1", ""); // this delete HTTP/1.1 49 req.replace("GET /", ""); 50 int val = req.toInt(); 51 52 byte buffer[10]; 53 buffer[0] = lowByte(val); 54 buffer[1] = highByte(val); 55 56 Serial.println(val); 57 Wire.beginTransmission(8); 58 Wire.write(buffer, 2); 59 Wire.endTransmission(); 60 61 62 client.flush(); 63 64}
Remote_Controll_Joystick.aia
java
You can open this file by going to https://appinventor.mit.edu/. Download the .aia file and then import project on the App Inventor.
1inary file (no preview
NodeMCU Code
arduino
Code for taking input data from your Joystick application on the phone and sending the data to Arduino via I2C communication protocol.
1#include <ESP8266WiFi.h> 2#include <WiFiClient.h> 3#include <Wire.h> 4 5const char* ssid = "Wi-Fi name"; 6const char* password = "password"; 7 8WiFiServer server(80); 9 10void setup() { 11 Serial.begin(115200); 12 delay(10); 13 14 Wire.begin(D1, D2); 15 16 Serial.println(); 17 Serial.print("Connecting to "); 18 Serial.print(ssid); 19 20 WiFi.begin(ssid, password); 21 22 while(WiFi.status() != WL_CONNECTED){ 23 delay(500); 24 Serial.print("."); 25 } 26 Serial.println(""); 27 Serial.println("WiFi connected"); 28 29 server.begin(); 30 Serial.println("Server started"); 31 32 Serial.println(WiFi.localIP()); 33} 34 35void loop() { 36 WiFiClient client = server.available(); 37 if(!client){ 38 return; 39 } 40 41 Serial.println("New Client"); 42 while(!client.available()){ 43 delay(1); 44 } 45 46 String req = client.readStringUntil('\ '); 47 req.replace("+", " "); // Spaces without + 48 req.replace(" HTTP/1.1", ""); // this delete HTTP/1.1 49 req.replace("GET /", ""); 50 int val = req.toInt(); 51 52 byte buffer[10]; 53 buffer[0] = lowByte(val); 54 buffer[1] = highByte(val); 55 56 Serial.println(val); 57 Wire.beginTransmission(8); 58 Wire.write(buffer, 2); 59 Wire.endTransmission(); 60 61 62 client.flush(); 63 64}
Remote_Controll_Joystick.aia
java
You can open this file by going to https://appinventor.mit.edu/. Download the .aia file and then import project on the App Inventor.
1inary file (no preview
Downloadable files
Interfacing L298n motor driver with Arduino and Motors
We direct the power to the Arduino via the L298n motor driver.
Interfacing L298n motor driver with Arduino and Motors

Interfacing NodeMCU with Arduino
To power the NodeMCU, just connect the 3V3 pin on the Wi-Fi module to 3.3V port on Arduino.
Interfacing NodeMCU with Arduino

Speed and Direction Control Circuit
Speed and Direction Control Circuit

Interfacing L298n motor driver with Arduino and Motors
We direct the power to the Arduino via the L298n motor driver.
Interfacing L298n motor driver with Arduino and Motors

Interfacing NodeMCU with Arduino
To power the NodeMCU, just connect the 3V3 pin on the Wi-Fi module to 3.3V port on Arduino.
Interfacing NodeMCU with Arduino

Speed and Direction Control Circuit
Speed and Direction Control Circuit

Comments
Only logged in users can leave comments