Friday 24 August 2012

CNC linear rails testing

Down at BuildBrighton tonight, fellow PIC-programmers were in short supply, so with reluctance we had to crack open an Arduino Duemilanove and learn some crazy Arduino coding. It turns out it's not as difficult as it looks (but it does still make you feel a bit dirty).

To get things working, we just wanted to be able to turn a stepper motor clockwise and anti-clockwise. Here's the code we came up with:



int state=0;
int dir=0;

void setup(){
     pinMode(1, INPUT_PULLUP);
     pinMode(2, INPUT_PULLUP);
     pinMode(3, OUTPUT);
     pinMode(4, OUTPUT);
     pinMode(5, OUTPUT);
     pinMode(6, OUTPUT);

}

void loop(){
     //read the pushbutton value into a variable
     int inputVal1 = digitalRead(1);
     int inputVal2 = digitalRead(2);
     
     // Keep in mind the pullup means the pushbutton's
     // logic is inverted. It goes HIGH when it's open,
     // and LOW when it's pressed.
     if (inputVal1 == LOW) {
          // turn the motor clockwise
          dir=1;
     }else if(inputVal2 == LOW) {
          // turn the motor anticlockwise
          dir=-1;
     }else{
      // stop turning the motor
      dir=0;
     }
     
     if(dir!=0){
               // move the motor
               state+=dir;
               if(state<0){state=7;}
               if(state>7){state=0;}
               
               switch(state){
                     case 0:
                     // energise coil A
                     digitalWrite(3, HIGH);
                     digitalWrite(4, LOW);
                     digitalWrite(5, LOW);
                     digitalWrite(6, LOW);
                    
                     break;
                    
                     case 1:
                     // energise coils A+B
                     digitalWrite(3, HIGH);
                     digitalWrite(4, HIGH);
                     digitalWrite(5, LOW);
                     digitalWrite(6, LOW);
                     break;
                    
                     case 2:
                     // energise coil B
                     digitalWrite(3, LOW);
                     digitalWrite(4, HIGH);
                     digitalWrite(5, LOW);
                     digitalWrite(6, LOW);
                     break;
                    
                     case 3:
                     // energise coils B+C
                     digitalWrite(3, LOW);
                     digitalWrite(4, HIGH);
                     digitalWrite(5, HIGH);
                     digitalWrite(6, LOW);
                     break;
                    
                     case 4:
                     // energise coil C
                     digitalWrite(3, LOW);
                     digitalWrite(4, LOW);
                     digitalWrite(5, HIGH);
                     digitalWrite(6, LOW);
                     break;
                    
                     case 5:
                     // energise coils C+D
                     digitalWrite(3, LOW);
                     digitalWrite(4, LOW);
                     digitalWrite(5, HIGH);
                     digitalWrite(6, HIGH);
                     break;
                    
                     case 6:
                     // energise coil D
                     digitalWrite(3, LOW);
                     digitalWrite(4, LOW);
                     digitalWrite(5, LOW);
                     digitalWrite(6, HIGH);
                     break;
                                             
                     case 7:
                     // energise coils D+A
                     digitalWrite(3, HIGH);
                     digitalWrite(4, LOW);
                     digitalWrite(5, LOW);
                     digitalWrite(6, HIGH);
                     break;
                    
               }
               
               delay(1);
     }
     
}




It's a simple state machine - when the motor is turning clockwise, we energise the coils in sequence 1...2...3... etc, when running anti-clockwise we go 7...6...5.... etc

Doing this allows us to quickly and easily make the motor run by pulling an input pin low (pull-up resistors mean the inputs are always high with no input on them).


Here's a video showing the motor in action:

1 comment:

  1. Thank you for your information. It's really nice and useful for me.

    ReplyDelete