Components and supplies
Dual H-Bridge motor drivers L298
NodeMCU ESP8266 Breakout Board
UTSOURCE Electronic Parts
Arduino UNO
Project description
Code
code arduino
c_cpp
1 #include <Arduino.h> 2 #include <ESP8266WiFi.h> 3 #include <Hash.h> 4 #include <ESPAsyncTCP.h> 5 #include <ESPAsyncWebServer.h> 6 #include "FS.h" 7 8// Replace with your network credentials 9const char* ssid = "NguyenTuong"; 10const char* password = "064897435"; 11 12#define LED 16 13 14int rotation_value; //value of rotation from local web 15//-----------------------position control 16const byte pin_a = 4; //for encoder pulse A 17const byte pin_b = 5; //for encoder pulse B 18const byte pin_fwd = 12; //for H-bridge: run motor forward 19const byte pin_bwd = 13; //for H-bridge: run motor backward 20const byte pin_pwm = 14; //for H-bridge: motor speed 21int encoder_r = 0; 22int encoder_f = 0; 23int position_pv = 0; 24int position_sv = 0; 25int m_direction = 0; 26 27// Create AsyncWebServer object on port 80 28AsyncWebServer server(80); 29 30const char index_html[] PROGMEM = R"rawliteral( 31<!DOCTYPE HTML><html> 32<head> 33 <script src="jquery.min.js"></script> 34 <script src="knob.js"></script> 35 36 <meta name="viewport" content="width=device-width, initial-scale=1"> 37 38</head> 39 40<body> 41 <center> 42 <h2>Motor position control</h2> 43 <input type="text" class="dial" value="1" data-displayPrevious=true data-fgColor="#00A8A9" data-thickness=.6> 44 </center> 45 46 <script> 47 $(".dial").knob({ 48 'change' : function (v) { console.log(Math.round(v,0)); 49 $.ajax({url: "/setLOCATION?rotation="+Math.round(v,0), success: function(result){ 50 $("#div1").html(result); 51 }}); 52 } 53 }); 54 </script> 55 56</body> 57</html>)rawliteral"; 58 59 60void setup(){ 61 //---------declaration for position control 62 pinMode(pin_a,INPUT_PULLUP); 63 pinMode(pin_b,INPUT_PULLUP); 64 pinMode(pin_fwd,OUTPUT); 65 pinMode(pin_bwd,OUTPUT); 66 pinMode(pin_pwm,OUTPUT); 67 attachInterrupt(digitalPinToInterrupt(pin_a), detect_a_r, RISING); 68 69 digitalWrite(pin_fwd,0); //stop motor 70 digitalWrite(pin_bwd,0); //stop motor 71 72 // Serial port for debugging purposes 73 Serial.begin(115200); 74 75 // Connect to Wi-Fi 76 WiFi.begin(ssid, password); 77 while (WiFi.status() != WL_CONNECTED) { 78 delay(1000); 79 Serial.println("Connecting to WiFi.."); 80 } 81 if(!SPIFFS.begin()){ 82 Serial.println("An Error has occurred while mounting SPIFFS"); 83 return; 84 } 85 86 // Print ESP32 Local IP Address 87 Serial.println(WiFi.localIP()); 88 89 // Route for root / web page 90 91 server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ 92 request->send_P(200, "text/html", index_html); 93 }); 94 server.on("/jquery.min.js", HTTP_GET, [](AsyncWebServerRequest *request){ 95 request->send(SPIFFS, "/jquery.min.js", "text/javascript"); 96 }); 97 server.on("/knob.js", HTTP_GET, [](AsyncWebServerRequest *request){ 98 request->send(SPIFFS, "/knob.js", "text/javascript"); 99 }); 100 server.on("/setLOCATION",HTTP_GET, [](AsyncWebServerRequest *request){ 101 String ROTATION = request->getParam("rotation")->value(); 102 rotation_value = 1024 - (ROTATION.toInt())*10; 103 Serial.print("rotation_value "); 104 Serial.println(rotation_value); 105 analogWrite(LED,rotation_value); //adjust LED light on NodeMCU 106 request->send(200, "text/plane",""); 107 108 }); 109 // Start server 110 server.begin(); 111} 112 113void loop(){ 114 analogWrite(pin_pwm,650); //set motor speed = 650/1024 115 position_sv = (1024 - rotation_value)/10; //unit: % (e.g. 0% ~ 0dg; 100% ~ 360dg) 116 position_pv = ((encoder_r+encoder_f)/2); //unit: %; encoder pulse per revolution: 200ppr 117 if(position_pv-position_sv < 0){ 118 digitalWrite(pin_fwd,1); 119 digitalWrite(pin_bwd,0); 120 } 121 else{ 122 if(position_pv-position_sv > 0){ 123 digitalWrite(pin_fwd,0); 124 digitalWrite(pin_bwd,1); 125 } 126 else{ 127 //stop motor if position between +0 ~ -0 128 digitalWrite(pin_fwd,0); //stop motor 129 digitalWrite(pin_bwd,0); //stop motor 130 } 131 } 132} 133 134void detect_a_r() { 135 m_direction = digitalRead(pin_b); //read direction of motor 136 if(!m_direction){ 137 encoder_r += 1; //increasing encoder at forward run 138 } 139 else{ 140 encoder_r += -1; //decreasing encoder at backward run 141 } 142 attachInterrupt(digitalPinToInterrupt(pin_a), detect_a_f, FALLING); //change interrupt to Falling edge 143} 144void detect_a_f() { 145 m_direction = digitalRead(pin_b); //read direction of motor 146 if(m_direction){ 147 encoder_f += 1; //increasing encoder at forward run 148 } 149 else{ 150 encoder_f += -1; //decreasing encoder at backward run 151 } 152 attachInterrupt(digitalPinToInterrupt(pin_a), detect_a_r, RISING); //change interrupt to Rising edge 153} 154
Downloadable files
12_circuit_1_Qufw6p0DGe.png
12_circuit_1_Qufw6p0DGe.png
12_circuit_1_Qufw6p0DGe.png
12_circuit_1_Qufw6p0DGe.png
Comments
Only logged in users can leave comments
whitebank
0 Followers
•0 Projects
0