瞭解
我量測出來的值大約在350左右
順便請問一下如何隨著推動搖桿的角度增加行走速度
因為我是給車子固定值
不曉得如何隨著電組大小改變而改變速度
改一改反而跑得怪怪的
const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;
const int X_THRESHOLD_LOW = 330;
const int X_THRESHOLD_HIGH = 370;
const int Y_THRESHOLD_LOW = 330;
const int Y_THRESHOLD_HIGH = 370;
int x_position;
int y_position;
int x_direction;
int y_direction;
int pwm_a = 3;
int pwm_b = 11;
int dir_a = 12;
int dir_b = 13;
void setup() {
Serial.begin(9600);
pinMode(pwm_a, OUTPUT);
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
}
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);
}
}
這是我改寫的程式
pwm_a 跟 pwm_b 是我的固定值
不曉得怎麼藉由電阻值轉換改變速度 |