請問一下如何隨著推動搖桿的角度增加行走速度
我是給車子固定值
不曉得如何隨著電阻大小改變而改變速度
void loop () {
x_direction = 0;
y_direction = 0;
x_position = analogRead(PIN_ANALOG_X);
y_position = analogRead(PIN_ANALOG_Y);
if (x_position > X_THRESHOLD_HIGH) {
x_direction = 1;
} else if (x_position < X_THRESHOLD_LOW) {
x_direction = -1;
}
if (y_position > Y_THRESHOLD_HIGH) {
y_direction = 1;
digitalWrite(dir_a, HIGH);
} else if (y_position < Y_THRESHOLD_LOW) {
y_direction = -1;
}
if (x_direction == -1) {
if (y_direction == -1) {
//Serial.println("left-down");
digitalWrite(dir_a, LOW);
digitalWrite(dir_b, LOW);
delay(100);
analogWrite(pwm_a, 50);
analogWrite(pwm_b, 255);
delay(100);
} else if (y_direction == 0) {
//Serial.println("left");
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
delay(100);
analogWrite(pwm_a, 0);
analogWrite(pwm_b, 255);
delay(100);
} else {
// y_direction == 1
//Serial.println("left-up");
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
delay(100);
analogWrite(pwm_a, 50);
analogWrite(pwm_b, 255);
delay(100);
}
} else if (x_direction == 0) {
if (y_direction == -1) {
//Serial.println("down");
digitalWrite(dir_a, LOW);
digitalWrite(dir_b, LOW);
delay(100);
analogWrite(pwm_a, 255);
analogWrite(pwm_b, 255);
delay(100);
} else if (y_direction == 0) {
//Serial.println("centered");
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
delay(100);
analogWrite(pwm_a, 0);
analogWrite(pwm_b, 0);
delay(100);
} else {
// y_direction == 1
//Serial.println("up");
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
delay(100);
analogWrite(pwm_a, 255);
analogWrite(pwm_b, 255);
delay(100);
}
} else {
// x_direction == 1
if (y_direction == -1) {
//Serial.println("right-down");
digitalWrite(dir_a, LOW);
digitalWrite(dir_b, LOW);
delay(100);
analogWrite(pwm_a, 255);
analogWrite(pwm_b, 50);
delay(100);
} else if (y_direction == 0) {
//Serial.println("right");
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
delay(100);
analogWrite(pwm_a, 255);
analogWrite(pwm_b, 0);
delay(100);
} else {
// y_direction == 1
//Serial.println("right-up");
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, HIGH);
delay(100);
analogWrite(pwm_a, 255);
analogWrite(pwm_b, 50);
delay(100);
}
} |