Bluno General Arduino

Romeo BLE mini v2 and GoBLE: Motors go in only one direction

userHead Account cancelled 2021-06-17 19:55:23 859 Views0 Replies
Dear Hivemind,
first port here.
I just started with Arduino and am stuck.

I have a DFRobot Devastator connected to an Romeo BLE mini v2.
Using the test code runs well. The motors are running and changing directions as expected.
This code is based on serial input via Arduino IDE.

But as soon as I use this code and connect GoBLE it only drives backwards.
I´ve tested serial output of the "Joystick" and it´s ok,

I used the GoBLE library found here: https://github.com/CainZ/GoBle/

Code:
Code: Select all
#include <Metro.h>

#include <GoBLE.h>
#include <QueueArray.h>
#include <DFMobile.h>
#include <Servo.h>
#include "GoBLE.h"

int speedPin_M1 = 5;     
int speedPin_M2 = 6;     
int directionPin_M1 = 4;     
int directionPin_M2 = 7;     

Servo base;
Servo top; 

int basePosition = 90; 
int topPosition = 115;  
int joystickX, joystickY;
int buttonState[5];

void setup() {
  Goble.begin();
  Serial.begin(115200);
  top.attach(9);        
  base.attach(8);     
  top.write(topPosition);   
  base.write(basePosition); 

}

void loop() {

  if (Goble.available()) {

    joystickX = Goble.readJoystickX();
    joystickY = Goble.readJoystickY();

    buttonState[SWITCH_UP]     = Goble.readSwitchUp();
    buttonState[SWITCH_DOWN]   = Goble.readSwitchDown();
    buttonState[SWITCH_LEFT]   = Goble.readSwitchLeft();
    buttonState[SWITCH_RIGHT]  = Goble.readSwitchRight();

        Serial.print("Joystick Value: ");
        Serial.print(joystickX);
        Serial.print("  ");
        Serial.println(joystickY);

    if (joystickY > 196) {
      Serial.println("turn right");
      carTurnRight(250, 250);
    }
    else if (joystickY < 64) {
      Serial.println("turn left");
      carTurnLeft(250, 250);
    }
    //else if (joystickX > 64) {
    else if (joystickX > 196) {
      Serial.println("move forward");
      //carAdvance(255, 255);
      carAdvance(-500, -500);
    }
    else if (joystickX < 64) {
      Serial.println("move backward");
      carBack(500, 500);
    }
    else {
      carStop();
    }

    if (buttonState[SWITCH_LEFT] == PRESSED &&
        buttonState[SWITCH_RIGHT] == RELEASED) {
      Serial.println("servo left");
      Serial.println(basePosition);
      basePosition += 5;
      if (basePosition >= 180) {
        basePosition = 180;
      }
    }
    else if (buttonState[SWITCH_RIGHT] == PRESSED &&
             buttonState[SWITCH_LEFT] == RELEASED) {
      Serial.println("servo right");
      Serial.println(basePosition);
      basePosition -= 5;
      if (basePosition <= 0) {
        basePosition = 0;
      }
    }
    else if (buttonState[SWITCH_UP] == PRESSED &&
             buttonState[SWITCH_DOWN] == RELEASED) {
      Serial.println("servo forward");
      Serial.println(topPosition);
      topPosition -= 5;
      if (topPosition <= 70) {
        topPosition = 75;
      }
    }
    else if (buttonState[SWITCH_DOWN] == PRESSED &&
             buttonState[SWITCH_UP] == RELEASED) {
      Serial.println("servo back");
      Serial.println(topPosition);
      topPosition += 5;
      if (topPosition >= 160) {
        topPosition = 155;
      }
    }
    top.write(topPosition);
    base.write(basePosition);
  }

}

void carStop() {                //  Motor Stop
  digitalWrite(speedPin_M2, 0);
  digitalWrite(directionPin_M1, LOW);
  digitalWrite(speedPin_M1, 0);
  digitalWrite(directionPin_M2, LOW);
}

void carTurnLeft(int leftSpeed, int rightSpeed) {       //Turn Left
  analogWrite (speedPin_M2, leftSpeed);             //PWM Speed Control
  digitalWrite(directionPin_M1, HIGH);
  analogWrite (speedPin_M1, rightSpeed);
  digitalWrite(directionPin_M2, LOW);
}

void carTurnRight(int leftSpeed, int rightSpeed) {     //Turn Right
  analogWrite (speedPin_M2, leftSpeed);
  digitalWrite(directionPin_M1, LOW);
  analogWrite (speedPin_M1, rightSpeed);
  digitalWrite(directionPin_M2, HIGH);
}

void carBack(int leftSpeed, int rightSpeed) {           //Move backward
  analogWrite (speedPin_M2, leftSpeed);
  digitalWrite(directionPin_M1, LOW);
  analogWrite (speedPin_M1, rightSpeed);
  digitalWrite(directionPin_M2, LOW);
}
void carAdvance(int leftSpeed, int rightSpeed) {         //Move forward
  analogWrite (speedPin_M2, leftSpeed);
  digitalWrite(directionPin_M1, HIGH);
  analogWrite (speedPin_M1, rightSpeed);
  digitalWrite(directionPin_M2, HIGH);
}