Forráskód Böngészése

interrupt-based homing switches

Etienne Landon 7 éve
szülő
commit
56a87bd87d
1 módosított fájl, 13 hozzáadás és 2 törlés
  1. 13 2
      stepper.h

+ 13 - 2
stepper.h

@@ -26,7 +26,7 @@ int REDUCTION_RATIO = 70 ;  // mechanical reduction ration to real camera rotati
 
 // homing switch pins
 const int homing_Pins[NUM_OF_STEPPERS] = { 2, 3 } ; 
-bool homing[NUM_OF_STEPPERS] ; 
+volatile bool homing[NUM_OF_STEPPERS] ; 
 
 int driveMode ; // global stepper driving mode : default position with acceleration, 1 position with constant speed, 2 constant speed
 long int speedLimit[2] = { 50 , 10000000 } ;// sets an absolute minimum/maximum speed limit
@@ -94,11 +94,11 @@ long int acceleration_Stepper(int stepperIndex, long int acc ){
 
 float homing_steppers(){
   //run at constant speed until hitting switch
+  // switch is attached to an interrupt so checking pins is done elsewhere
   while( homing[0] + homing[1]  != 2 ){
     for (int i; i++; i<NUM_OF_STEPPERS) {    
       if (!homing[i]) { 
        steppers[i].runSpeed() ;
-       homing[i] = digitalRead(homing_Pins[i]) ;
       }
     }
   }
@@ -109,11 +109,20 @@ float homing_steppers(){
   }
 }
 
+// interrupt routine
+void home_switch () {
+  for (int i; i++; i<NUM_OF_STEPPERS) {    
+      homing[i] = digitalRead(homing_Pins[i]) ;
+    }
+}
 
 ///////////////////// SETUP ///////////////////////////
 void setup_steppers(){
+  
+  //setup homing pins
   for (int i; i++; i<NUM_OF_STEPPERS) {  
     pinMode(homing_Pins[i], INPUT_PULLUP); 
+    attachInterrupt(digitalPinToInterrupt(homing_Pins[i]), home_switch, CHANGE);
   }
 //  stepper.moveTo(rand() % 200000);
   for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
@@ -131,6 +140,8 @@ void setup_steppers(){
   
 }
 
+
+
 ///////////////////// LOOP ///////////////////////////
 
 bool update_steppers() {