Getting motor to stop

I am trying to finish my halloween prop by using arduino UNO board, dual/single motor and a servo with battery pack. The coding I used does not get the motor to stop, it runs constantly and I need it to stop. I control it with a flysky transmitter and receiver. What code can I use to get the motor to stop. Here is the code I am currently using, what do I need to add to get the motor to stop


// Controller pins
const int CH_2_PIN = 3;


// Motor driver pins
const int STBY_PIN = 7;
const int AIN1_PIN = 12;
const int AIN2_PIN = 11;
const int APWM_PIN = 10;
const int BIN1_PIN = 1;
const int BIN2_PIN = 2;
const int BPWM_PIN = 5;


// Parameters
const int deadzone = 40;  // Anything between -40 and 40 is stop


void setup() {


  // Configure pins
  pinMode(STBY_PIN, OUTPUT);
  pinMode(AIN1_PIN, OUTPUT);
  pinMode(AIN2_PIN, OUTPUT);
  pinMode(APWM_PIN, OUTPUT);
  pinMode(BIN1_PIN, OUTPUT);
  pinMode(BIN2_PIN, OUTPUT);
  pinMode(BPWM_PIN, OUTPUT);


  // Enable motor driver
  digitalWrite(STBY_PIN, HIGH);
}


void loop() {


  // Read pulse width from receiver
  int ch_2 = pulseIn(CH_2_PIN, HIGH, 25000);


  // Convert to PWM value (-255 to 255)
  ch_2 = pulseToPWM(ch_2);


  // Drive motor
  drive(ch_2, ch_2);


  delay(5);
}


// Positive for forward, negative for reverse
void drive(int speed_a, int speed_b) {


  // Limit speed between -255 and 255
  speed_a = constrain(speed_a, -255, 255);
  speed_b = constrain(speed_b, -255, 255);


  // Set direction for motor A
  if ( speed_a == 0 ) {
    digitalWrite(AIN1_PIN, LOW);
    digitalWrite(AIN2_PIN, LOW);
  } else if ( speed_a > 0 ) {
    digitalWrite(AIN1_PIN, HIGH);
    digitalWrite(AIN2_PIN, LOW);
  } else {
    digitalWrite(AIN1_PIN, LOW);
    digitalWrite(AIN2_PIN, HIGH);
  }


  // Set direction for motor B
  if ( speed_b == 0 ) {
    digitalWrite(BIN1_PIN, LOW);
    digitalWrite(BIN2_PIN, LOW);
  } else if ( speed_b > 0 ) {
    digitalWrite(BIN1_PIN, HIGH);
    digitalWrite(BIN2_PIN, LOW);
  } else {
    digitalWrite(BIN1_PIN, LOW);
    digitalWrite(BIN2_PIN, HIGH);
  }


  // Set speed
  analogWrite(APWM_PIN, abs(speed_a));
  analogWrite(BPWM_PIN, abs(speed_b));
}


// Convert RC pulse value to motor PWM value
int pulseToPWM(int pulse) {

  // If we're receiving numbers, convert them to motor PWM
  if ( pulse > 1000 ) {
    pulse = map(pulse, 1000, 2000, -500, 500);
    pulse = constrain(pulse, -255, 255);
  } else {
    pulse = 0;
  }


  // Anything in deadzone should stop the motor
  if ( abs(pulse) <= deadzone ) {
    pulse = 0;
  }


  return pulse;
}

Always show us a good schematic of your proposed circuit. Show us good images of your ‘actual’ wiring. Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch. Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

1 Like



This is what it looks like if the images uploaded and I coded it on here

The motor will stop if you put in a line of code to set the STBY_PIN to LOW;
You have not explained under what conditions you want to stop the motor and how to get it going again. That is a whole different discussion.

So just add the STBY_PIN, LOW underneath the high code

When is it supposed to stop ?


1 Like

OBviously, NO! That would stop the motor from ever running. You have to put on your thinking cap and decide when and where and how to deal with the motor.

Yea thats the problem. Im using the code that I got from someones project and I have no idea how to code. Lol yes I know I probably shouldnt be doing it but its for a halloween prop

Yes it is

You do not have to know how to code in order to decide when and how you want to tell the program to turn the motor on and when and how to tell the program to turn the motor back on.

I dont know that, I just know that it needs to stop when Im not pressing on the throttle and go when I am

Hi,

const int BIN1_PIN = 1;

Pin 1 on a UNO is one of the two programming pins, 0 and 1.
It is best to avoid using them.

Tom... :grinning: :+1: :coffee: :australia:

1 Like

Im not very good at coding so I have no idea what to put lol

Showing the wiring drawing or instructions used to make the connections would help a lot.

1 Like

This is literally what I am going by, the coding and everything is there, but nothing to stop it

Hi,
Are you using the same motor?
If so have you tried the code that the Destructible supplied?

But a circuit draw of YOUR project will help.
Sometimes a reverse engineering helps to find problems with projects.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

The pin declarations in your code do not match your photo or the drawing from the Instrucables link. The Instructables code is what you have. The Instructables link has a link to the original code, which makes more sense because it does not use "Pin 1" (usb comms). Information about the parts you are using and how they are connected will help clarify the code used.

I am using the same exact everything as Donald Bell provides in his material list, and the code that he provides, not the original coding from Shawn Hymel

I am using the coding from Donld Bell that he provided, not the one from Shawn Hymel.

BUT how did YOU connect it?
Can you explain why you have EVERY pin of the motor controller wired?

When in the Destructible "fritzy" there are only FOUR WIRE?

Can you explain why you have a programming pin assigned and then declared an OUTPUT

const int BIN1_PIN = 1;
 pinMode(BIN1_PIN, OUTPUT);

Sorry but you need to get out pen(cil) and paper and help yourself by helping us.

Tom... :grinning: :+1: :coffee: :australia: