Flysky and Arduino A4988

Hi,
I am trying to run two Nema 17 motors with my Flysky receiver. I am looking at doing so using two of my arduino a4899 motor drivers.
Would this code work?

const int stepPin = 3; 
const int dirPin = 4;
const int fPin = 10;
const int bPin = 11;

void setup() {
  // put your setup code here, to run once:
   Serial.begin(9600);
  pinMode(stepPin,OUTPUT);
    pinMode(fPin, INPUT);
    pinMode(bPin, INPUT);
  pinMode(dirPin, OUTPUT);

}
void loop() {
  digitalWrite(dirPin,HIGH);
  // put your main code here, to run repeatedly:
  if
   (pulseIn(fPin, HIGH), 1100, 1900, 0, 255);
  else if
   (pulseIn(fPin, LOW), 1100, 1900, 0, 255);

  digitalWrite(dirPin,LOW);
  // put your main code here, to run repeatedly:
  if
   (pulseIn(bPin, HIGH), 1100, 1900, 0, 255);
  else if
   (pulseIn(bPin, LOW), 1100, 1900, 0, 255);

delay(20);
}

fPin is for forwards and bPin is for backwards. dirPin is the direction of the motor eg. HIGH is forwards, stepPin is the steps of the motor.

Thanks in advance.

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

1 Like

Why What do you think this does?

a7

You have two motors but only one set of Step and Direction pins. Are the two motor drivers connected to the same two pins and thus always doing the same thing?

I don't know what you thought this would do, but it doesn't. Same for the other three similar statements.

Yes, the two motors do the same thing. As for that section of code I found someone else using it and it worked.
What I want with that is both motors to be off unless there is a command from fPin (the flysky receiver) to go dirPin HIGH or LOW (forwards or backwards) and for stepPin to go until fPin stops its input.
Is there any better code I could use there instead?

Thank you everyone for there input here.

Please supply a link to, or post, code that uses the line we have flagged as problematic.

Please describe what you think it does.

a7

Sorry can't find the person who used that code. He used it to control an led, but he had analogWrite before that section of code.

Would this code work:

const int stepPin = 3; 
const int dirPin = 4; 

double ch1=2;
int a=5; int b=6;


void setup()
{
  Serial.begin(9600);
  
  pinMode(2,INPUT);
  pinMode(5,OUTPUT); pinMode(6,OUTPUT);
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  
}

void loop()
{
ch1 = pulseIn(2,HIGH);

if(ch1==0)
{     
 digitalWrite(dirPin,HIGH);
 digitalWrite(a,LOW); digitalWrite(b,LOW);
 digitalWrite(stepPin,LOW);
}

else if(ch1>1530)
{
 digitalWrite(dirPin,HIGH);
 digitalWrite(a,HIGH); digitalWrite(b,HIGH);
 digitalWrite(stepPin,HIGH);
}




ch1 = pulseIn(2,HIGH);

if(ch1==0)
{     
 digitalWrite(dirPin,LOW);
 digitalWrite(a,LOW); digitalWrite(b,LOW);
 digitalWrite(stepPin,LOW);
}

else if(ch1>1530)
{
 digitalWrite(dirPin,LOW);
 digitalWrite(a,HIGH); digitalWrite(b,HIGH);
 digitalWrite(stepPin,HIGH);
}

}

Thanks

Have you tried using Serial.print statements to see if your pulseIn calls are returning plausible values?

Do you have something I against using a library of code meant specifically for use with the A4988?

TBH I have no clue whether what you wrote would work or not. To answer your question would be learning too much about the A4988, Imwould just use a library and trust it to get the details correct.

Good luck!

a7

No, I have not.

No, I didn't think about there being any specific library. How should I use it the library?

Thanks

Sry, I assumed you are familiar with the Arduino and have used libraries to make it easier and more fool-proof to connect any number of devices.

google is your friend:

    a4988 arduino library

code, tutorials, examples. Get busy.

a7

If you don't want to learn some basic programming
connect your receivers servo-pins to a common brushless motorcontroller or a common brushed motorcontroller and you are done.

Is there a specific reason to use stepper-motors?
Do you need to stop the rotation at very accurate angles?

What is the overall project? What does switching motors on off do in the end?
There is a pretty high change that the final purpose of this can be achieved in an easier way than analysing a servo-signal and making stepper-motors rotate/stop

best regards Stefan

Hello Stefan,
Thanks for your message.

They are fast and powerful. Which is exactly what I need.

No, I do not.

I am building a 1/10 rc car. I am wanting a lot of power and speed, but I do not need very precise angles, I just need to be able to turn them on and off. Why I am using Nema 17 motors 1: Is that they are powerful and fast. 2: I already have a pile of them.

Thank you again.

Are you saying that will work with

two Nema 17 motors

Please identify a common motor controller you can connect to an r/c receiver and control a Nema 17 motor.

a7

Sorry I did not add that I meant connect a standard brushless-motor or a standard brushed DC-motor to the standard RC-receiver.

As the TO has added he has a lot of Nema17 motors laying around and wants to use the Nema17 stepper-motors instead of buying something new

My suggestion does not fit.
best regards Stefan

Thanks, Would a L298P Shield work? Can I control two Nema 17 motors with it or just one?

Thanks again.

maybe I have to add some thoughts:

you are building an RC-car. So I assume it is battery-powered.
The efficiency of a stepper-motor how much electric energy are you putting in and how much mechanical driving you get out of it is pretty low.

If your RC car is just for 5 minutes fun then reload battery or use the next battery out of 10 batterys and the car is also moving slowly wlaking speed just for fun
this will work.

A stepper-motor uses the same amount of current whenever you drive regardless of the speed.

if you plan to use the "RC" car with a wired powersupply that can deliver current 24/7 you can run it long.
A battery will be empty pretty quick.

If you plan to have speed in this RC car switch over to the classical driving system
brushless motor-controller and brushless motor

Stepper motors are, generally, neither fast nor powerful.

If you want fast and powerful, use Brushless DC (BLDC) motors and matching Electronic Speed Controls (ESCs).

Thanks All. I guess I will give up on the Nema 17 motor. I understand what StefanL38 was saying, Thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.