2

Micro-controller : SainSmart Mega 2560
Motor Shield: Osepp Motor Shield V1.0
I am trying to implement Radio Frequency communication on my wheeled robot however when the motors are running the radio frequency code will not receive messages.

The motor shield uses pins 4,7,8,12
I have setup the radio frequency to occur on pins 22,23 ,5

I see this reference to Why does VirtualWire conflicts with PWM signal in Arduino/ATmega328 pin D10? but am not sure if this applies to my situation.

How do I get Radio Frequency receiver/transmitter to work while motor shield in use?

code demonstrating the situation:

  #include <RH_ASK.h>
  #include <SPI.h> // Not actually used but needed to compile
  RH_ASK driver(2000, 22, 23, 5,true); // ESP8266: do not use pin 11
  /// *************************
  //      MOTOR SETUP
  /// *************************
  // Arduino pins for the shift register
  #define MOTORLATCH 12
  #define MOTORCLK 4
  #define MOTORENABLE 7
  #define MOTORDATA 8

  // 8-bit bus after the 74HC595 shift register
  // (not Arduino pins)
  // These are used to set the direction of the bridge driver.
  #define MOTOR1_A 2
  #define MOTOR1_B 3
  #define MOTOR2_A 1
  #define MOTOR2_B 4
  #define MOTOR3_A 5
  #define MOTOR3_B 7
  #define MOTOR4_A 0
  #define MOTOR4_B 6

    // Arduino pins for the PWM signals.
    #define MOTOR1_PWM 11
    #define MOTOR2_PWM 3
    #define MOTOR3_PWM 6
    #define MOTOR4_PWM 5
    #define SERVO1_PWM 10
    #define SERVO2_PWM 9

    // Codes for the motor function.
    #define FORWARD 1
    #define BACKWARD 2
    #define BRAKE 3
    #define RELEASE 4

    void setup()
    {
        Serial.begin(9600); // Debugging only
        if (!driver.init())
             Serial.println("init failed");

      //comment out code below  to allow receiver to read radio frequency  communication
      //BEGIN
        motor(1, FORWARD, 255);
        motor(2, FORWARD, 255);
        motor(4, FORWARD, 255);
        motor(3, FORWARD, 255);
       //END

    }
    void loop()
    {
        uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
        uint8_t buflen = sizeof(buf);
        if (driver.recv(buf, &buflen)) // Non-blocking
        {
            int i=0;
            // Message with a good checksum received, dump it.
            driver.printBuffer("Got:", buf, buflen);
            buf[5]= '\0';
            Serial.println((char*)buf);
        }
    }


    void motor(int nMotor, int command, int speed)
    {
      int motorA, motorB;

      if (nMotor >= 1 && nMotor <= 4)
      {  
        switch (nMotor)
        {
        case 1:
          motorA   = MOTOR1_A;
          motorB   = MOTOR1_B;
          break;
        case 2:
          motorA   = MOTOR2_A;
          motorB   = MOTOR2_B;
          break;
        case 3:
          motorA   = MOTOR3_A;
          motorB   = MOTOR3_B;
          break;
        case 4:
          motorA   = MOTOR4_A;
          motorB   = MOTOR4_B;
          break;
        default:
          break;
        }

        switch (command)
        {
        case FORWARD:
          motor_output (motorA, HIGH, speed);
          motor_output (motorB, LOW, -1);     // -1: no PWM set
          break;
        case BACKWARD:
          motor_output (motorA, LOW, speed);
          motor_output (motorB, HIGH, -1);    // -1: no PWM set
          break;;
        case RELEASE:
          motor_output (motorA, LOW, 0);  // 0: output floating.
          motor_output (motorB, LOW, -1); // -1: no PWM set
          break;
        default:
          break;
        }
      }
    }

    void motor_output (int output, int high_low, int speed)
    {
      int motorPWM;

      switch (output)
      {
      case MOTOR1_A:
      case MOTOR1_B:
        motorPWM = MOTOR1_PWM;
        break;
      case MOTOR2_A:
      case MOTOR2_B:
        motorPWM = MOTOR2_PWM;
        break;
      case MOTOR3_A:
      case MOTOR3_B:
        motorPWM = MOTOR3_PWM;
        break;
      case MOTOR4_A:
      case MOTOR4_B:
        motorPWM = MOTOR4_PWM;
        break;
      default:
        speed = -3333;
        break;
      }

      if (speed != -3333)
      {
        shiftWrite(output, high_low);
        if (speed >= 0 && speed <= 255)    
        {
          analogWrite(motorPWM, speed);
        }
      }
    }

    void shiftWrite(int output, int high_low)
    {
      static int latch_copy;
      static int shift_register_initialized = false;
      if (!shift_register_initialized)
      {
        // Set pins for shift register to output
        pinMode(MOTORLATCH, OUTPUT);
        pinMode(MOTORENABLE, OUTPUT);
        pinMode(MOTORDATA, OUTPUT);
        pinMode(MOTORCLK, OUTPUT);

        // Set pins for shift register to default value (low);
        digitalWrite(MOTORDATA, LOW);
        digitalWrite(MOTORLATCH, LOW);
        digitalWrite(MOTORCLK, LOW);
        // Enable the shift register, set Enable pin Low.
        digitalWrite(MOTORENABLE, LOW);
        // start with all outputs (of the shift register) low
        latch_copy = 0;
        shift_register_initialized = true;
      }
      bitWrite(latch_copy, output, high_low);
      shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
      delayMicroseconds(5);    // For safety, not really needed.
      digitalWrite(MOTORLATCH, HIGH);
      delayMicroseconds(5);    // For safety, not really needed.
      digitalWrite(MOTORLATCH, LOW);
    }
Community
  • 1
  • 1

2 Answers2

0

It looks like the reference you give could be the problem. To try that fix just find the RH_ASK.cpp file and uncomment line 16 like this

// RH_ASK on Arduino uses Timer 1 to generate interrupts 8 times per bit interval
// Define RH_ASK_ARDUINO_USE_TIMER2 if you want to use Timer 2 instead of Timer 1 on Arduino
// You may need this to work around other libraries that insist on using timer 1
#define RH_ASK_ARDUINO_USE_TIMER2
djUniversal
  • 624
  • 5
  • 7
0

Your motor is using pin 5:

#define MOTOR4_PWM 5

Try using a different third pin for your radio (make sure to match your software and hardware to the same new pin). Glancing at the specsheet, one option would be pin 24. Your motor code reserves every pin from 0 through 12. So, try...

RH_ASK driver(2000, 22, 23, 24,true); // ESP8266: do not use pin 11

Or change your motor pins using similar logic.