slik-dispenser/arduino/motor.ino

43 lines
1014 B
C++

int motor_speed = 1200;
int motor_lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
// Motor pins
// Pins 8 and 9 conflict with the display
int motor_pin1 = 10; // Blue - 28BYJ48 pin 1
int motor_pin2 = 11; // Pink - 28BYJ48 pin 2
int motor_pin3 = 12; // Yellow - 28BYJ48 pin 3
int motor_pin4 = 13; // Orange - 28BYJ48 pin 4
void motor_setup()
{
pinMode(motor_pin1, OUTPUT);
pinMode(motor_pin2, OUTPUT);
pinMode(motor_pin3, OUTPUT);
pinMode(motor_pin4, OUTPUT);
}
void motor_clockwise()
{
for (int i = 7; i >= 0; i--) {
motor_set_output(i);
delayMicroseconds(motor_speed);
}
}
void motor_anticlockwise()
{
for (int i = 0; i < 8; i++) {
motor_set_output(i);
delayMicroseconds(motor_speed);
}
}
void motor_set_output(int out)
{
digitalWrite(motor_pin1, bitRead(motor_lookup[out], 0));
digitalWrite(motor_pin2, bitRead(motor_lookup[out], 1));
digitalWrite(motor_pin3, bitRead(motor_lookup[out], 2));
digitalWrite(motor_pin4, bitRead(motor_lookup[out], 3));
}