-
Notifications
You must be signed in to change notification settings - Fork 1
/
stepper motor sample
184 lines (161 loc) · 4.02 KB
/
stepper motor sample
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Included the required library
#include <Stepper.h>
#include <WiFi.h>
#include <AWS_IOT.h>
AWS_IOT hornbill;
// Define the Wifi and AWS parameters
char WIFI_SSID[]="...";
char WIFI_PASSWORD[]="....";
char HOST_ADDRESS[]="...";
char CLIENT_ID[]= "...";
char TOPIC_NAME[]= "...";
//Initial State
int status = WL_IDLE_STATUS;
int tick=0,msgCount=0,msgReceived = 0;
char payload[512];
char rcvdPayload[512];
int dirStatus;
void mySubCallBackHandler (char *topicName, int payloadLen, char *payLoad)
{
strncpy(rcvdPayload,payLoad,payloadLen);
rcvdPayload[payloadLen] = 0;
msgReceived = 1;
Serial.print("Message arrived [");
Serial.print(topicName);
Serial.print("] ");
for (int i = 0; i < payloadLen; i++)
{
Serial.print((char)rcvdPayload[i]); // Print payload content
}
//Test
Serial.print("dirStatus=");
dirStatus = (char) rcvdPayload[8]; // Extracting the controlling command
//from the Payload to Controlling mottor from AWS
Serial.println(dirStatus);
Serial.println();
}
int Pin1 = 13;
int Pin2 = 12;
int Pin3 = 14;
int Pin4 = 27;
int _step = 0;
int count=0;
void setup() {
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
pinMode(Pin4, OUTPUT);
// set the speed at 60 rpm:
//motor.setSpeed(60);
// initialize the serial port:
Serial.begin(115200);
delay(2000);
// Connection to Wifi and then AWS check
while (status != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
Serial.println(WIFI_SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// wait 5 seconds for connection:
delay(5000);
}
Serial.println("Connected to wifi");
if(hornbill.connect(HOST_ADDRESS,CLIENT_ID)== 0)
{
Serial.println("Connected to AWS");
delay(1000);
if(0==hornbill.subscribe(TOPIC_NAME,mySubCallBackHandler))
{
Serial.println("Subscribe Successfull");
}
else
{
Serial.println("Subscribe Failed, Check the Thing Name and Certificates");
while(1);
}
}
else
{
Serial.println("AWS connection failed, Check the HOST Address");
while(1);
}
delay(2000);
}
void loop() {
switch(_step){
case 0:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, HIGH);
break;
case 1:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, HIGH);
break;
case 2:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, LOW);
break;
case 3:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, LOW);
break;
case 4:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 5:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 6:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 7:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, HIGH);
break;
default:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
}
if(msgReceived == 1)
{
msgReceived = 0;
Serial.print("Received Message:");
Serial.println(rcvdPayload);
}
if(dirStatus == 49){
_step++;
}
else if(dirStatus == 50) {
_step--;
}
if(_step>7){
_step=0;
}
if(_step<0){
_step=7;
}
delay(1);
}