0

Used the plan from: https://www.instructables.com/id/Arduino-RC-Gamepad/

The motor doesn't stop when release the joystick, need help.

Thanks.

<pre>
    {#include <VirtualWire.h>

//motor A connected between A01 and A02
//motor B connected between B01 and B02

int STBY = 10; //standby  ////  An integer (more commonly called an int) is a number without a decimal point.


//Motor A
int PWMAL = 3; //??Speed control X
int PWMAR = 5; //??Speed control

// int AL_EN = 3; //Direction Y
//int AR_EN = 4; //Direction

//Motor B
int PWMBL = 6; //??Speed control
int PWMBR = 7; //??Speed control

//int BL_EN = 9; //Direction
//int BR_EN = 10; //Direction  ////WAS16

const boolean FORWARD = HIGH; ////The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only". This means that the variable can be used just as any other variable of its type, but its value cannot be changed. You will get a compiler error if you try to assign a value to a const variable.
const boolean REVERSE = LOW;

float speed_Max = 255; //pwm usually goes from 0-255  ////A float is a floating-point number, which means it is a number that has a decimal place
float speed_Min = 0;
float analogInput_Max = 1023;  ////
float analogInput_Min = 0;
float analogInput_Middle_X = 515;//ideally it would be analogInput_Max / 2
float analogInput_Middle_Y = 495;//ideally it would be analogInput_Max / 2
float deadBand = 0;
float middleMax = (analogInput_Max / 2) + deadBand;
float middleMin = (analogInput_Max / 2) - deadBand;
boolean pastDirection = FORWARD;

// boolean B_pastDirection = B_FORWARD;

void setup() {

  ////////////////////////////
  // Configure Timer 3 for PWM @ 3.9 kHz. Timer3 is associated with Pins 5, 2 and 3
  //try using pins 5,2 and 6,7

  TCCR3A = 0;           // undo the configuration done by...
  TCCR3B = 0;           // ...the Arduino core library
  TCNT3  = 0;           // reset timer

  TCCR3A = _BV(COM3A1)  // non-inverted PWM on ch. A
           | _BV(COM3B1)  // same on ch; B
           | _BV(WGM30);  // mode 10: ph. correct PWM, TOP = ICR1

  TCCR3B = (TCCR3B & 0b11111000) | 0x02; // / 0x01/31372.55 [Hz] / 0x02  3921.16 Hz / //** use this code got 3.9khz but joystick works not too good

  //////////////////////////////
  // Configure Timer 4 for PWM @ 3.9 kHz. Timer4 is associated with pins 6,7 and 8

  TCCR4A = 0;           // undo the configuration done by...
  TCCR4B = 0;           // ...the Arduino core library
  TCNT4  = 0;           // reset timer

  TCCR4A = _BV(COM4A1)  // non-inverted PWM on ch. A
           | _BV(COM4B1)  // same on ch; B
           | _BV(WGM40);  // mode 10: ph. correct PWM, TOP = ICR1

  TCCR4B = (TCCR4B & 0b11111000) | 0x02; // / 0x01/31372.55 [Hz] / 0x02  3921.16 Hz / //** use this code got 3.9khz but joystick works not too good

  /////////////////////////////////

  Serial.begin( 9600 );

  pinMode(STBY, OUTPUT); //// pin: the number of the pin whose mode you wish to set; mode: INPUT, OUTPUT, or INPUT_PULLUP.

  pinMode(PWMAL, OUTPUT);
  pinMode(PWMAR, OUTPUT);

  // pinMode(AL_EN, OUTPUT); // connect to Vcc
  //  pinMode(AR_EN, OUTPUT); // connect to Vcc

  pinMode(PWMBL, OUTPUT);
  pinMode(PWMBR, OUTPUT);

  //receiver setup
  vw_set_rx_pin(2);          //Sets pin 12 as the RX Pin  //// should be pin '2' for easy hook out
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000);            // Bits per sec
  vw_rx_start();             // Start the receiver PLL running
}

void loop() {

  uint8_t buf[VW_MAX_MESSAGE_LEN]; // This declares a variable array. instead of 7 variables buf1, buf2 etc...
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(buf, &buflen)) // Non-blocking
  {
    int i;
    int column = 0;
    String message;
    int commands[30];

    // Message with a good checksum received, dump it.
    for (i = 0; i < buflen; i++)
    {
      //DEBUG:
      Serial.print(char(buf[i]));

      if (char(buf[i]) == '|') {
        commands[column] = message.toInt();
        message = ""; //Clear
        column++;
      } else {
        message += char(buf[i]);
      }
    }
    //one more time to capture the last value since the message does not end with |
    commands[column] = message.toInt();

    motorControl(commands[0], commands[1]);
  }
}

void move(int motor, int speed, boolean direction) {
  digitalWrite(STBY, HIGH); //disable standby //// Write a HIGH or a LOW value to a digital pin.
  ///////////////////////////////////////////////////
  /*
    if (motor == 1) {
      digitalWrite(AL_EN, direction);
      digitalWrite(AR_EN, !direction);
      analogWrite(PWMAL, speed);   // ?? Writes an analog value (PWM wave) to a pin.
      analogWrite(PWMAR, speed); //??
    } else {
      digitalWrite(BL_EN, !direction);
      digitalWrite(BR_EN, direction);
      analogWrite(PWMBL, speed); //??
      analogWrite(PWMBR, speed); //??
    }
  */

  if (motor == 1) {
    if (direction) {

      // digitalWrite(AL_EN, HIGH); //  short these two pins directly to Vc
      // digitalWrite(AR_EN, HIGH); //
      analogWrite(PWMAL, speed);

      Serial.println("speedL =  ");
      Serial.println(speed);

    } else  {

      // digitalWrite(AL_EN, HIGH); // sime time short these two pins directly hook to Vc better
      //  digitalWrite(AR_EN, HIGH); //
      analogWrite(PWMAR, speed);

      Serial.println("speedR =  ");
      Serial.println(speed);
    }
  } else {
    if (direction) {

      // digitalWrite(BL_EN, HIGH);
      //  digitalWrite(BR_EN, HIGH);
      analogWrite(PWMBL, speed);

    } else  {

      // digitalWrite(BL_EN, HIGH);
      // digitalWrite(BR_EN, HIGH);
      analogWrite(PWMBR, speed);
    }
  }
}

void motorControl(float x, float y) {
  boolean currentDirection = y >= analogInput_Middle_Y;

  //map(value, fromLow, fromHigh, toLow, toHigh);
  if (currentDirection == REVERSE) {
    y = map(y, analogInput_Middle_Y, analogInput_Min, speed_Min, speed_Max) ;
  } else {
    y = map(y, analogInput_Middle_Y, analogInput_Max, speed_Min, speed_Max);
  }

  int subtractFromLeft = map(x, analogInput_Middle_X, analogInput_Min, speed_Min, y);
  int subtractFromRight = map(x, analogInput_Middle_X, analogInput_Max, speed_Min, y);

  if (subtractFromRight < 0) {
    subtractFromRight = 0;
  }

  if (subtractFromLeft < 0) {
    subtractFromLeft = 0;
  }

  int Throttle_RIGHT = y - subtractFromRight;
  int Throttle_LEFT = y - subtractFromLeft;

  boolean currentDirection_LEFT = currentDirection;
  boolean currentDirection_RIGHT = currentDirection;

  if (Throttle_LEFT < 1 && Throttle_RIGHT > 1) {
    currentDirection_LEFT = !currentDirection; //
    Throttle_LEFT = Throttle_RIGHT;
  }

  if (Throttle_RIGHT < 1 && Throttle_LEFT > 1) {
    currentDirection_RIGHT = !currentDirection;
    Throttle_RIGHT = Throttle_LEFT;
  }

  move(1, Throttle_LEFT, currentDirection_LEFT);
  move(2, Throttle_RIGHT, currentDirection_RIGHT);
}}
  • please edit the code, I can't put it into the window. – oldbin Apr 9 '19 at 23:38
  • Yes, Jot, but I don't know how to move it from there to here, can you merge them? thank you. – oldbin Apr 10 '19 at 0:17
  • please edit the code, I can't put it into the window .... select all of the code and click the {} button or press ctrl-k – jsotola Apr 10 '19 at 0:18
  • 1
    Hint: Start by writing a simple sketch and verify the analogInput_Middle_X/Y setting. – Mikael Patel Apr 10 '19 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.