April 6: STEPS
April 6, 2011
In today’s code, we’re using the arduino’s Stepper Library and arrays to make our stepper motors move according to fixed patterns.
This afternoon’s PRO CHALLENGE:
Store 5 different patterns in arrays. Make the code choose randomly which patterns to follow.
For this afternoon’s code, click “Read More”
// using arrays to store patterns
#include <Stepper.h>
#define NUM_STEPS 48
int steps1[11] = { 20, -5, 30, 20, 10, -10, 2, -10, 2, -10, 0
};
int delays1[11] = { 5, 20, 5, 8, 10, 3, 10, 3, 10, 3, 0
};
int steps2[5] = { 10, -20, 5, -30, 0
};
int delays2[5] = { 50, 40, 100, 23, 0
};
Stepper stepper(NUM_STEPS, 2,3,4,5);
void setup(){
stepper.setSpeed(180);
}
void loop(){
play_arrays(steps1, delays1); delay(500); play_arrays(steps2, delays2); delay(500);
}
void play_arrays(int step_array[], int delay_array[]){
int i;
for(i=0; i<1000; i++){ // arbitrariy decide 1000 is larget sequence we will play
if(step_array[i] == 0 || delay_array[i] == 0){
break;
}
step_motor(step_array[i], delay_array[i]);
}
}
void step_motor(int step_count, int step_delay){
int counter;
int turn_amt;
int turn_dir;
turn_amt = abs(step_count); // get post number of steps
if(step_count > 0){ // get direction to go, either -1 or 1
turn_dir = 1;
}
else{
turn_dir = -1;
}
for(counter = 0; counter < turn_amt; counter++){ // loop num of steps times...
stepper.step(turn_dir); // step either CW/CCW dep. on 1/-1
delay(step_delay);
}
}
Advertisement
No comments yet