Program only working when command sent via serial

I'm working on a project that involves controlling two step motors with an arduino uno. I've written a program to control it using the serial connection to my computer, and this works without a problem. However, I'm trying to get it to work wirelessly and I'm having some issues. I'm using the virtual wire library, and the code to run the motors wirelessly is the same as the code for running it from my computer; the only difference is how the command is received. The wireless code works fine, but only for one motor; I can never get both motors to respond. What puzzles me is that it works fine using the serial connection, but only works for one motor even though the code for wireless vs serial are almost identical. I've been struggling with this problem for over two months, and feel like I've exhausted ever possible solution without any luck. Any help is appreciated. If you want to see the code, I have it posted here: http://forum.arduino.cc//index.php?topic=192354.msg1422891#msg1422891

Thanks!

Post your code (all of it) here.

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

If you'd quit trying to hide the {, and put it on a new line where it belongs, you'd see that you never send anything unless you receive something.

Why does that surprise you?

Instead of all that handwaving, you could tell us what you are sending to the first Arduino, and which motor is not performing correctly.

but only works for one motor even though the code for wireless vs serial are almost identical.

Almost? Explain what the differences are. And why.

Thanks for the help. Here is the code that I use to control the two step motors using the serial connection. With this code, I type in a four digit code into the serial monitor and send it to the arduino. The first digit indicates which motor (1 or 2), the second digit indicates direction (1 or 2 for clockwise or counterclockwise) and the last two digits are proportional to the amount it rotates (with 64 being a full rotation). This code sends the command via serial to the arduino, which then runs the two motors connected to it. This is the first half of the code (too long to fit in one post). Also, I know that there’s a way to simplify it and that it’s not necessary to write out every command firing the pins, but I haven’t gotten around to it yet.

int motorPin1A = 10;    // Blue   - 28BYJ48 pin 1
int motorPin2A = 11;    // Pink   - 28BYJ48 pin 2
int motorPin3A = 12;    // Yellow - 28BYJ48 pin 3
int motorPin4A = 13;    // 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 same program…

    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++;

      }
    }
  
  delay(1000);
   }
  }

This is the code that I use to wirelessly send the same four digit code that I described above.

#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();
   
  }
}

I know that this code does work and that it is sending commands because I wrote another program to Serialprint what the above code sends out.

This is the third and final program. This program receives the command that is sent out by the program I posted above and then executes the command. It uses the same four digit code to determine which motor, which direction, and how far to rotate. The code to turn the motors is the exact same as the code from the first program that I posted (which works without a problem). The only difference is the way that it receives the code; instead of through the serial connection (there is none b/c it is wireless) it receives the command through Virtual wire.

int motorPin1A = 10;    // Blue   - 28BYJ48 pin 1
int motorPin2A = 12;    // Pink   - 28BYJ48 pin 2
int motorPin3A = 12;    // Yellow - 28BYJ48 pin 3
int motorPin4A = 13;    // 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(0);
  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
    Serial.println(command);
    steps=String(command.charAt(2))+String(command.charAt(3));
    stepsi=64*(steps.toInt());
    
    if (command.charAt(0)=='1')
    {
      if(command.charAt(1)=='1')
      {
          while(var1<stepsi){
          digitalWrite(motorPin1A,LOW);//step1
          digitalWrite(motorPin2A,LOW);
          digitalWrite(motorPin3A,LOW);
          digitalWrite(motorPin4A,HIGH);  
          delayMicroseconds(delaytime);
          var1++;

Everything below this point is the same as the first program I posted. The exception is that I included a break at the bottom of this code.

However, with this code only motor 2 works. When the first number of the four digit code I type in is 1 (indicating motor 1) motor 1 buzzes for a few seconds without moving and then stops. The length of time that it buzzes is proportional to the last two digits that I type in (which is the steps it’s supposed to rotate) but it doesn’t move. I’ve tried changing the pins and making sure that everything is properly connected, but it still doesn’t work. Both motors work fine when I’m sending the code through the serial. But when I send wirelessly, only motor 2 will work.

I’m also unsure about this chunk of code:

  a=String(int(buf[0])-48); 
    b=String(int(buf[1])-48);
    c=String(int(buf[2])-48);
    d=String(int(buf[3])-48);

When I was using my program to serial print what was being sent, I noticed that it was sent in hex and hence each digit was 48+ whatever I typed in. I included this code to turn it back to what was typed in the serial and it seems to work, but at the same time it doesn’t seem correct.

Thanks again! I really appreciate it.

Also, I know that there's a way to simplify it and that it's not necessary to write out every command firing the pins, but I haven't gotten around to it yet.

Yeah. It's called the Stepper library. With that, all your code would have fit in one post.

    if (command.charAt(0)=='2'){//First character B;controls X/Y-axis motor
      if(command.charAt(1)=='1'){

Consistency is a good thing. Soisusingthespacebarnowandthen.

The code you posted for the receiver side is reading instructions from the Serial port. The code you posted for the sender is using VirtualWire to send data. That implies a radio between the two Arduinos. And, a disconnect between the receiver code and the sender code.

How ARE the two Arduino's supposed to share data? How ARE they connected?

PaulS, you're right; I have a lot of work to do with cleaning up the code.

The receiver has a 433 Mhz radio receiver on it and is receiving commands through the radio using Virtual Wire, not the serial port. There are a few serial commands still left in the code that shouldn't be there (again, I need to clean it up). But the receiver code is using Virtual Wire, and it is receiving the data from the transmitting arduino (I've gotten it to work wirelessly, but only for some commands.

Thanks for telling me about Stepper Library. I'll look into it.

Thanks!