Arduino Uno ile bluetooth araç kontrolü kod sorunu
  • arduino uno ile kontrol edeceğimiz iki tekerlek iki dc motordan oluşan sistemimiz mevcut. L298 motor shieldi ile motorları sürdüreceğiz. Android telefonun bluetoothu ile kontrol vermeye çalışıyoruz. Ancak bluetooth ile kontrol sağlayamıyoruz. Motorların ise bir tanesi kendi kendine aralıklarla dönüyor. Kodumuz aşağıdaki gibidir. Sorunu anlayamadık.Ne yapamız gerekiyor?


    //Keyboard Controls:
    //
    // 1 -Motor 1 Left
    // 2 -Motor 1 Stop
    // 3 -Motor 1 Right
    //
    // 4 -Motor 2 Left
    // 5 -Motor 2 Stop
    // 6 -Motor 2 Right

    // Declare L298N Dual H-Bridge Motor Controller directly since there is not a library to load.

    // Motor 1
    int dir1PinA = 2;
    int dir2PinA = 3;
    int speedPinA = 9; // Needs to be a PWM pin to be able to control motor speed

    // Motor 2
    int dir1PinB = 4;
    int dir2PinB = 7;
    int speedPinB = 12; // Needs to be a PWM pin to be able to control motor speed
    String command = "55555"; // Stores response of the HC-06 Bluetooth device
    float floatVal=123.234;
    char charVal[10]; //temporarily holds data from vals
    String stringVal = "";
    float a=21.25;

    #include
    #include
    SoftwareSerial mySerial(10,11); // RX, TX

    String message;



    void setup() {
    pinMode(dir1PinA,OUTPUT);
    pinMode(dir2PinA,OUTPUT);
    pinMode(speedPinA,OUTPUT);
    pinMode(dir1PinB,OUTPUT);
    pinMode(dir2PinB,OUTPUT);
    pinMode(speedPinB,OUTPUT);

    // Open serial communications:
    Serial.begin(9600);
    Serial.println("Type AT commands!");

    // The HC-06 defaults to 9600 according to the datasheet.
    mySerial.begin(9600);

    }

    void loop() {
    // Read device output if available.
    if (mySerial.available()) {
    while(mySerial.available()) { // While there is more to be read, keep reading.
    command += (char)mySerial.read();
    }

    Serial.println(command);
    command = ""; // No repeats
    }

    // Read user input if available.
    if (Serial.available()){
    delay(10); // The delay is necessary to get this working!
    int inByte=mySerial.write(Serial.read());
    switch (inByte) {

    //______________Motor 1______________

    case 'U': // Motor 1,2 Forward
    analogWrite(speedPinA, 255);//Sets speed variable via PWM
    digitalWrite(dir1PinA, LOW);
    digitalWrite(dir2PinA, HIGH);
    Serial.println("Motor 1 Forward"); // Prints out ?Motor 1 Forward? on the serial monitor
    Serial.println(" "); // Creates a blank line printed on the serial monitor
    analogWrite(speedPinB, 255);
    digitalWrite(dir1PinB, LOW);
    digitalWrite(dir2PinB, HIGH);
    Serial.println("Motor 2 Forward");
    Serial.println(" ");
    break;

    case 'S': // Motor 1,2 Stop (Freespin)
    analogWrite(speedPinA, 0);
    digitalWrite(dir1PinA, LOW);
    digitalWrite(dir2PinA, HIGH);
    Serial.println("Motor 1 Stop");
    Serial.println(" ");
    // Motor 1 Stop (Freespin)
    analogWrite(speedPinB, 0);
    digitalWrite(dir1PinB, LOW);
    digitalWrite(dir2PinB, HIGH);
    Serial.println("Motor 2 Stop");
    Serial.println(" ");
    break;

    case 'L': // Motor 1 Reverse
    analogWrite(speedPinA, 255);
    digitalWrite(dir1PinA, HIGH);
    digitalWrite(dir2PinA, LOW);
    Serial.println("Motor 1 Reverse");
    Serial.println(" ");
    analogWrite(speedPinB, 255);
    digitalWrite(dir1PinB, LOW);
    digitalWrite(dir2PinB, HIGH);
    Serial.println("Motor 2 Forward");
    Serial.println(" ");
    break;

    case 'R': // Motor 1 Reverse
    analogWrite(speedPinA, 255);
    digitalWrite(dir1PinA, LOW);
    digitalWrite(dir2PinA, HIGH);
    Serial.println("Motor 1 Forward");
    Serial.println(" ");
    analogWrite(speedPinB, 255);
    digitalWrite(dir1PinB, HIGH);
    digitalWrite(dir2PinB, LOW);
    Serial.println("Motor 2 Reverse");
    Serial.println(" ");
    break;



    default:
    // turn all the connections off if an unmapped key is pressed:
    for (int thisPin = 2; thisPin < 11; thisPin++) {
    digitalWrite(thisPin, LOW);
    }
    }
    }
    }
  • ben çalışan bir kod yollayım sen motor ayarlarını kendine göre düzenle

  • #include

    SoftwareSerial myBluetooth(0, 1); // RX, TX bacak bağlantısı ters olacak
    int deger;
    int hiz,hiz2=60;

    //Motor A
    int dir1PinA=13;
    int dir2PinA=12;
    int speedPinA=10;

    //Motor B
    int dir1PinB=11;
    int dir2PinB=8;
    int speedPinB=9;

    unsigned long time;
    int speed;
    int dir;

    void setup() {
    //bluetooth ayarları
    myBluetooth.begin(9600);
    myBluetooth.println("Bluetooth Mode:ON / 1 ve 0?ı tuslayin.");

    //motor pin ayarı
    pinMode(dir1PinA,OUTPUT);
    pinMode(dir2PinA,OUTPUT);
    pinMode(speedPinA,OUTPUT);
    pinMode(dir1PinB,OUTPUT);
    pinMode(dir2PinB,OUTPUT);
    pinMode(speedPinB,OUTPUT);
    Serial.begin(9600);
    }

    void loop()
    {
    if (myBluetooth.available())
    {
    deger=myBluetooth.read();

    if(deger=='1')
    {hiz=hiz2;
    motor(hiz,hiz,1,1);Serial.println("Motor Geri");
    delay(200); hiz=0;
    }
    if (deger=='0')
    {hiz=hiz2;
    motor(hiz,hiz,0,0);Serial.println("Motor ileri");
    delay(200); hiz=0;
    }
    if (deger=='2')
    {hiz=hiz2;
    motor(hiz,hiz,0,1);Serial.println("Motor sola");
    delay(120); hiz=0;
    }
    if (deger=='3')
    {hiz=hiz2;
    motor(hiz,hiz,1,0);Serial.println("Motor saga");
    delay(120); hiz=0;
    }
    if (deger=='4')
    {
    hiz=0;
    motor(hiz,hiz,0,0);Serial.println("Motor Durdu");
    }
    if (deger=='5')
    {
    if (hiz<255) hiz++; Serial.println(hiz);<br /> // myBluetooth.print("hiz="); myBluetooth.println(hiz2);

    }
    if (deger=='6')
    {
    if (hiz>30) hiz--; Serial.println(hiz);
    // myBluetooth.print("hiz="); myBluetooth.println(hiz2);

    }
    analogWrite(speedPinA,hiz);
    analogWrite(speedPinB,hiz);
    }
    }

    void motor(int solmotor,int sagmotor,int solyon,int sagyon)
    {
    digitalWrite(dir1PinA,LOW);
    digitalWrite(dir2PinA,HIGH);
    digitalWrite(dir1PinB,LOW);
    digitalWrite(dir2PinB,HIGH);

    if (sagyon==1)
    {
    digitalWrite(dir1PinB,HIGH);
    digitalWrite(dir2PinB,LOW);
    }
    if (solyon==1)
    {
    digitalWrite(dir1PinA,HIGH);
    digitalWrite(dir2PinA,LOW);
    }
    analogWrite(speedPinA,solmotor);
    analogWrite(speedPinB,sagmotor);
    }


  • sorunları hallettik.
  • sorunları hallettik.
  • Kolay gelsin

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Login with Facebook

Tagged

Açık kaynak kültürü gereği, çözdüğünüz problemlerin çözümlerini paylaşmayı lütfen unutmayın.