DFRobotShop Rover w/Xbee och Joystick (2 / 4 steg)
Steg 2: Koden för Arduino Uno(Joystick)
ord, jag bytte den för att skriva ut bokstäverna som rover skissen förstår.
CONST byte PIN_ANALOG_X = 0;
CONST byte PIN_ANALOG_Y = 1;
CONST int X_THRESHOLD_LOW = 450; /Jag har riktigt stora så det är mindre chans för contrains
CONST int X_THRESHOLD_HIGH = 550; analog feltolkning.
CONST int Y_THRESHOLD_LOW = 450;
CONST int Y_THRESHOLD_HIGH = 550;
int x_position;
int y_position;
int x_direction;
int y_direction;
void setup() {
Serial.BEGIN(9600);
}
Ogiltig loop () {
x_direction = 0;
y_direction = 0;
x_position = analogRead(PIN_ANALOG_X);
y_position = analogRead(PIN_ANALOG_Y);
om (x_position > X_THRESHOLD_HIGH) {
x_direction = 1;
} else om (x_position < X_THRESHOLD_LOW) {
x_direction = -1;
}
om (y_position > Y_THRESHOLD_HIGH) {
y_direction = 1;
} else om (y_position < Y_THRESHOLD_LOW) {
y_direction = -1;
}
om (x_direction == -1) {
om (y_direction == -1) {
Serial.println("left-Down");
} else om (y_direction == 0) {
Serial.println("a"); A är whats vilket gör det gå till vänster
} annat {
y_direction == 1
Serial.println("left-up");
}
} else om (x_direction == 0) {
om (y_direction == -1) {
Serial.println("s"); S är för omvänd
} else om (y_direction == 0) {
Serial.println("f"); F är för stopp--skickar detta tills något annat skickas
} annat {
y_direction == 1
Serial.println("w"); W är fram
}
} annat {
x_direction == 1
om (y_direction == -1) {
Serial.println("Right-Down");
} else om (y_direction == 0) {
Serial.println("d"); D är rätt
} annat {
y_direction == 1
Serial.println("Right-up");
}
}}