简体   繁体   English

用Arduino Uno控制两个直流电机

[英]Controlling two DC motors with arduino uno

So Im using this sample code to run my two motors...but the code only works for one motor...how do I modify this code for the other motor to work? 因此,我使用此示例代码来运行我的两个电动机...但是该代码仅适用于一个电动机...如何修改此代码以使另一个电动机正常工作? I tried replacing channel a with channel b but that didnt work for some reason? 我尝试将频道a替换为频道b,但是由于某些原因而无法正常工作?

const int channel_a_enable  = 6;
const int channel_a_input_1 = 4;
const int channel_a_input_2 = 7;
const int channel_b_enable  = 5;
const int channel_b_input_3 = 3;
const int channel_b_input_4 = 2;

void setup()
{
  pinMode( channel_a_enable, OUTPUT );  // Channel A enable
  pinMode( channel_a_input_1, OUTPUT ); // Channel A input 1
  pinMode( channel_a_input_2, OUTPUT ); // Channel A input 2

  pinMode( channel_b_enable, OUTPUT );  // Channel B enable
  pinMode( channel_b_input_3, OUTPUT ); // Channel B input 3
  pinMode( channel_b_input_4, OUTPUT ); // Channel B input 4

  Serial.begin( 38400 );
  Serial.println("Starting up");
}

void loop()
{
      Serial.println("Channel A forward");
      analogWrite( channel_a_enable, 255);
      digitalWrite( channel_a_input_1, HIGH);
      digitalWrite( channel_a_input_2, LOW);
      delay(5000);
      allInputsOff();

      Serial.println("Channel A reverse");
      analogWrite( channel_a_enable, 255);
      digitalWrite( channel_a_input_1, LOW);
      digitalWrite( channel_a_input_2, HIGH);
      delay(5000);
      allInputsOff();

      Serial.println("Channel A forward half speed");
      analogWrite( channel_a_enable, 127);
      digitalWrite( channel_a_input_1, HIGH);
      digitalWrite( channel_a_input_2, LOW);
      delay(5000);
      allInputsOff();
}

void allInputsOff()
{
  digitalWrite( 4, LOW );
  digitalWrite( 7, LOW );
  digitalWrite( 6, LOW );
  digitalWrite( 3, LOW );
  digitalWrite( 2, LOW );
  digitalWrite( 5, LOW );
}

Your demo code references only channel 'a' except for set-up. 您的演示代码仅引用通道“ a”(设置除外)。

Here's a snippet showing how you'd turn both motors on: 这是显示如何打开两个电动机的代码段:

Serial.println("Channels A and B forward");
analogWrite( channel_a_enable, 255);
digitalWrite( channel_a_input_1, HIGH);
digitalWrite( channel_a_input_2, LOW);
analogWrite( channel_b_enable, 255);
digitalWrite( channel_b_input_3, HIGH);
digitalWrite( channel_b_input_4, LOW);
delay(5000);
allInputsOff();

Of course the code for each motor is almost identical so you could create a subroutine: 当然,每个电动机的代码几乎相同,因此您可以创建一个子例程:

void runMotor(int enablePin, int input1, int input2) {
    analogWrite( enablePin, 255);
    digitalWrite( input1, HIGH);
    digitalWrite( input2, LOW);
}

And then use it like this: 然后像这样使用它:

Serial.println("Channels A and B forward");
runMotor(channel_a_enable, channel_a_input_1, channel_a_input_2);
runMotor(channel_b_enable, channel_b_input_3, channel_b_input_4);
delay(5000);
allInputsOff();

Disclaimer: I'm writing this in my lunch break and not in the Arduino IDE so there may be minor errors. 免责声明:我在午休时间而不是在Arduino IDE中编写此代码,因此可能会有一些小错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM