7
7
8
8
9
9
// Define the pins for the motors and drivers
10
- const int stepPin1 = 5 ;
11
- const int dirPin1 = 15 ;
12
- const int enablePin = 19 ;
13
- const int stepPin2 = 12 ; // D6
10
+ const int stepPin1 = 5 ;
11
+ const int dirPin1 = 15 ;
12
+ const int enablePin = 19 ;
13
+ const int stepPin2 = 12 ; // D6
14
14
const int dirPin2 = 14 ; // D5
15
15
16
+ const int DC_motor = 4 ;
17
+
16
18
// Define the maximum speed and acceleration of the motors
17
19
const float maxSpeed = 200 ; // in steps per second
18
20
const float acceleration = 200 ; // in steps per second per second
@@ -41,11 +43,14 @@ unsigned long previousSensorTime = 0;
41
43
unsigned int object_measured = 0 ;
42
44
unsigned long previousWiFiTime = 0 ;
43
45
unsigned long previousDisplayTime = 0 ;
46
+ unsigned long startMotorTime = 0 ;
47
+ unsigned long TOTAL_TIME_MS = 90000 ;
44
48
45
49
const unsigned long sensorInterval = 20 ; // interval in milliseconds
46
- const unsigned long WiFiInterval = 1500 ;
50
+ const unsigned long WiFiInterval = 720 ;
47
51
const unsigned long WiFiInterval2 = 2000 ;
48
- const unsigned long displayInterval = 1000 ;
52
+ const unsigned long displayInterval = 3000 ;
53
+
49
54
50
55
// SERVER SETUP
51
56
// Replace with your network credentials
@@ -62,6 +67,8 @@ void setup() {
62
67
// Set up the motor driver pins
63
68
pinMode (enablePin, OUTPUT);
64
69
digitalWrite (enablePin, LOW);
70
+ pinMode (DC_motor, OUTPUT);
71
+ digitalWrite (DC_motor, HIGH);
65
72
66
73
Serial.begin (115200 );
67
74
@@ -73,21 +80,24 @@ void setup() {
73
80
// Set up the OLED display
74
81
display.begin (SSD1306_SWITCHCAPVCC, OLED_ADDR);
75
82
display.clearDisplay ();
76
- display.setTextSize (1 );
83
+ display.setTextSize (2 );
77
84
display.setTextColor (SSD1306_WHITE);
78
- display.setCursor (0 , 0 );
85
+ display.setCursor (0 , 0 );
79
86
display.print (" Memristor Robotics" );
80
87
display.display ();
81
88
delay (2000 );
82
89
display.clearDisplay ();
90
+ display.setCursor (0 , 0 );
91
+ display.print (" WAIT START" );
92
+ display.display ();
83
93
84
94
// Connect to Wi-Fi network
85
95
WiFi.begin (ssid, password);
86
96
while (WiFi.status () != WL_CONNECTED) {
87
97
delay (1000 );
88
98
Serial.println (" Connecting to WiFi..." );
89
99
}
90
-
100
+
91
101
// Start UDP server
92
102
if (!udp.begin (localUdpPort)) {
93
103
Serial.println (" Failed to start UDP server" );
@@ -96,7 +106,7 @@ void setup() {
96
106
Serial.print (" Local IP address: " );
97
107
Serial.println (WiFi.localIP ());
98
108
Serial.print (" UDP server started on port " );
99
- Serial.println (localUdpPort);
109
+ Serial.println (localUdpPort);
100
110
101
111
Serial.println (" Waiting for 1212 to start loop." );
102
112
@@ -120,31 +130,43 @@ void setup() {
120
130
stepper1.setAcceleration (acceleration);
121
131
stepper2.setMaxSpeed (1500 );
122
132
stepper2.setAcceleration (1500 );
123
-
133
+
124
134
Serial.println (" Starting loop." );
135
+ display.clearDisplay ();
136
+ display.setCursor (0 , 10 );
137
+ display.print (" Count: " );
138
+ display.setCursor (80 , 10 );
139
+ display.print (objectCount);
140
+ display.display ();
125
141
}
126
142
127
143
128
144
void loop () {
129
145
unsigned long currentTime = millis ();
130
146
sensorData = analogRead (sensorPin);
131
- if (sensorData > 2500 && sensorData < 3800 && motor_enable)
147
+ if (sensorData > 1500 && sensorData < 3950 && motor_enable)
132
148
{
133
149
objectCount++;
134
150
delay (300 );
135
- }
136
- if (currentTime - previousDisplayTime >= displayInterval)
137
- {
138
151
Serial.print (" Sensor Data: " );
139
152
Serial.println (sensorData);
140
153
141
- display.clearDisplay ();
142
- display.setCursor (0 , 10 );
143
- display.print (" Object count : " );
154
+ display.clearDisplay ();
155
+ display.setCursor (0 , 10 );
156
+ display.print (" Count : " );
144
157
display.setCursor (80 , 10 );
145
158
display.print (objectCount);
146
159
display.display ();
160
+ }
161
+ if (currentTime - startMotorTime >= TOTAL_TIME_MS) {
162
+ // turn off motors
163
+ motor_enable = 0 ;
164
+ }
165
+ if (currentTime - previousDisplayTime >= displayInterval)
166
+ {
147
167
168
+ Serial.print (" Sensor Data: " );
169
+ Serial.println (sensorData);
148
170
// send points via udp
149
171
char message[32 ];
150
172
sprintf (message, " %lu" , objectCount);
@@ -156,48 +178,56 @@ void loop() {
156
178
157
179
previousDisplayTime = currentTime;
158
180
}
159
-
181
+ // if (currentTime - previousWiFiTime >= WiFiInterval)
182
+ // {
160
183
// Check for incoming UDP messages
161
184
int packetSize = udp.parsePacket ();
162
185
if (packetSize) {
163
186
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
164
187
udp.read (packetBuffer, UDP_TX_PACKET_MAX_SIZE);
165
188
packetBuffer[4 ] = 0 ;
166
- if (strcmp (packetBuffer, " 0202" ) == 0 )
189
+ /* if (strcmp(packetBuffer, "0202") == 0)
167
190
motor_enable = 0;
168
- else if (strcmp (packetBuffer, " 1212" ) == 0 )
169
- motor_enable = 1 ;
191
+ else */
192
+ if (strcmp (packetBuffer, " 1212" ) == 0 && startMotorTime == 0 ) {
193
+ motor_enable = 1 ;
194
+ startMotorTime = currentTime;
195
+ delay (200 );
196
+ }
170
197
Serial.print (" Received message: " );
171
198
Serial.println (packetBuffer);
172
199
}
173
-
200
+ // previousWiFiTime = currentTime;
201
+ // }
174
202
// check if motors should be turned off
175
203
if (motor_enable)
176
- {
204
+ { digitalWrite (DC_motor, LOW);
177
205
// Move the first motor continuously
178
- if (!stepper1.distanceToGo ())
206
+ if (!stepper1.distanceToGo ())
179
207
{
180
- stepper1.moveTo (-100000 );
208
+ stepper1.moveTo (-200000 );
181
209
}
182
210
stepper1.run ();
183
-
211
+ /*
184
212
// Move the second motor back and forth by 20 degrees
185
213
static bool direction = true;
186
214
static int currentPosition = 0;
187
215
const int targetPosition = direction ? currentPosition + 20 : currentPosition - 20;
188
216
stepper2.moveTo(targetPosition);
189
-
190
- if (abs (stepper2.distanceToGo ()) < 10 )
217
+
218
+ if (abs(stepper2.distanceToGo()) < 10)
191
219
{
192
220
currentPosition = targetPosition;
193
221
direction = !direction;
194
222
}
195
- stepper2.run ();
223
+ stepper2.run();
224
+ */
196
225
}
197
- else
198
- {
226
+ else
227
+ { digitalWrite (DC_motor, HIGH);
199
228
digitalWrite (enablePin, HIGH);
229
+
200
230
stepper1.disableOutputs ();
201
231
stepper1.disableOutputs ();
202
232
}
203
- }
233
+ }
0 commit comments