amiman99 Posted December 24, 2018 Share Posted December 24, 2018 Hi, Can someone help me with my Arduino code? I need my main loop to loop or do 12 times only and then stop. I know it nees some kind of FOR statement, but I don't know where to put it. Thanks ========================================== //#define STEPPER_PIN_1 10#define STEPPER_PIN_2 11#define STEPPER_PIN_3 12#define STEPPER_PIN_4 13#define RELAY_PIN_9 9 int step_number = 0;void setup() {pinMode(STEPPER_PIN_1, OUTPUT);pinMode(STEPPER_PIN_2, OUTPUT);pinMode(STEPPER_PIN_3, OUTPUT);pinMode(STEPPER_PIN_4, OUTPUT);pinMode(RELAY_PIN_9, OUTPUT); } void loop() {for(int a = 0; a < 1400; a++){ OneStep(false); delay(2); if (a == 0){ delay(500); digitalWrite(STEPPER_PIN_1, LOW); digitalWrite(STEPPER_PIN_2, LOW); digitalWrite(STEPPER_PIN_3, LOW); digitalWrite(STEPPER_PIN_4, LOW); digitalWrite(RELAY_PIN_9, HIGH); delay(1000); digitalWrite(RELAY_PIN_9, LOW); delay(500);} }} void OneStep(bool dir){ if(dir){switch(step_number){ case 0: digitalWrite(STEPPER_PIN_1, HIGH); digitalWrite(STEPPER_PIN_2, LOW); digitalWrite(STEPPER_PIN_3, LOW); digitalWrite(STEPPER_PIN_4, LOW); break; case 1: digitalWrite(STEPPER_PIN_1, LOW); digitalWrite(STEPPER_PIN_2, HIGH); digitalWrite(STEPPER_PIN_3, LOW); digitalWrite(STEPPER_PIN_4, LOW); break; case 2: digitalWrite(STEPPER_PIN_1, LOW); digitalWrite(STEPPER_PIN_2, LOW); digitalWrite(STEPPER_PIN_3, HIGH); digitalWrite(STEPPER_PIN_4, LOW); break; case 3: digitalWrite(STEPPER_PIN_1, LOW); digitalWrite(STEPPER_PIN_2, LOW); digitalWrite(STEPPER_PIN_3, LOW); digitalWrite(STEPPER_PIN_4, HIGH); break;} }else{ switch(step_number){ case 0: digitalWrite(STEPPER_PIN_1, LOW); digitalWrite(STEPPER_PIN_2, LOW); digitalWrite(STEPPER_PIN_3, LOW); digitalWrite(STEPPER_PIN_4, HIGH); break; case 1: digitalWrite(STEPPER_PIN_1, LOW); digitalWrite(STEPPER_PIN_2, LOW); digitalWrite(STEPPER_PIN_3, HIGH); digitalWrite(STEPPER_PIN_4, LOW); break; case 2: digitalWrite(STEPPER_PIN_1, LOW); digitalWrite(STEPPER_PIN_2, HIGH); digitalWrite(STEPPER_PIN_3, LOW); digitalWrite(STEPPER_PIN_4, LOW); break; case 3: digitalWrite(STEPPER_PIN_1, HIGH); digitalWrite(STEPPER_PIN_2, LOW); digitalWrite(STEPPER_PIN_3, LOW); digitalWrite(STEPPER_PIN_4, LOW); } }step_number++; if(step_number > 3){ step_number = 0; }} ================================================ Quote Link to comment Share on other sites More sharing options...
Osgeld Posted December 24, 2018 Share Posted December 24, 2018 #define STEPPER_PIN_1 10 #define STEPPER_PIN_2 11 #define STEPPER_PIN_3 12 #define STEPPER_PIN_4 13 #define RELAY_PIN_9 9 int step_number = 0; int loop_number = 0; void setup() { pinMode(STEPPER_PIN_1, OUTPUT); pinMode(STEPPER_PIN_2, OUTPUT); pinMode(STEPPER_PIN_3, OUTPUT); pinMode(STEPPER_PIN_4, OUTPUT); pinMode(RELAY_PIN_9, OUTPUT); } void loop() { if loop_number < 12 { for(int a = 0; a < 1400; a++) { OneStep(false); delay(2); if (a == 0) { delay(500); digitalWrite(STEPPER_PIN_1, LOW); digitalWrite(STEPPER_PIN_2, LOW); digitalWrite(STEPPER_PIN_3, LOW); digitalWrite(STEPPER_PIN_4, LOW); digitalWrite(RELAY_PIN_9, HIGH); delay(1000); digitalWrite(RELAY_PIN_9, LOW); delay(500); } } loop_number++; } } Quote Link to comment Share on other sites More sharing options...
amiman99 Posted December 24, 2018 Author Share Posted December 24, 2018 Thanks Osgeld, I changed the: if loop_number < 12 to if (loop_number < 12) and it worked, Thanks a lot! Quote Link to comment Share on other sites More sharing options...
Osgeld Posted December 25, 2018 Share Posted December 25, 2018 (edited) welcome (and yea sorry about that I often forget the () after an if, and I didnt have arduino's software on this machine to nag me) you could use a for loop in void setup() and leave loop empty to do the same thing you have to have a void loop() but you don't have to have anything in it Edited December 25, 2018 by Osgeld Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.