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
9const char* ssid = "NguyenTuong";
10const char* password = "064897435";
11
12#define LED 16
13
14int rotation_value;
15
16const byte pin_a = 4;
17const byte pin_b = 5;
18const byte pin_fwd = 12;
19const byte pin_bwd = 13;
20const byte pin_pwm = 14;
21int encoder_r = 0;
22int encoder_f = 0;
23int position_pv = 0;
24int position_sv = 0;
25int m_direction = 0;
26
27
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
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);
70 digitalWrite(pin_bwd,0);
71
72
73 Serial.begin(115200);
74
75
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
87 Serial.println(WiFi.localIP());
88
89
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);
106 request->send(200, "text/plane","");
107
108 });
109
110 server.begin();
111}
112
113void loop(){
114 analogWrite(pin_pwm,650);
115 position_sv = (1024 - rotation_value)/10;
116 position_pv = ((encoder_r+encoder_f)/2);
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
128 digitalWrite(pin_fwd,0);
129 digitalWrite(pin_bwd,0);
130 }
131 }
132}
133
134void detect_a_r() {
135 m_direction = digitalRead(pin_b);
136 if(!m_direction){
137 encoder_r += 1;
138 }
139 else{
140 encoder_r += -1;
141 }
142 attachInterrupt(digitalPinToInterrupt(pin_a), detect_a_f, FALLING);
143}
144void detect_a_f() {
145 m_direction = digitalRead(pin_b);
146 if(m_direction){
147 encoder_f += 1;
148 }
149 else{
150 encoder_f += -1;
151 }
152 attachInterrupt(digitalPinToInterrupt(pin_a), detect_a_r, RISING);
153}
154
Anonymous user
2 years ago
This is an interesting project. I like the JS interface and how you programmed it. I now understand how to use interupts for this type op indexmotors. However i have some comments. I experienced some issues before i got my version of this project working. What versions of Arduino IDE and boards did you use? I had to modify your sketch on several points to get it working. The main problem is proberly the combination with analogWrite() and interupts. ESP8266 (and Arduino IDE) seem to have some issues with that. This is the code i used: https://github.com/Quercuz/ESP8266-DC-Motor-Position-Control