【问题标题】:Arduino Motor RampArduino电机斜坡
【发布时间】:2015-07-26 07:18:36
【问题描述】:

我需要一些有关我的 arduino 斜坡电机代码的帮助。 我正在做一个遥控机器人。遥控器只有一个按钮。

如果按钮为高:电机应使用 pwm 逐渐增加其速度(以pwm=0 开头)。从静止速度到最大速度的时间应为 1 秒。一旦达到最大速度,它应该保持最大速度 (pwm=255)。 松开按钮的那一刻,电机应从当前速度逐渐降低到完全停止。

到目前为止,我已经成功编写了加速和保持速度部分的代码;代码不包含按钮部分。

int motor;
int motorpwm=11;
int x=1;
int i;

void setup()
{

pinMode(11,OUTPUT);
Serial.begin(9600);
}

void loop() 
{

for(int i=0;i<256;i=i+x)
 {
analogWrite(motorpwm,i);
Serial.println(i);
if(i==255)
  {
x=0;
Serial.println("PWM is maximum");
  }
 }
}

请包含完整的代码,包括代码的按钮控制部分。

【问题讨论】:

  • 你试过了吗?你的按钮引脚是什么?
  • 我可以在引脚 8 上添加一个按钮引脚。
  • 您的全局变量“i”从未使用过。你的 for 循环也永远不会结束。查看 IDE 中的 BlinkWithoutDelay 示例或查看此内容:forum.arduino.cc/index.php?topic=223286.0 然后您可以运行代码检查按钮,独立于控制电机的代码。

标签: c arduino


【解决方案1】:

不向您保证这是否没有错误,但您可以尝试一下并在此处发布您的结果,以便我可以为您调试。给你:

int motor;
int motorpwm = 11;
int x = 1;
int i;
int pwmValue;
void setup()
{
  pinMode(11, OUTPUT);
  pinMode(2, INPUT); //Button connected to Vcc and pulled down using a 10k resistor. 
  Serial.begin(9600);
}

void loop()
{
  if (digitalRead(2)) //if button is pressed it will read High signal
  {
    if (pwmValue <= 255)
    {
      analogWrite(motorpwm, pwmValue++);
      Serial.print("Going Up with value: ");
      Serial.println(pwmValue);
    }
    else
    {
      Serial.println("PWM is maximum");
    }
  }

  else
  {
    if (motorpwm > 0)
    {
      analogWrite(motorpwm, pwmValue--);
      Serial.print("Going down with value: ");
      Serial.print(pwmValue);
    }
    else
    {
      Serial.println("PWM is minimum");
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2019-07-01
    • 1970-01-01
    • 2020-03-04
    • 2013-09-04
    • 1970-01-01
    相关资源
    最近更新 更多