Controling DC motors using 433 RF Transmitter and Reciever

Hello!

This is my independent study project for the year and it’s main goal is to be able to move the motors 6 different ways: both right, both left, motor one right, motor one left, two right, and two left.

To do this I used an Arduino Micro and the 433 RF Transmitter to create a remote for the user to input the designated course of action via the 4 buttons. (I didn’t have 6 so the smaller 2 buttons have an attempt to compensate for this through user action. One tap for one direction, two taps for the opposite.)

As for the motors I used 2 5V DC motors controlled through an Arduino Uno, a H-bridge(L293D), and the 433 RF Reciever.

The motors, H-bridge, and the code for the function of the motors work. My issue is with the RF communication. I used the RadioHead library (RadioHead: RadioHead Packet Radio library for embedded microprocessors ). Here is my code below:

REMOTE
**************
#include <SPI.h>
#include <RH_ASK.h>

// 8 - far left
// 9 - middle left
// 10 - middle right
// 11 - far right
int bothOne = 8;
int bothTwo = 11;
int rightMotorOne = 10;
int leftMotorOne = 9;
int remotePins[]= {8,9,10,11};
int counter=0;
int counterTwo=0;

//RH_ASK::RH_ASK(uint16_t speed = 2000, uint8_t txPin = 2, uint8_t pttPin = 2, bool pttInverted = false);

RH_ASK driver;

void setup()
{
  driver.setModeTx();
  Serial.begin(9600);

  for(int i = 0; i<5 ; i++)
  {
    pinMode(remotePins[i], INPUT);
    digitalWrite(remotePins[i],HIGH);
  }
}

void loop()
{
char *msg2;

if(digitalRead(bothOne) == LOW)
{
  char *msg2 = "1";
  digitalWrite(13, true);
  driver.send((uint8_t *)msg2, strlen(msg2));
  driver.waitPacketSent();
  delay(200);
  digitalWrite(13, false);
}

if(digitalRead(bothTwo) == LOW)
{
  char *msg2 = "2";
  digitalWrite(13, true); 
  driver.send((uint8_t *)msg2, strlen(msg2));
  driver.waitPacketSent();
  delay(200);
  digitalWrite(13, false);
}


if(digitalRead(leftMotorOne) == LOW)
{
 counter++; 
}
if(digitalRead(rightMotorOne) == LOW)
{
 counterTwo++; 
}

if(counter==1)
{
  char *msg2 = "3";
  digitalWrite(13, true); 
  driver.send((uint8_t *)msg2, strlen(msg2));
  driver.waitPacketSent();
  delay(200);
  digitalWrite(13, false);
  counter=0;
}
if(counter==2)
{
 char *msg2 = "4";
 digitalWrite(13, true);
 driver.send((uint8_t *)msg2, strlen(msg2));
 driver.waitPacketSent();
 delay(200);
 digitalWrite(13, false);
 counter=0;
}

if(counterTwo==1)
{
  char *msg2 = "5";
  digitalWrite(13, true); 
  driver.send((uint8_t *)msg2, strlen(msg2));
  driver.waitPacketSent(); 
  delay(200);
  digitalWrite(13, false);
  counterTwo=0;
}
if(counterTwo==2)
{
  char *msg2 = "6";
  digitalWrite(13, true); 
  driver.send((uint8_t *)msg2, strlen(msg2));
  driver.waitPacketSent();
  delay(200);
  digitalWrite(13, false);
  counterTwo=0;
}
}
*******************

MOTORS
*******************
#include <RH_ASK.h>
#include <SPI.h>

//Motor 1:
//Pin 1 = Arduino 10
//Pin 2 = Arduino 9
//If 1 = High and 2 = Low, one direction.
//If 1 = Low and 2 = High, other direction.
//Motor 2:
//Pin 9 = Arduino 11
//Pin 10 = Arduino 13
//If 9 = High and 10 = Low, one direction.
//If 9 = Low and 10 = High,other direction.

#define E1 10  // Enable Pin for motor 1, turn on motor 1
#define E2 6  // Enable Pin for motor 2, turn on motor 2

#define I1 8  // Control pin 1 for motor 1
#define I2 9  // Control pin 2 for motor 1
#define I3 12  // Control pin 1 for motor 2
#define I4 7  // Control pin 2 for motor 2

RH_ASK driver;

void setup() {
driver.setModeRx();
//for motors
  pinMode(E1, OUTPUT);
  pinMode(E2, OUTPUT);

  pinMode(I1, OUTPUT);
  pinMode(I2, OUTPUT);
  pinMode(I3, OUTPUT);
  pinMode(I4, OUTPUT);
//for transmitter
  Serial.begin(9600);
}


//255 = full speed
//153 = half speed
//90 = lowest speed
void loop() {
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);

digitalWrite(13, true);
driver.recv(buf, &buflen);
digitalWrite(13, false);

if(buf==(uint8_t *)"1")
{
 turnBRight(); 
}
if(buf== (uint8_t *)"2")
{
  turnBLeft();
}
if(buf==(uint8_t *)"3")
{
  turnOneLeft();
}
if(buf == (uint8_t *)"4")
{
  turnOneRight();
}
if(buf== (uint8_t *)"5")
{
  turnTwoLeft();
}
if(buf== (uint8_t *)"6")
{
  turnTwoRight();
}

}

void turnBRight()//right
{
      analogWrite(E1, 90);
   analogWrite(E2, 90);

   digitalWrite(I1, LOW);
   digitalWrite(I2, HIGH);
   digitalWrite(I3, LOW);
   digitalWrite(I4, HIGH);
}
void turnBLeft()//left
{
      analogWrite(E1, 90);
   analogWrite(E2, 90);

   digitalWrite(I1, HIGH);
   digitalWrite(I2, LOW);
   digitalWrite(I3, HIGH);
   digitalWrite(I4, LOW);
}
void turnOneLeft()
{
      analogWrite(E1, 90);

   digitalWrite(I1, HIGH);
   digitalWrite(I2, LOW);
}
void turnOneRight()
{
      analogWrite(E1, 90);

   digitalWrite(I1, LOW);
   digitalWrite(I2, HIGH);
}
void turnTwoLeft()
{
      analogWrite(E2, 90);

   digitalWrite(I3, HIGH);
   digitalWrite(I4, LOW);
}
void turnTwoRight()
{
      analogWrite(E2, 90);

   digitalWrite(I3, LOW);
   digitalWrite(I4, HIGH);
}
void stopMotors()
{
      analogWrite(E1, 0);
   analogWrite(E2, 0);
}
**********************

When run, it seems as if the transmitter is stuck trying to send the message completely and the receiver seems to be continuously searching for the message it’s supposed to be receiving, but I’m wondering if it could be a hardware problem as well.

Help would be appreciated.
Thanks!

Please edit your post and enclose the code in code tags ("</>" button).

Look,

First - take a look at this post in which you are shown how to do may thinks at the same time.

Second - Find out something about Top down design- Also know as how to eat a whole cow!

Third - When we use top down design we split things in to parts. So as soon as we look at your question we see it has two parts.

A. Controlling motors and

B. Moving data between two Arduino’s (assumed that there are two)

After which you will see that control of the motors and the method moving of the data are not really related. I could use any method of getting input to the Arduino and I could be controlling anything.

The following is not for the faint of heart!

First gut, skin, remove the head and tail of your cow (All these bits can be used - ox-tail soup real was made form cows tails - mmmm - soup!

Next chop the it in two from top to bottom, then in to smaller parts, then in to the joints, stakes and other cuts, Now cook which ever bit you wish to eat.

Take each cook bit cut and serve. Use knife and fork to create a bite sized chunk.

Chew and then allow the acid and other things in your gut to reduce the food to smaller bits which can be re-used by your body.

Mark

int remotePins[]= {8,9,10,11};


   for(int i = 0; i<6 ; i++)
   {
     pinMode(remotePins, INPUT);
     digitalWrite(remotePins,HIGH);

Just another noob here but this is what I noticed.
you only have 4 elements in your array.
but the for loop counts 5.
Also the brackets and variable i on remotePins within the for loop is missing

int remotePins[]= {8,9,10,11};


   for(int i = 0; i<5 ; i++)
   {
     pinMode(remotePins[i], INPUT);
     digitalWrite(remotePins[i],HIGH);
}

mixing int’s and #define’s is confusing.
all of these could be byte or const byte and would save space
and this could be another array

 #define I1 8  // Control pin 1 for motor 1
#define I2 9  // Control pin 2 for motor 1
#define I3 12  // Control pin 1 for motor 2
#define I4 7  // Control pin 2 for motor 2

  pinMode(I1, OUTPUT);
   pinMode(I2, OUTPUT);
   pinMode(I3, OUTPUT);
   pinMode(I4, OUTPUT);