Components and supplies
HC-05 Bluetooth Module
LED (generic)
Test Connector, Tip Jack
Stepper Motor, Mini Step
Arduino UNO
Stepper motor driver board A4988
Apps and platforms
Arduino IDE
MIT app inventor
Project description
Code
sketch_nov16b.ino
arduino
1#include <SoftwareSerial.h> 2 3// define pin input and outputs 4#define buttonFlor1 2 5#define buttonFlor2 3 6#define buttonFlor3 4 7#define LEDFlor1 5 8#define LEDFlor2 6 9#define LEDFlor3 7 10#define dirPin 8 11#define stepPin 9 12#define TxD 10 13#define RxD 11 14#define stepsPerRevolution 200 15#define speed 2000 // in fact us delay between stepper pulses, higher number, slower speed 16int position = 0; //current position of elevator 17int newPosition = 0; 18int positionFlor1 = 0; 19int positionFlor2 = 860; 20int positionFlor3 = 1720; 21int z; 22 23SoftwareSerial bluetoothSerial(TxD, RxD); 24char c; 25 26void setup() { 27 // put your setup code here, to run once: 28pinMode (buttonFlor1, INPUT); 29pinMode (buttonFlor2, INPUT); 30pinMode (buttonFlor3, INPUT); 31pinMode (LEDFlor1, OUTPUT); 32pinMode (LEDFlor2, OUTPUT); 33pinMode (LEDFlor3, OUTPUT); 34pinMode(stepPin, OUTPUT); 35pinMode(dirPin, OUTPUT); 36 37bluetoothSerial.begin(9600); 38Serial.begin(9600); 39 40 41// Testing LEDs - turn on/off leds 1,2,3 42 digitalWrite(LEDFlor1, HIGH); 43 delay(1000); 44 digitalWrite(LEDFlor1, LOW); 45digitalWrite(LEDFlor2, HIGH); 46 delay(1000); 47 digitalWrite(LEDFlor2, LOW); 48digitalWrite(LEDFlor3, HIGH); 49 delay(1000); 50 digitalWrite(LEDFlor3, LOW); 51 52// Testing steppeer motor - one circle clockwise, one circle counterlockwise 53 digitalWrite(dirPin, LOW); 54 for (int i = 0; i < stepsPerRevolution; i++) { 55 // These four lines result in 1 step: 56 digitalWrite(stepPin, HIGH); 57 delayMicroseconds(speed); 58 digitalWrite(stepPin, LOW); 59 delayMicroseconds(speed); 60 } 61 62 digitalWrite(dirPin, HIGH); 63 for (int i = 0; i < stepsPerRevolution; i++) { 64 // These four lines result in 1 step: 65 digitalWrite(stepPin, HIGH); 66 delayMicroseconds(speed); 67 digitalWrite(stepPin, LOW); 68 delayMicroseconds(speed); 69 } 70// set zero position 71position = 0; 72digitalWrite(LEDFlor1, HIGH); 73} 74 75int GoToFlor1(int position, int positionFlor1) { 76 //Turn off all LEDs 77 digitalWrite(LEDFlor1, LOW); 78 digitalWrite(LEDFlor2, LOW); 79 digitalWrite(LEDFlor3, LOW); 80 81 if (position < positionFlor1) { 82 digitalWrite(dirPin, LOW); //define direction CV 83 for (int i = position; i < positionFlor1; i++) { 84 digitalWrite(stepPin, HIGH); 85 delayMicroseconds(speed); 86 digitalWrite(stepPin, LOW); 87 delayMicroseconds(speed); 88 } 89 } 90 91 else { 92 digitalWrite(dirPin, HIGH); //define direction CCV 93 z=(position-positionFlor1)/7; 94 95 for (int i = positionFlor1; i < position; i++) { 96 digitalWrite(stepPin, HIGH); 97 delayMicroseconds(speed); 98 digitalWrite(stepPin, LOW); 99 delayMicroseconds(speed); 100 101 if (i>(positionFlor1+z)){ 102 digitalWrite(LEDFlor1, HIGH); 103 } 104 if (i>(positionFlor1+(2*z))){ 105 digitalWrite(LEDFlor1, LOW); 106 } 107 if (i>(positionFlor1+(3*z))){ 108 digitalWrite(LEDFlor1, HIGH); 109 } 110 if (i>(positionFlor1+(4*z))){ 111 digitalWrite(LEDFlor1, LOW); 112 } 113 if (i>(positionFlor1+(5*z))){ 114 digitalWrite(LEDFlor1, HIGH); 115 } 116 if (i>(positionFlor1+(6*z))){ 117 digitalWrite(LEDFlor1, LOW); 118 } 119 120 121 122 123 } 124 } 125 126 //Turn on LEDFlor1 127 digitalWrite(LEDFlor1, HIGH); 128 129// Set new position 130return positionFlor1; 131 132} 133 134 135int GoToFlor2(int position, int positionFlor2) { 136 //Turn off all LEDs 137 digitalWrite(LEDFlor1, LOW); 138 digitalWrite(LEDFlor2, LOW); 139 digitalWrite(LEDFlor3, LOW); 140 141 142 if (position < positionFlor2) { 143 z=(positionFlor2-position)/7; 144 digitalWrite(dirPin, LOW); //define direction CV 145 for (int i = position; i < positionFlor2; i++) { 146 digitalWrite(stepPin, HIGH); 147 delayMicroseconds(speed); 148 digitalWrite(stepPin, LOW); 149 delayMicroseconds(speed); 150 151 if (i>z){ 152 digitalWrite(LEDFlor2, HIGH); 153 } 154 if (i>2*z){ 155 digitalWrite(LEDFlor2, LOW); 156 } 157 if (i>3*z){ 158 digitalWrite(LEDFlor2, HIGH); 159 } 160 if (i>4*z){ 161 digitalWrite(LEDFlor2, LOW); 162 } 163 if (i>5*z){ 164 digitalWrite(LEDFlor2, HIGH); 165 } 166 if (i>6*z){ 167 digitalWrite(LEDFlor2, LOW); 168 } 169 170 } 171 } 172 173 else { 174 digitalWrite(dirPin, HIGH); //define direction CCV 175 z=(position-positionFlor2)/7; 176 177 for (int i = positionFlor2; i < position; i++) { 178 digitalWrite(stepPin, HIGH); 179 delayMicroseconds(speed); 180 digitalWrite(stepPin, LOW); 181 delayMicroseconds(speed); 182 183 if (i>(positionFlor2+z)){ 184 digitalWrite(LEDFlor2, HIGH); 185 } 186 if (i>(positionFlor2+(2*z))){ 187 digitalWrite(LEDFlor2, LOW); 188 } 189 if (i>(positionFlor2+(3*z))){ 190 digitalWrite(LEDFlor2, HIGH); 191 } 192 if (i>(positionFlor2+(4*z))){ 193 digitalWrite(LEDFlor2, LOW); 194 } 195 if (i>(positionFlor2+(5*z))){ 196 digitalWrite(LEDFlor2, HIGH); 197 } 198 if (i>(positionFlor2+(6*z))){ 199 digitalWrite(LEDFlor2, LOW); 200 } 201 202 } 203 } 204 205 //Turn on LEDFlor2 206 digitalWrite(LEDFlor2, HIGH); 207 208 209// Set new position 210return positionFlor2; 211 212} 213 214int GoToFlor3(int position, int positionFlor3) { 215 //Turn off all LEDs 216 digitalWrite(LEDFlor1, LOW); 217 digitalWrite(LEDFlor2, LOW); 218 digitalWrite(LEDFlor3, LOW); 219 220 if (position < positionFlor3) { 221 222 z=(positionFlor3-position)/7; 223 224 225 226 digitalWrite(dirPin, LOW); //define direction CV 227 for (int i = position; i < positionFlor3; i++) { 228 digitalWrite(stepPin, HIGH); 229 delayMicroseconds(speed); 230 digitalWrite(stepPin, LOW); 231 delayMicroseconds(speed); 232 233 if (i>(position+z)){ 234 digitalWrite(LEDFlor3, HIGH); 235 } 236 if (i>(position+(2*z))){ 237 digitalWrite(LEDFlor3, LOW); 238 } 239 if (i>(position+(3*z))){ 240 digitalWrite(LEDFlor3, HIGH); 241 } 242 if (i>(position+(4*z))){ 243 digitalWrite(LEDFlor3, LOW); 244 } 245 if (i>(position+(5*z))){ 246 digitalWrite(LEDFlor3, HIGH); 247 } 248 if (i>(position+(6*z))){ 249 digitalWrite(LEDFlor3, LOW); 250 } 251 252 253 } 254 } 255 256 else { 257 digitalWrite(dirPin, HIGH); //define direction CCV 258 for (int i = positionFlor3; i < position; i++) { 259 digitalWrite(stepPin, HIGH); 260 delayMicroseconds(speed); 261 digitalWrite(stepPin, LOW); 262 delayMicroseconds(speed); 263 } 264 } 265 266 //Turn on LEDFlor3 267 digitalWrite(LEDFlor3, HIGH); 268 269 270// Set new position 271return positionFlor3; 272 273} 274 275void loop() { 276 // Check calls for elevator 277int CallFlor1 = digitalRead(buttonFlor1); 278int CallFlor2 = digitalRead(buttonFlor2); 279int CallFlor3 = digitalRead(buttonFlor3); 280 281 if (bluetoothSerial.available()){ 282 c = bluetoothSerial.read(); 283 if (c == '1'){ 284 newPosition = GoToFlor1(position, positionFlor1); 285 position = newPosition; 286 } 287 if (c == '2'){ 288 newPosition = GoToFlor2(position, positionFlor2); 289 position = newPosition; 290 } 291 if (c == '3'){ 292 newPosition = GoToFlor3(position, positionFlor3); 293 position = newPosition; 294 } 295 296 } 297 298 299 if (CallFlor1 == HIGH) { 300 newPosition = GoToFlor1(position, positionFlor1); 301 position = newPosition; 302 } 303 304 if (CallFlor2 == HIGH) { 305 newPosition = GoToFlor2(position, positionFlor2); 306 position = newPosition; 307 } 308 309 if (CallFlor3 == HIGH) { 310 newPosition = GoToFlor3(position, positionFlor3); 311 position = newPosition; 312 } 313} 314
Downloadable files
shema_elevator-edit2_wKOwas79d5.png
shema_elevator-edit2_wKOwas79d5.png
shema_elevator-edit2_wKOwas79d5.png
shema_elevator-edit2_wKOwas79d5.png
Comments
Only logged in users can leave comments
Dkovacic74
0 Followers
•0 Projects
Table of contents
Intro
8
0