1

我一直在努力让一些电机通过 H 桥工作并设法使用此代码。

// initialise motors
int enA = 3; // Motor 1
int in1 = 4;
int in2 = 2;

int enB = 5; // Motor 2
int in3 = 8;
int in4 = 7;

int enC = 11; // Motor 3
int in5 = 12;
int in6 = 13;

int enD = 6; // Motor 4
int in7 = 9;
int in8 = 10;

void setup()
{

  pinMode(enA, OUTPUT);   // set the outputs for motors
  pinMode(enB, OUTPUT);
  pinMode(enC, OUTPUT);
  pinMode(enD, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);
  pinMode(in7, OUTPUT);
  pinMode(in8, OUTPUT);

}


void motorLoop(){
// setting the direction to turn and speed
  digitalWrite(in1, HIGH); // Motor 1
  digitalWrite(in2, LOW);


  digitalWrite(in3, HIGH); // Motor 2
  digitalWrite(in4, LOW);


  digitalWrite(in5, HIGH); // Motor 3
  digitalWrite(in6, LOW);


  digitalWrite(in7, HIGH); // Motor 4
  digitalWrite(in8, LOW);

// Set the speed for the Motors
  analogWrite(enA, 1);
  analogWrite(enB, 20);
  analogWrite(enC, 100);
  analogWrite(enD, 200);
};

void loop()
{
  motorLoop();
  delay(500);

}

但是,我正在尝试将数据转换为数组并遇到了一些问题。我从未尝试过使用数字输入创建阵列,但使用模拟输入。这是我在 (TinkerCAD) 上的项目的链接https://www.tinkercad.com/things/fFQKRTjhDrb-smashing-allis-kieran/editel?tenant=circuits?sharecode=6rKnUZsFtcOAetd_TufIuN8TfUgi8EupA1TMjlxiacM=

正如您通过此代码所看到的,我尝试设置未显示错误的启用输入,但我正在努力设置电机旋转的输出和速度。

    // initialise motors
// Motor 1
int in1 = 4;
int in2 = 2;

// Motor 2
int in3 = 8;
int in4 = 7;

// Motor 3
int in5 = 12;
int in6 = 13;

// Motor 4
int in7 = 9;
int in8 = 10;

// Array of PWM's
int i = 0;
byte pwms[i] = {3,5,6,11};
byte numberPwms = 4;

void setup()
{

  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);
  pinMode(in7, OUTPUT);
  pinMode(in8, OUTPUT);

  for(byte i = 0; i <= numberPwms; i++){
    pinMode(pwms[i], OUTPUT);
  };

}


void motorLoop(){
// setting the direction to turn and speed
  digitalWrite(in1, HIGH); // Motor 1
  digitalWrite(in2, LOW);


  digitalWrite(in3, HIGH); // Motor 2
  digitalWrite(in4, LOW);


  digitalWrite(in5, HIGH); // Motor 3
  digitalWrite(in6, LOW);


  digitalWrite(in7, HIGH); // Motor 4
  digitalWrite(in8, LOW);

// Set the speed for the Motors

  for(byte i = 0; i < numberPwms; i++){
    analogWrite(pwms[i], 200);
  };

};

void loop()
{
  motorLoop();
  delay(500);

}

对此的任何帮助将不胜感激:D

4

1 回答 1

0

您的 for 循环计数 5 次。尝试将其更改为:

for (byte i = 0; i < numberPwms; i++) {
    pinMode(pwms[i], OUTPUT);
};

你的数组pwms[]也用 0 个元素初始化,但无论如何都应该工作。但是你可以改变它。

uint8_t pwms[] = {3, 5, 6, 11};
于 2019-11-07T20:27:55.150 回答