Motor only working part of the time

Hi there!

I'm working on a project where I remotely control two step motors and an LED. I have two arduino unos. One is connected to the motors and LED and the other has a serial connection to my laptop. I'm using some one way 433 MHz transmitters/receivers to send messages from the arduino connected to my laptop to the arduino controlling the motors and light. To control the light and motor, I've come up with a four digit code that I type into the serial monitor, which is then transmitted from one arduino to the other.

The problem is that one of the motors refuses to work when I try and operate it remotely. I've written another program that directly sends the four digit code from my laptop to the arduino with the lights and motors, and this works perfectly fine. However, when I try sending the code wirelessly, it doesn't work.

I'm fairly confident that the four digit code is being sent correctly; I've used Serial.readln to read the code being received, and there hasn't been a problem. I also know that the code to run the motors is fine as it is identical between the program that wirelessly sends the four digit code and the program that directly transmits the four digit code to the arduino with motors. I also know that it's not a problem with the motor, because I've been able to get it work without a problem, but not when commands are sent wirelessly.

Thanks for your help in advance. I've been stuck here for two weeks and seem to have exhausted all possible options. If it'll help to post all of the code, let me know.

Thanks!

Do you have any code to show?

When you're running wireless, are you also running on batteries?

Many batteries cannot supply the amount of current needed to run a motor.

I agree, make sure you try to power the motor with its own battery before changing the code, It may be as simple as that.

Thanks for the suggestions. I've been running it wirelessly on battery, but I've also tried it with the arduino plugged into a wall outlet; still nothing. One of the motors works fine even when battery operated, but the other motor stubbornly refuses to work. Should I have the motor directly connected to the battery and not run it through the arduino? Each motor draws 92 mA and the LED is run through a MOSFET transistor which doesn't draw much current, and I read that the arduino uno can put out 500 mA.

This is the code for running the motors and light when it is directly connected to my laptop. I type in a four digit code into the serial monitor, and it performs the operation. The first digit is either 1,2, or 3 and dictates which motor it is or if it's the light receiving the command. The second number is either 1 or 2 and specifies direction the motor turns or whether to turn the LED on or off. The last two digits go from 00 to 64 and tell the motor how many steps to go. This works perfectly fine.

int motorPin1A = 8;    // Blue   - 28BYJ48 pin 1
int motorPin2A = 9;    // Pink   - 28BYJ48 pin 2
int motorPin3A = 10;    // Yellow - 28BYJ48 pin 3
int motorPin4A = 11;    // Orange - 28BYJ48 pin 4

int motorPin1B = 4;    // Blue   - 28BYJ48 pin 1
int motorPin2B = 5;    // Pink   - 28BYJ48 pin 2
int motorPin3B = 6;    // Yellow - 28BYJ48 pin 3
int motorPin4B = 7;    // Orange - 28BYJ48 pin 4

//int motorApower = 1;
//int motorBpower = 2;

int delaytime=1500;

int stepsi; //Number of steps commanded
String command;
char buffer[5];
int var1;
String steps;

void setup(){
  pinMode(motorPin1A, OUTPUT);
  pinMode(motorPin2A, OUTPUT);
  pinMode(motorPin3A, OUTPUT);
  pinMode(motorPin4A, OUTPUT);
  
  pinMode(motorPin1B, OUTPUT);
  pinMode(motorPin2B, OUTPUT);
  pinMode(motorPin3B, OUTPUT);
  pinMode(motorPin4B, OUTPUT);
  
  pinMode(13, OUTPUT);

  Serial.begin(9600);
}
void loop(){
  if (Serial.available()>0){
    Serial.readBytes(buffer,5);
    command=String(buffer);
    var1=0; //clear out var1
    stepsi=0; //clear out steps
    Serial.println(command);
    steps=String(command.charAt(2))+String(command.charAt(3));
    stepsi=64*(steps.toInt());
    
    if (command.charAt(0)=='1'){//First character 1;controls Z-axis motor
      if(command.charAt(1)=='1'){
        //Code to turn motor 1 clockwise
          Serial.println("Motor A clockwise "+String(stepsi));
          while(var1<stepsi){
          digitalWrite(motorPin1A,LOW);//step1
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,HIGH);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,LOW);//step2
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,HIGH);
          digitalWrite(motorPin4A,HIGH);  
          delayMicroseconds(delaytime);
          var1++;
          
          digitalWrite(motorPin1A,LOW);//step3
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,HIGH);
          digitalWrite(motorPin4A,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,LOW);//step4
          digitalWrite(motorPin2A,HIGH);
          digitalWrite(motorPin3A,HIGH);
          digitalWrite(motorPin4A,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,LOW);//step5
          digitalWrite(motorPin2A,HIGH);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,HIGH);//step6
          digitalWrite(motorPin2A,HIGH);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,HIGH);//step7
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,HIGH);//step8
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,HIGH);  
          delayMicroseconds(delaytime);
          var1++;
      }
      }
      if(command.charAt(1)=='2'){
        //Code to turn motor 1 anti-clockwise
          Serial.println("Motor A anti-clockwise "+String(stepsi));
          while(var1<stepsi){
          digitalWrite(motorPin1A,HIGH);//step8
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,HIGH);  
          delayMicroseconds(delaytime);
          var1++;

          digitalWrite(motorPin1A,HIGH);//step7
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,HIGH);//step6
          digitalWrite(motorPin2A,HIGH);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,LOW);//step5
          digitalWrite(motorPin2A,HIGH);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,LOW);//step4
          digitalWrite(motorPin2A,HIGH);
          digitalWrite(motorPin3A,HIGH);
          digitalWrite(motorPin4A,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,LOW);//step3
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,HIGH);
          digitalWrite(motorPin4A,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,LOW);//step2
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,HIGH);
          digitalWrite(motorPin4A,HIGH);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1A,LOW);//step1
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,HIGH);  
          delayMicroseconds(delaytime);
          var1++;

      }

Second half of the code…

}
    }
    if (command.charAt(0)=='2'){//First character B;controls X/Y-axis motor
      if(command.charAt(1)=='1'){
        //Code to turn motor 2 clockwise
          Serial.println("Motor B clockwise "+String(stepsi));
          while(var1<stepsi){
          digitalWrite(motorPin1B,LOW);//step1
          digitalWrite(motorPin2B,LOW);
          digitalWrite(motorPin3B,LOW);
          digitalWrite(motorPin4B,HIGH);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,LOW);//step2
          digitalWrite(motorPin2B,LOW);
          digitalWrite(motorPin3B,HIGH);
          digitalWrite(motorPin4B,HIGH);  
          delayMicroseconds(delaytime);
          var1++;
          
          digitalWrite(motorPin1B,LOW);//step3
          digitalWrite(motorPin2B,LOW);
          digitalWrite(motorPin3B,HIGH);
          digitalWrite(motorPin4B,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,LOW);//step4
          digitalWrite(motorPin2B,HIGH);
          digitalWrite(motorPin3B,HIGH);
          digitalWrite(motorPin4B,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,LOW);//step5
          digitalWrite(motorPin2B,HIGH);
          digitalWrite(motorPin3B,LOW);
          digitalWrite(motorPin4B,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,HIGH);//step6
          digitalWrite(motorPin2B,HIGH);
          digitalWrite(motorPin3B,LOW);
          digitalWrite(motorPin4B,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,HIGH);//step7
          digitalWrite(motorPin2B,LOW);
          digitalWrite(motorPin3B,LOW);
          digitalWrite(motorPin4B,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,HIGH);//step8
          digitalWrite(motorPin2B,LOW);
          digitalWrite(motorPin3B,LOW);
          digitalWrite(motorPin4B,HIGH);  
          delayMicroseconds(delaytime);
          var1++;
      }
      }
      if(command.charAt(1)=='2'){
        //Code to turn motor 2 anti-clockwise
          Serial.println("Motor B anti-clockwise "+String(stepsi));
          while(var1<stepsi){
          digitalWrite(motorPin1B,HIGH);//step8
          digitalWrite(motorPin2B,LOW);
          digitalWrite(motorPin3B,LOW);
          digitalWrite(motorPin4B,HIGH);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,HIGH);//step7
          digitalWrite(motorPin2B,LOW);
          digitalWrite(motorPin3B,LOW);
          digitalWrite(motorPin4B,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,HIGH);//step6
          digitalWrite(motorPin2B,HIGH);
          digitalWrite(motorPin3B,LOW);
          digitalWrite(motorPin4B,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,LOW);//step5
          digitalWrite(motorPin2B,HIGH);
          digitalWrite(motorPin3B,LOW);
          digitalWrite(motorPin4B,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,LOW);//step4
          digitalWrite(motorPin2B,HIGH);
          digitalWrite(motorPin3B,HIGH);
          digitalWrite(motorPin4B,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,LOW);//step3
          digitalWrite(motorPin2B,LOW);
          digitalWrite(motorPin3B,HIGH);
          digitalWrite(motorPin4B,LOW);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,LOW);//step2
          digitalWrite(motorPin2B,LOW);
          digitalWrite(motorPin3B,HIGH);
          digitalWrite(motorPin4B,HIGH);  
          delayMicroseconds(delaytime);
          var1++;
  
          digitalWrite(motorPin1B,LOW);//step1
          digitalWrite(motorPin2B,LOW);
          digitalWrite(motorPin3B,LOW);
          digitalWrite(motorPin4B,HIGH);  
          delayMicroseconds(delaytime);
          var1++;

      }
    }
      }
    if (command.charAt(0)=='C'){//First character C; controls LED
      if(command.charAt(1)=='X'){
        digitalWrite(13,HIGH);
        Serial.println("LED On");
      }
      if(command.charAt(1)=='Y'){
        digitalWrite(13,LOW);
        Serial.println("LED Off");
      }
     
    }
     if(command=="DZ00"){//Kill command for step motors; LED will retain state
        digitalWrite(motorPin1A,LOW);
        digitalWrite(motorPin2A,LOW);
        digitalWrite(motorPin3A,LOW);
        digitalWrite(motorPin4A,LOW);
        
        digitalWrite(motorPin1B,LOW);
        digitalWrite(motorPin2B,LOW);
        digitalWrite(motorPin3B,LOW);
        digitalWrite(motorPin4B,LOW);
        
        Serial.println("Motor Kill Command");
      }
     if(command=="XXXX"){//Nuclear kill command; all pins off
        digitalWrite(motorPin1A,LOW);
        digitalWrite(motorPin2A,LOW);
        digitalWrite(motorPin3A,LOW);
        digitalWrite(motorPin4A,LOW);
        
        digitalWrite(motorPin1B,LOW);
        digitalWrite(motorPin2B,LOW);
        digitalWrite(motorPin3B,LOW);
        digitalWrite(motorPin4B,LOW);

        digitalWrite(13,LOW);
        
        Serial.println("Nuclear Kill Command");
     }
  delay(1000);
   }
  }

Here’s the code that I’m using to wirelessly transmit the four digit codes:

#include <VirtualWire.h>
//Transmitter Code

char buffer[5];
void setup()
{
  Serial.begin(9600);
  vw_setup(2000);
  vw_set_tx_pin(7);
  
}

void loop()
{
  if(Serial.available()>0){
    Serial.readBytes(buffer,5);
   
      vw_send((uint8_t*)buffer,5);
      vw_wait_tx();
   
  }
}

Finally, this is the code used to receive the commands and execute the procedure. I’m suspecting that there’s something wrong with when I receive the code. Using serial.Readln, I noticed that the four digit code was being received in hexadecimal form, so I subtracted 48 from each to convert back to characters. I have a feeling that this is what’s messing things up.

int motorPin1A = 8;    // Blue   - 28BYJ48 pin 1
int motorPin2A = 9;    // Pink   - 28BYJ48 pin 2
int motorPin3A = 10;    // Yellow - 28BYJ48 pin 3
int motorPin4A = 11;    // Orange - 28BYJ48 pin 4

int motorPin1B = 4;    // Blue   - 28BYJ48 pin 1
int motorPin2B = 5;    // Pink   - 28BYJ48 pin 2
int motorPin3B = 6;    // Yellow - 28BYJ48 pin 3
int motorPin4B = 7;    // Orange - 28BYJ48 pin 4

String a;
String b;
String c;
String d;

//int motorApower = 1;
//int motorBpower = 2;

int delaytime=700;

#include<VirtualWire.h>

int stepsi; //Number of steps commanded
String command;
char buffer[5];
int var1;
String steps;

void setup(){
  pinMode(motorPin1A, OUTPUT);
  pinMode(motorPin2A, OUTPUT);
  pinMode(motorPin3A, OUTPUT);
  pinMode(motorPin4A, OUTPUT);
  
  pinMode(motorPin1B, OUTPUT);
  pinMode(motorPin2B, OUTPUT);
  pinMode(motorPin3B, OUTPUT);
  pinMode(motorPin4B, OUTPUT);
  
  pinMode(12, OUTPUT);
  
  vw_setup(2000);
  vw_set_rx_pin(2);
  vw_rx_start(); 

}
void loop(){
  uint8_t buflen=VW_MAX_MESSAGE_LEN;
  uint8_t buf[buflen];
  
  if(vw_get_message(buf, &buflen))
  {
    int i;
    for(int i=0;i<buflen;i++)
    {
    a=String(int(buf[0])-48); 
    b=String(int(buf[1])-48);
    c=String(int(buf[2])-48);
    d=String(int(buf[3])-48);      
    command=a+b+c+d;
    var1=0; //clear out var1
    stepsi=0; //clear out steps
    steps=String(command.charAt(2))+String(command.charAt(3));
    stepsi=64*(steps.toInt());
    
    if (command.charAt(0)=='2'){//First character A;controls Z-axis motor
      if(command.charAt(1)=='1'){
        //Code to turn motor A clockwise
          Serial.println("Motor A clockwise "+String(stepsi));
          while(var1<stepsi){
          digitalWrite(motorPin1A,LOW);//step1
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,HIGH);  
          delayMicroseconds(delaytime);
          var1++;

The rest of it is the same as the code shown above.

I apologize that the code is so long. I know that there’s a way to shorten it so that I don’t have to list out every single pin firing to get it to turn, but I’ve kept it like this for now because it’s more intuitive to me.

Thanks so much for the help!