safwan092

Project-ON7532

Mar 17th, 2021
453
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define SENSOR_PIN_1  6
  2. #define SENSOR_PIN_2  7
  3. #define STEPPER_PIN_1 9
  4. #define STEPPER_PIN_2 10
  5. #define STEPPER_PIN_3 11
  6. #define STEPPER_PIN_4 12
  7.  
  8. int step_number = 0;
  9.  
  10. void setup() {
  11.   Serial.begin(9600);
  12.   pinMode(SENSOR_PIN_1,  INPUT);
  13.   pinMode(SENSOR_PIN_2,  INPUT);
  14.   pinMode(STEPPER_PIN_1, OUTPUT);
  15.   pinMode(STEPPER_PIN_2, OUTPUT);
  16.   pinMode(STEPPER_PIN_3, OUTPUT);
  17.   pinMode(STEPPER_PIN_4, OUTPUT);
  18. }
  19.  
  20. void loop() {
  21.  
  22.   if (digitalRead(SENSOR_PIN_1) == 0 && digitalRead(SENSOR_PIN_2) == 1 ) {
  23.     OneStep(true);
  24.     delay(2);
  25.   }
  26.   else if (digitalRead(SENSOR_PIN_2) == 0 && digitalRead(SENSOR_PIN_1) == 1) {
  27.     OneStep(false);
  28.     delay(2);
  29.   }
  30.   else {
  31.     digitalWrite(STEPPER_PIN_1, LOW);
  32.     digitalWrite(STEPPER_PIN_2, LOW);
  33.     digitalWrite(STEPPER_PIN_3, LOW);
  34.     digitalWrite(STEPPER_PIN_4, LOW);
  35.   }
  36.  
  37. }//end of LOOP
  38.  
  39.  
  40. void OneStep(bool dir) {
  41.   if (dir) {
  42.     switch (step_number) {
  43.       case 0:
  44.         digitalWrite(STEPPER_PIN_1, HIGH);
  45.         digitalWrite(STEPPER_PIN_2, LOW);
  46.         digitalWrite(STEPPER_PIN_3, LOW);
  47.         digitalWrite(STEPPER_PIN_4, LOW);
  48.         break;
  49.       case 1:
  50.         digitalWrite(STEPPER_PIN_1, LOW);
  51.         digitalWrite(STEPPER_PIN_2, HIGH);
  52.         digitalWrite(STEPPER_PIN_3, LOW);
  53.         digitalWrite(STEPPER_PIN_4, LOW);
  54.         break;
  55.       case 2:
  56.         digitalWrite(STEPPER_PIN_1, LOW);
  57.         digitalWrite(STEPPER_PIN_2, LOW);
  58.         digitalWrite(STEPPER_PIN_3, HIGH);
  59.         digitalWrite(STEPPER_PIN_4, LOW);
  60.         break;
  61.       case 3:
  62.         digitalWrite(STEPPER_PIN_1, LOW);
  63.         digitalWrite(STEPPER_PIN_2, LOW);
  64.         digitalWrite(STEPPER_PIN_3, LOW);
  65.         digitalWrite(STEPPER_PIN_4, HIGH);
  66.         break;
  67.     }
  68.   } else {
  69.     switch (step_number) {
  70.       case 0:
  71.         digitalWrite(STEPPER_PIN_1, LOW);
  72.         digitalWrite(STEPPER_PIN_2, LOW);
  73.         digitalWrite(STEPPER_PIN_3, LOW);
  74.         digitalWrite(STEPPER_PIN_4, HIGH);
  75.         break;
  76.       case 1:
  77.         digitalWrite(STEPPER_PIN_1, LOW);
  78.         digitalWrite(STEPPER_PIN_2, LOW);
  79.         digitalWrite(STEPPER_PIN_3, HIGH);
  80.         digitalWrite(STEPPER_PIN_4, LOW);
  81.         break;
  82.       case 2:
  83.         digitalWrite(STEPPER_PIN_1, LOW);
  84.         digitalWrite(STEPPER_PIN_2, HIGH);
  85.         digitalWrite(STEPPER_PIN_3, LOW);
  86.         digitalWrite(STEPPER_PIN_4, LOW);
  87.         break;
  88.       case 3:
  89.         digitalWrite(STEPPER_PIN_1, HIGH);
  90.         digitalWrite(STEPPER_PIN_2, LOW);
  91.         digitalWrite(STEPPER_PIN_3, LOW);
  92.         digitalWrite(STEPPER_PIN_4, LOW);
  93.  
  94.  
  95.     }
  96.   }
  97.   step_number++;
  98.   if (step_number > 3) {
  99.     step_number = 0;
  100.   }
  101. }
RAW Paste Data