Hi there!
I'm working on a project where I wirelessly control two step motors using an arduino uno. To send commands to the motors, I have a second uno connected to my laptop. Commands are entered into the serial monitor, sent to the arduino hooked to my computer, and then transmitted wirelessly using the virtual wire library to the arduino connected to the motors.
The problem I'm having is that when I control the motors wirelessly, one of the motors only turns weakly in one direction. Both of the motors work fine when I directly control them from my laptop (using a separate program). However, when I send the commands wirelessly one motor works while the other only works in one direction.
I read on this post: Stepper motor, doesn't turn counter clockwise, wrong steps number indication - Motors, Mechanics, Power and CNC - Arduino Forum that changing the order of the pins might help. I tried all 24 permutations, but all I found were 8 ways to make it work in just one direction.
The motors are powered by a wall adapted, so it probably isn't a problem with the power supply.
Here is the program used to transmit the commands:
#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();
}
}
And here is the program I've been using to receive the commands and turn the motor. First digit specifies which motor, second digit specifies direction, and last two digits tell it how far to rotate (64=full turn).
#include <Stepper.h>
#include<VirtualWire.h>
String command;
int anglei;
String angle;
char buffer[5];
int i;
int motorPin1 = 8;
int motorPin2 = 10;
int motorPin3 = 9;
int motorPin4 = 11;
int motorPin5 = 4;
int motorPin6 = 6;
int motorPin7 = 5;
int motorPin8 = 7;
String a;
String b;
String c;
String d;
Stepper stepper1(64, motorPin1, motorPin2, motorPin3, motorPin4);
Stepper stepper2(64, motorPin5, motorPin6, motorPin7, motorPin8);
void setup()
{
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(motorPin5, OUTPUT);
pinMode(motorPin6, OUTPUT);
pinMode(motorPin7, OUTPUT);
pinMode(motorPin8, OUTPUT);
stepper1.setSpeed(400);
stepper2.setSpeed(200);
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))
{
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;
angle=String(command.charAt(2))+String(command.charAt(3));
anglei=32*(angle.toInt());
if(command.charAt(0)=='1')
{
if(command.charAt(1)=='1')
{
stepper1.step(anglei);
}
if(command.charAt(1)=='2')
{
anglei=int(anglei*-1);
stepper1.step(int(anglei));
}
}
if(command.charAt(0)=='2')
{
if(command.charAt(1)=='1')
{
stepper2.step(anglei);
}
if(command.charAt(1)=='2')
{
anglei=int(anglei*-1);
stepper2.step(int(anglei));
}
}
break;
}
}
}
I've gotten both motors to work fine when connected to my laptop, but just can't get it to work wirelessly. Any help will be greatly appreciated.
Thanks!