Skip to content

Commit

Permalink
Create GamePad7688Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
anchi1216 committed Dec 29, 2015
1 parent e0c2ffb commit a9f0314
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions source_code/GamePad7688Sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const int motorIn1 = 2;
const int motorIn2 = 3;
const int motorIn3 = 4;
const int motorIn4 = 5;

String dpadLeft_press = "left|1";
String dpadLeft_release = "left|0";
String dpadUp_press = "up|1";
String dpadUp_release = "up|0";
String dpadRight_press = "right|1";
String dpadRight_release = "right|0";
String dpadDown_press = "down|1";
String dpadDown_release = "down|0";

const int DELAY = 1000;

void setup()
{
Serial1.begin(57600);
Serial.begin(9600);
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(motorIn3, OUTPUT);
pinMode(motorIn4, OUTPUT);
}

void loop()
{
String command="";
while (Serial1.available()) {
int v = Serial1.read();
Serial.println(v, DEC);
if ( v != -1) {
command += (char)v;
Serial.println(command);
if (command.equals(dpadLeft_press)) {
left();
delay(DELAY);
command = "";
} else if (command.equals(dpadLeft_release)) {
motorstop();
command = "";
delay(500);
} else if (command.equals(dpadUp_press)) {
forward();
command = "";
delay(DELAY);
} else if (command.equals(dpadUp_release)) {
motorstop();
command = "";
delay(500);
} else if (command.equals(dpadRight_press)) {
right();
command = "";
delay(DELAY);
} else if (command.equals(dpadRight_release)) {
motorstop();
command = "";
delay(500);
} else if (command.equals(dpadDown_press)) {
backward();
command = "";
delay(DELAY);
} else if (command.equals(dpadDown_release)) {
motorstop();
command = "";
delay(500);
}
}
}
}

void motorstop()
{
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW);
}

void forward()
{
digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, HIGH);
digitalWrite(motorIn4, LOW);
}

void backward()
{
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, HIGH);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, HIGH);
}

// Let right motor keep running, but stop left motor
void right()
{
digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW);
}

// Let left motor keep running, but stop right motor
void left()
{
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, HIGH);
digitalWrite(motorIn4, LOW);
}

0 comments on commit a9f0314

Please sign in to comment.