-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoModbusRTUVFDController.ino
278 lines (246 loc) · 7.74 KB
/
ArduinoModbusRTUVFDController.ino
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*****************************************************************************
ArduinoModbusRTUVFDController
Poyraz Yildirim
Version 1.0
*****************************************************************************/
// ---------------------------------------------------------------------------
// Include the base required libraries
// ---------------------------------------------------------------------------
//#include <SensorModbusMaster.h>
#include <LiquidCrystal_I2C.h>
#include <JC_Button.h>
// ---------------------------------------------------------------------------
// MODBUS SETUP
// ---------------------------------------------------------------------------
// Define the sensor's modbus address
//byte modbusAddress = 1; // The sensor's modbus address, or SlaveID
//long modbusBaudRate = 38400; // The baud rate the sensor uses
// Create the stream instance
//HardwareSerial modbusSerial = Serial;
// Create the modbus instance
//modbusMaster modbus;
// ---------------------------------------------------------------------------
// LCD SETUP
// ---------------------------------------------------------------------------
//Define the I2C address
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ---------------------------------------------------------------------------
// BUTTONS SETUP
// ---------------------------------------------------------------------------
//Define Button and Led Pins
const byte BTN_FW_PIN (4),
BTN_RV_PIN (5),
BTN_START_PIN (7),
BTN_STOP_PIN (6),
LED_FW_PIN (10),
LED_RV_PIN (11),
LED_START_PIN (13),
LED_STOP_PIN (12),
POT_SPEED_PIN(A0);
//Bind the Buttons
Button btnFw(BTN_FW_PIN),
btnRv(BTN_RV_PIN),
btnStart(BTN_START_PIN),
btnStop(BTN_STOP_PIN);
//Define the global variables
unsigned long previousMillis = 0;
const long interval = 500;
//Define the filter variables for Potentiometer output
float const damping_coefficient = 0.08;
float filter_output = 0;
int mappedPotVal = 0;
int16_t statusWord;
int16_t controlWord;
int buttonState = 0;
int ledState = 0;
// ---------------------------------------------------------------------------
// Main setup function
// ---------------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
Serial.println("Serial Started");
// ---------------------------------------------------------------------------
// Modbus Setup
// ---------------------------------------------------------------------------
//Baudrate and parity
//Serial.begin(modbusBaudRate, SERIAL_8E1);
//Slave id, serial port used
//modbus.begin(modbusAddress, &Serial);
// ---------------------------------------------------------------------------
// LCD Setup
// ---------------------------------------------------------------------------
// initialize the lcd
lcd.init();
// Activate the backlight
lcd.backlight();
// ---------------------------------------------------------------------------
// Buttons Setup
// ---------------------------------------------------------------------------
// initialize the buttons
btnFw.begin();
btnRv.begin();
btnStart.begin();
btnStop.begin();
Serial.println("Setup finished");
pinMode(LED_FW_PIN, OUTPUT);
pinMode(LED_RV_PIN, OUTPUT);
pinMode(LED_START_PIN, OUTPUT);
pinMode(LED_STOP_PIN, OUTPUT);
}
// ---------------------------------------------------------------------------
// Main setup function
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Main loop function
// ---------------------------------------------------------------------------
void loop()
{
//Filter the raw potentiometer data and map it max/min motor speed
filter_output += (damping_coefficient * ((analogRead(POT_SPEED_PIN) - filter_output)));
mappedPotVal = map(filter_output, 0, 1022, 0, 1500);
Serial.println(mappedPotVal);
//Read the buttons
btnFw.read();
btnRv.read();
btnStart.read();
btnStop.read();
SM_Buttons();
SM_Main();
SM_LEDs();
}
void SM_Main() {
switch (buttonState) {
case 0: // INIT
buttonState = 1;
break;
case 1: //STOPPED
lcd.setCursor(0, 0);
lcd.print("PESS START TO ");
lcd.setCursor(0, 1);
lcd.print("SWITCH READY ");
break;
case 2: // READY
lcd.setCursor(0, 0);
lcd.print("READY TO START");
lcd.setCursor(0, 1);
lcd.print("RPM ");
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print(mappedPotVal);
break;
case 3: // RUN FW
lcd.setCursor(0, 0);
lcd.print("RUN ");
lcd.setCursor(9, 0);
lcd.print("FORWARD");
lcd.setCursor(0, 1);
lcd.print("RPM ");
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print(mappedPotVal);
break;
case 4: // RUN RV
lcd.setCursor(0, 0);
lcd.print("RUN ");
lcd.setCursor(9, 0);
lcd.print("REVERSE");
lcd.setCursor(0, 1);
lcd.print("RPM ");
lcd.setCursor(12, 1);
lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print(mappedPotVal);
break;
}
}
void SM_Buttons() {
if ( btnStop.wasPressed() && buttonState == 2)
{
Serial.println("Transition from Ready to Stop");
buttonState = 1;
}
if ( btnStop.wasPressed() && buttonState == 3)
{
Serial.println("Transition from Run FW to Stop");
buttonState = 1;
}
if ( btnStop.wasPressed() && buttonState == 4)
{
Serial.println("Transition from Run REV to Stop");
buttonState = 1;
}
if ( btnStart.wasPressed() && buttonState == 1 )
{
Serial.println("Transition from Stopped to Ready");
buttonState = 2;
}
if ( btnStart.isPressed() && btnFw.isPressed() && buttonState == 2)
{
Serial.println("Transition from Ready to Run FW");
buttonState = 3;
}
if ( btnStart.isPressed() && btnRv.isPressed() && buttonState == 2 )
{
Serial.println("Transition from Ready to Run REV");
buttonState = 4;
}
}
void SM_LEDs() {
unsigned long currentMillis = millis();
if (buttonState == 0) {
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(LED_FW_PIN, ledState);
digitalWrite(LED_RV_PIN, ledState);
digitalWrite(LED_START_PIN, ledState);
digitalWrite(LED_STOP_PIN, ledState);
Serial.println("Blinking");
}
}
if (buttonState == 1) {
digitalWrite(LED_FW_PIN, LOW);
digitalWrite(LED_RV_PIN, LOW);
digitalWrite(LED_START_PIN, LOW);
digitalWrite(LED_STOP_PIN, HIGH);
}
if (buttonState == 2) {
digitalWrite(LED_START_PIN, HIGH);
digitalWrite(LED_STOP_PIN, LOW);
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(LED_FW_PIN, ledState);
digitalWrite(LED_RV_PIN, ledState);
}
}
if (buttonState == 3) {
digitalWrite(LED_FW_PIN, HIGH);
digitalWrite(LED_RV_PIN, LOW);
digitalWrite(LED_START_PIN, HIGH);
digitalWrite(LED_STOP_PIN, LOW);
}
if (buttonState == 4) {
digitalWrite(LED_FW_PIN, LOW);
digitalWrite(LED_RV_PIN, HIGH);
digitalWrite(LED_START_PIN, HIGH);
digitalWrite(LED_STOP_PIN, LOW);
}
}