Skip to content

Instantly share code, notes, and snippets.

@desireesantos
Created March 28, 2014 02:36
Show Gist options
  • Save desireesantos/9824038 to your computer and use it in GitHub Desktop.
Save desireesantos/9824038 to your computer and use it in GitHub Desktop.
int direction= 3;
int step = 2;
int velocity = 5;
int number = 0;
char command;
#define FORWARD LOW
#define BACKWARD HIGH
void setup(){
pinMode(direction,OUTPUT);
pinMode(step,OUTPUT);
Serial.begin(9600);
}
void loop(){
number = Serial.available();
if(number > 0){
// Serial.println("Available: " + number);
command = Serial.read();
// Serial.println(command, DEC);
if(command == 'B'){
moveHeadBackward();
}
if (command == 'F')
{
moveHeadForward();
}
if (command == 'V')
{
setupVelocity();
}
// Serial.flush();
}
}
void resetDrive(){
moveHeadBackward();
setupDirection(FORWARD);
}
void moveHeadBackward(){
setupDirection(BACKWARD);
for(int x = 0; x< 80; x++){
digitalWrite(step,HIGH);
digitalWrite(step,LOW);
delay(velocity);
}
}
void moveHeadForward(){
setupDirection(FORWARD);
for(int x = 0; x< 80; x++){
digitalWrite(step,HIGH);
digitalWrite(step,LOW);
delay(velocity);
}
}
void setupVelocity(){
int newVelocity = Serial.read();
// Serial.println("newVelocity: " + newVelocity);
velocity = newVelocity;
}
void setupDirection(int value){
digitalWrite(direction,value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment