Skip to content

Instantly share code, notes, and snippets.

@eni23
Last active November 18, 2016 23:21
Show Gist options
  • Save eni23/33e3f347c8c38e88578436e6c4c09c17 to your computer and use it in GitHub Desktop.
Save eni23/33e3f347c8c38e88578436e6c4c09c17 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
int current_pos = 7;
int motor_speed = 20;
int motor_pins[4] = { 9, 6, 7, 8 };
bool positions[8][4] = {
{ 1, 0, 0, 0 },
{ 1, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 1 },
{ 0, 0, 0, 1 },
{ 1, 0, 0, 1 }
};
void led( bool state ) {
digitalWrite( 13, state );
}
void setup() {
Serial.begin( 115200 );
for ( int i = 0; i < 4; i++ ) {
pinMode( motor_pins[i], OUTPUT );
}
}
void motor_move( int pos, int steps = 1 ) {
if ( pos > 7 ) pos = 7;
if ( pos < 0 ) pos = 0;
for ( int i = 0; i < ( steps + 1 ); i++ ) {
led( 1 );
for ( int ii = 0; ii <4 ; ii++ ) {
digitalWrite( motor_pins[ii], positions[pos][ii] );
}
delay( motor_speed );
led( 0 );
}
}
void loop() {
if ( Serial.available() > 0 ) {
int key_pressed = Serial.read();
bool move = 0;
if ( key_pressed == 67 ) {
current_pos++;
if ( current_pos > 7 ) {
current_pos = 0;
}
move = 1;
}
if ( key_pressed == 68 ) {
current_pos--;
if ( current_pos < 0 ) {
current_pos = 7;
}
motor_move( current_pos, 1 );
move = 1;
}
if ( move ) {
motor_move( current_pos, 1 );
Serial.println( current_pos );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment