Masalah Skrip Arduino

Saat ini saya sedang mencoba membuat mobil RC di Arduino dan menghadapi masalah berikut, di mana saya tidak dapat mereset Pelindung Driver Motor L298P (Dengan Keyes) untuk menghentikan pergerakan motor (Hanya menggunakan satu motor di port Motor B).

Pada dasarnya motor menempel satu arah berputar dan tidak berhenti ketika posisi JoyStick di-reset.

Inilah kode yang saya gunakan saat ini. (Juga Termasuk cobaan menyedihkanku).

#include <Servo.h>
Servo myservo;

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 5, 6, 7, 8);

int JoyStick_X = 0; //Analog
int JoyStick_Y = 1; //Analog
int JoyStick_Z = 1; //Digital

int SpeedPin = 11;
int Direction = 13;

void setup(void) {
  myservo.attach(9);

  lcd.begin(16, 2);
  lcd.clear();

  pinMode(JoyStick_Z, INPUT_PULLUP);

  pinMode(Direction, OUTPUT);


  Serial.begin(9600);
}

void loop(void) {
  int x, y, z;
  x = analogRead(JoyStick_X);
  y = analogRead(JoyStick_Y);
  z = digitalRead(JoyStick_Z);

  lcd.setCursor(0, 0);
  lcd.print("Car Status");

  lcd.setCursor(0, 1);
  lcd.print("Spd:");
  lcd.print(x);
  /* if (z == 0) {
     lcd.print("High");
    }
    else if (z == 0 && x < 250) {
     lcd.print("RHgh");
    }
    else if (x > 510 && x < 530) {
     lcd.print("No  ");
    }
    else if (x > 530 && x < 730) {
     lcd.print("Low ");
    }
    else if (x > 730) {
     lcd.print("Med ");
    }
    else if (x < 510 && x > 250) {
     lcd.print("RLow");
    }
    else if (x < 250) {
     lcd.print("RMed");
    }*/

  lcd.print(" ");
  lcd.setCursor(10, 1);
  lcd.print("Trn:");

  if (y < 500) {
    lcd.print("L ");
  }
  else if (525 > y && y > 500) {
    lcd.print("-");
  }
  else if (y > 525) {
    lcd.print("R ");
  }

  lcd.setCursor(11, 0);
  lcd.print("Bst:");
  if (z == 0) {
    lcd.print("Y");
  }
  else {
    lcd.print("N");
  }

  myservo.write(40 + y * 8.7890625 / 100);

    if (x > 530) {
      digitalWrite(Direction, HIGH);
    }
    else if (x < 510) {
      digitalWrite(Direction, LOW);
    }
    if (x > 510 && x < 530) {
      digitalWrite(0, 0);
    }


  //int speed = ((0.520408 * x ) - 275.816326 );

  analogWrite(SpeedPin, 255);

  /*int value = 255;
    digitalWrite(M1,LOW);

    analogWrite(E1, value);*/


/*var n;
  switch (n)
  {
    case 1:
      digitalWrite(Direction, HIGH);
      break;
    case 2:
      digitalWrite(Direction, LOW);
      break;
    default:
      digitalWrite(0, 0);
  }*/
}

person Kunal Gehlot    schedule 13.07.2017    source sumber
comment
Dan di mana Anda mencoba menghentikan motor?   -  person gre_gor    schedule 13.07.2017


Jawaban (1)


Saya pikir Anda kehilangan analogWrite(SpeedPin, 0); di suatu tempat dalam kode Anda. Karena SpeedPin sepertinya adalah cara Anda mengontrol kecepatan, Anda perlu menulis angka nol untuk berhenti.

person TomServo    schedule 13.07.2017