Skip to content

Instantly share code, notes, and snippets.

@taesamja
Last active November 25, 2016 06:15
Show Gist options
  • Save taesamja/bf9088564a41a6dd265130da0dcc6a6e to your computer and use it in GitHub Desktop.
Save taesamja/bf9088564a41a6dd265130da0dcc6a6e to your computer and use it in GitHub Desktop.
int chA_dir = 12;
int chA_speed = 3;
int chA_stop = 9;
int chB_dir = 13;
int chB_speed = 11;
int chB_stop = 8;
//speed range : 85 ~ 255
int motor_speed = 100;
void setup() {
pinMode(chA_dir,OUTPUT);
pinMode(chA_speed,OUTPUT);
pinMode(chA_stop,OUTPUT);
pinMode(chB_dir,OUTPUT);
pinMode(chB_speed,OUTPUT);
pinMode(chB_stop,OUTPUT);
Serial.begin(9600);
Serial.println("Speed range : 85 ~ 255 ");
Serial.println("F:forward, B:back, L:left, R:right, S:stop");
Serial.println("Input method : ex)F85 or f85 ~ F255 or f255");
}
//input method : ex)F100 or f100
void loop() {
char inByte;
if(Serial.available()>0)
{
inByte = Serial.read();
motor_speed = Serial.parseInt();
Serial.print("inByte = ");Serial.println(inByte);
Serial.print("motor_speed = ");Serial.println(motor_speed);
if(inByte == 'F' || inByte == 'f') dc_motor_f(motor_speed);
if(inByte == 'B' || inByte == 'b') dc_motor_b(motor_speed);
if(inByte == 'L' || inByte == 'l') dc_motor_l(motor_speed);
if(inByte == 'R' || inByte == 'r') dc_motor_r(motor_speed);
if(inByte == 'S' || inByte == 's') dc_motor_s();
}
}
void dc_motor_f(int num) // 전진
{
digitalWrite(chA_dir,HIGH); digitalWrite(chB_dir,HIGH);
digitalWrite(chA_stop,LOW); digitalWrite(chB_stop,LOW);
analogWrite(chA_speed,num); analogWrite(chB_speed,num);
}
void dc_motor_b(int num) // 후진
{
digitalWrite(chA_dir,LOW); digitalWrite(chB_dir,LOW);
digitalWrite(chA_stop,LOW); digitalWrite(chB_stop,LOW);
analogWrite(chA_speed,num); analogWrite(chB_speed,num);
}
void dc_motor_l(int num) // 좌회전
{
digitalWrite(chA_dir,HIGH); digitalWrite(chB_dir,LOW);
digitalWrite(chA_stop,LOW); digitalWrite(chB_stop,LOW);
analogWrite(chA_speed,num); analogWrite(chB_speed,num);
}
void dc_motor_r(int num) // 우회전
{
digitalWrite(chA_dir,LOW); digitalWrite(chB_dir,HIGH);
digitalWrite(chA_stop,LOW); digitalWrite(chB_stop,LOW);
analogWrite(chA_speed,num); analogWrite(chB_speed,num);
}
void dc_motor_s() // 정지
{
digitalWrite(chA_stop,HIGH); digitalWrite(chB_stop,HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment