0

I am trying to control a stepper motor wirelessly with a button. I am using the 433MHZ rf module and the Virtualwire library.

the code is working, the receiver is getting the messages from the transmitter, but the problem is the messages are sent extremely slowly, so much so that the receiver circuit led is flashing on and off instead of staying in the on position when the transmitter button is being held down. consequently the stepper motor stutters instead of moving smoothly.

I have tried increasing the bits per second in the vw_setup but that just makes the led flash faster and not stay in the on position. it also makes the stepper vibrate more.

how do i make the stepper motor move smoothly wirelessly over rf?

transmitter code:

#include <VirtualWire.h>

int currentlocation = 0;
const int stepPin = 3; 
const int dirPin = 4;  
int active = 0;
const int ledPin =  13;      // the number of the LED pin
String dataout;
char buffer[1];
int idx=0;

class Button {

  public:

    int pin;
    int state;
    int location;
    int Speed;
    int Direction;
    int active;
    int prevstate;

    Button(int pin1, int state1, int location1, int active1, int previousState)
    {
      pin = pin1;
      state = state1;
      location = location1;
      active = active1;
      prevstate = previousState;
    }

    Button(int pin1, int state1, int speed1, int direction1)
    {
      pin = pin1;
      state = state1;
      Speed = speed1;
      Direction = direction1;
    }

    Show(){
      if (digitalRead(pin) == HIGH){
          digitalWrite(ledPin, HIGH);
          } else {
          // turn LED off:
          digitalWrite(ledPin, LOW);
          }
    }
};

Button button(7,0,0,0,0);
Button button2(8,0,30,0,0);
Button Movebutton1(5,0,500,HIGH);
Button Movebutton2(6,0,500,LOW);
Button buttons[2] = {button, button2};
Button Movebuttons[2] = {Movebutton1, Movebutton2};

void setup() {
      vw_setup(2000);
      pinMode(stepPin,OUTPUT); 
      pinMode(dirPin,OUTPUT);
      pinMode(ledPin, OUTPUT);
      Serial.begin(9600);      // open the serial port at 9600 bps:    
}

void loop() {
    for (int i = 0; i<2; i++){
      buttons[i].Show();
      if (digitalRead(buttons[i].pin) == HIGH){  
      sprintf(buffer, "%d", i);
      vw_send((uint8_t *)buffer, strlen(buffer));
      vw_wait_tx();
      }
    }
    for (int i = 0; i<2; i++){
      Movebuttons[i].Show();
      if (digitalRead(Movebuttons[i].pin) == HIGH){
      sprintf(buffer, "%d", i+2);
      vw_send((uint8_t *)buffer, strlen(buffer));
      vw_wait_tx();
      }
    }
}

Receiver code:

#include <VirtualWire.h>

char message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
int last_idx = 0;

int currentlocation = 0;
const int stepPin = 3; 
const int dirPin = 4;  
int active = 0;
const int ledPin =  13;      // the number of the LED pin
String str_out;

class Button {

  public:

    int pin;
    int state;
    int location;
    int Speed;
    int Direction;
    int active;
    int prevstate;

    Button(int pin1, int state1, int location1, int active1, int previousState)
    {
      pin = pin1;
      state = state1;
      location = location1;
      active = active1;
      prevstate = previousState;
    }

    Button(int pin1, int state1, int speed1, int direction1)
    {
      pin = pin1;
      state = state1;
      Speed = speed1;
      Direction = direction1;
    }

    Relocate(){
      int shift = location - currentlocation;
      if (shift > 0){
        Direction = 0;
      }
      else{
        Direction = 1;
      }
      shift = abs(shift);
      digitalWrite(dirPin,Direction); // Enables the motor to move in a particular direction
      // Makes 200 pulses for making one full cycle rotation
      for(int x = 0; x < shift; x++) {
        digitalWrite(stepPin,HIGH); 
        delayMicroseconds(500); 
        digitalWrite(stepPin,LOW); 
        delayMicroseconds(500); 
      }
      currentlocation = location;
    }

    Move(){
      digitalWrite(dirPin,Direction); // Enables the motor to move in a particular direction
      // Makes 200 pulses for making one full cycle rotation
        digitalWrite(stepPin,HIGH); 
        delayMicroseconds(Speed); 
        digitalWrite(stepPin,LOW); 
        delayMicroseconds(Speed); 
      if (Direction == 1){
      currentlocation -= 1;
      }
      else{
      currentlocation += 1;
      }
    }

    Show(){
      if (vw_get_message(message, &messageLength)){
          digitalWrite(ledPin, HIGH);
          } else {
          // turn LED off:
          digitalWrite(ledPin, LOW);
          }
    }
};

Button button(7,0,0,0,0);
Button button2(8,0,30,0,0);
Button Movebutton1(5,0,500,HIGH);
Button Movebutton2(6,0,500,LOW);
Button buttons[2] = {button, button2};
Button Movebuttons[2] = {Movebutton1, Movebutton2};


void setup() {
        // Initialize the IO and ISR
      vw_setup(2000); // Bits per sec
      vw_rx_start(); // Start the receiver
      Serial.begin(9600);
      pinMode(stepPin,OUTPUT); 
      pinMode(dirPin,OUTPUT);
      pinMode(ledPin, OUTPUT);   
}

void loop() {

      // Set buffer to size of expected message
    if (vw_get_message(message, &messageLength)){
    int idx=atoi(message);
    Serial.print(message);
    for (int i = 0; i<2; i++){
      buttons[i].Show();
      if (idx == i){
      buttons[i].Relocate();
      active = i;    
      //buttons[i].location = currentlocation;
      }
    }
    for (int i = 2; i<4; i++){
      Movebuttons[i-2].Show();
      if (idx == i){
      Movebuttons[i-2].Move();
      buttons[active].location = currentlocation;
      }
    }
    }
}
10
  • how do i make the motor move smoothly when i hold down the button wirelessly, how do i stop the jittering – Mark Markrowave Charlton Jan 19 '19 at 1:35
  • 1
    no i want to hold the button and send the on position while im holding it down – Mark Markrowave Charlton Jan 19 '19 at 1:46
  • 1
    the motor is stuttering because the messages are stuttering – Mark Markrowave Charlton Jan 19 '19 at 1:46
  • 1
    every time the loop function executes it checks to see if the button is on right? so if the message is not getting there on time it assumes that it is off – Mark Markrowave Charlton Jan 19 '19 at 1:59
  • 1
    A transmit bit rate of 2000 is normal. Some 433MHz modules can do 4kHz, but that should be specified and such specifications are hard to find. There is overhead, I'm guessing that one value takes about 20 to 25 pulses. By law the transmitter may only be busy for 10%. That results into sending data 8 times per second. Just 8 per second, no more. – Jot Jan 19 '19 at 2:00
0

What is the incoming message interval (1/incomingMessageFrequency)? Whenever the receiving end receives a "button-down" message, it should activate the motor for that interval + long enough to receive or not receive the next "button-down". Once the next "button-down" should have been received and has been missed, that's when you should stop the motor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.