我正在研究一个通过 arduino uno 和 L298N 控制旧遥控汽车直流电机的项目。我有电机工作,但想包括一个故障保护装置,如果蓝牙超出范围,它会停止电机,这样它就不会继续行驶直到它崩溃。代码从下面开始,我尝试在最底部进行故障保护。
//This begins the actual motor stuff
int forwards = (PS4.getAnalogButton(R2)); // Read R2 Button for forward
int backwards = (PS4.getAnalogButton(L2)); // Read L2 Button for backwards
forwards = map(forwards, 0, 255, 0, 255); //Need to figure out how to add offset to remove buzzing
//noise from motor.
backwards = map(backwards, 0, 255, 0, 255);
//This is for Rear motor Forwards
if ((PS4.getAnalogButton( R2 )) > 0 ) {
analogWrite(enA, forwards );
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);}
//This is for Rear motor Backwards
if ((PS4.getAnalogButton( L2 )) > 0 ) {
analogWrite(enA, backwards );
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);}
//This is so that if neither L2/R2 is pressed the motor does nothing.
if (PS4.getAnalogButton( R2 ) == 0 &&
PS4.getAnalogButton( L2 ) == 0 ){
analogWrite(enA, 255);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW); }
//This is for front l/r Motor
int steer = (PS4.getAnalogHat(LeftHatX) );
steer = map(steer, 0, 255, 255, 255);
//This is for front motor turning left
//code for analog for l/r
if ((PS4.getAnalogHat(LeftHatX)) >135 ){
analogWrite(enB, steer);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);}
else if ((PS4.getAnalogHat(LeftHatX)) <115 ){
analogWrite(enB, steer);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH); }
else{
analogWrite(enB, 255);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW); }
//This is code to turn off motor if controller is out of range and disconnect still a wip
//if (PS4.disconnect())return = true;{ //this comes back as an error "could not convert
'PS4.PS4BT::<anonymous>.BTHID::disconnect()' from 'void' to 'bool' "
//analogWrite(enA, 255);
//digitalWrite(in1, LOW);
//digitalWrite(in2, LOW);}
}}