0

My issue is that I can't rotate the motors continuously. If I press any button, motor.step will be executed and then it stops, even though I didn't release the button.
This is my project so far.
Code:

#include <AFMotor.h>


//define steppers
AF_Stepper motor1(48, 1);
AF_Stepper motor2(48, 2);

char data;

void setup()
{

  motor1.setSpeed(200);  // 10 rpm
  motor2.setSpeed(200);  // 10 rpm
  Serial.begin(9600);
}

void loop()
{
  Serial.println(data);
  if (Serial.available())
  {
    data = Serial.read();
    if (data == 'A')
    {
      motor1.step(200, FORWARD, DOUBLE);
    }
    if (data == 'B')
    {
      motor1.step(200, BACKWARD, DOUBLE);
    }
    if (data == 'C')
    {
      motor2.step(200, FORWARD, SINGLE);
    }
    if (data == 'D')
    {
      motor2.step(200, BACKWARD, SINGLE);
    }
    if (data == 'E')
    {
    delay(50);
    }
  }
}

App Inventor blocks:app inventor blocks

0

This statement: data = Serial.read(); consumes the data sent over the serial port, so to get continual movement, you will have to constantly feed in A or B, etc. from whatever is generating that data.

Alternately, you could define a 2nd set of single-character commands to accompany the start-rotation parts so that send one command to turn on the motor, and a different command to turn off the motor. You would need to add a variable to hold the current state (started or stopped) of each motor, and turn on or off those states depending on what commands come in from the user.

(untested code)

#include <AFMotor.h>

//define steppers
AF_Stepper motor1(48, 1);
AF_Stepper motor2(48, 2);

char data;
int8_t motor1state=0;  // 0=> Stopped, -1=>Reverse, 1=>Forward
int8_t motor2state=0;

void setup()
{
  motor1.setSpeed(200);  // 10 rpm
  motor2.setSpeed(200);  // 10 rpm
  Serial.begin(9600);
}

void loop()
{
  Serial.println(data);
  if (Serial.available())
  {
    data = Serial.read();
    if (data == 'A')
    {
      motor1state = 1;
    }
    if (data == 'B')
    {
      motor1state = -1;
    }
    if (data == 'C')
    {
      motor2state = 1;
    }
    if (data == 'D')
    {
      motor2state = -1;
    }
    if (data == 'F')
    {
      motor1state = 0;
    }
    if (data == 'G')
    {
      motor2state = 0;
    }
    if (data == 'E')
    {
    delay(50);
    }
  }

  if (motor1state>0)
  {
    // Step forward
    motor1.step(200, FORWARD, DOUBLE);
  }
  else if (motor1state<0)
  {
    // Step backward
    motor1.step(200, BACKWARD, DOUBLE);
  }
  else
  {
    // Do nothing
  }

  if (motor2state>0)
  {
    // Step forward
    motor2.step(200, FORWARD, SINGLE);
  }
  else if (motor2state<0)
  {
    // Step backward
    motor2.step(200, BACKWARD, SINGLE);
  }
  else
  {
    // Do nothing
  }
}
  • Thanks for you reply. Now, if I press any button, the steppers won't stop rotating until I press any other button and then again, they won't stop. I'll somehow tweak this and maybe make a use of it. @jose – Róbert Pollák Apr 18 '18 at 14:42
  • That's the point. – jose can u c Apr 18 '18 at 14:50
  • I just rebuilt the mobile app for the 'G' and 'F' cases, now everything's fine. Thank you, you're great! @jose – Róbert Pollák Apr 18 '18 at 14:52
  • Everything's working correctly, but I am getting broken pipe error(516) messege randomly while I'am controlling the steppers? @jose – Róbert Pollák Apr 18 '18 at 15:58
  • What does the other end look like? You added a bluetooth tag -- are you sending from an Android app? An error like that would not indicate a problem with the Arduino code, but with the bluetooth module or the Android side. – jose can u c Apr 18 '18 at 16:01
0

Serial isn't a keyboard. When you send 'A' you send 'A'. Whatever that source is - if you send a single A you send a single A, and you receive a single A.

You will have to come up with some other protocol and write some special software on your computer that monitors the keys directly, and sends something along the lines of "I Pressed A" and "I Released A".

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.