【问题标题】:Stroboscope with Arduino带 Arduino 的频闪仪
【发布时间】:2021-04-12 00:24:26
【问题描述】:

首先对不起我的英语不好

我是一名学生,我想用 Arduino 为我的学校项目制作频闪仪

频率在 10hz 到 3000hz 之间可变,并使用旋转编码器改变

编码器正常旋转1步频率\pm 1hz和编码器下推时旋转编码器频率\pm 100hz

Arduino 在引脚 13 上产生一个 PWM 信号,它连接到一个大功率 npn 晶体管,它打开和关闭一个 10 瓦的 LED

我使用 Paul Stoffregen 的 Encoder.h 库和tone() 函数对其进行编码

但我有一个问题

我编写了这个程序并将其上传到 Arduino Uno,但它不起作用 IDK 问题出在哪里

#include <Encoder.h>
#define ENCODER_PULSES_PER_STEP 1
int f = 10;
int direction;
Encoder myEnc(2, 3);
int t = 0;
void setup() {

  pinMode(13, OUTPUT);
  pinMode(4, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  direction = myEnc.read();
}

void loop() {


  if (abs(direction) >= ENCODER_PULSES_PER_STEP) {
    if (direction > 0) {
      if (digitalRead(4) == 1) {
        f++;
        if (f >> 2500)f = 2500;
      }
      else {
        f = f + 100;
        if (f >> 2500)f = 2500;
      }
    } else {
      if (digitalRead(4) == 1) {
        f--;
        if (f << 10)f = 10;
      }
      else {
        f = f - 100;
      }
    }
    myEnc.write(0);


  }
  tone(13, f);
}

【问题讨论】:

  • 不要为编码器引脚设置 pinmode,库会处理它。在 loop() 中读取编码器。
  • 您不能使用 Arduino 的 PWM 来创建频闪仪。你永远不会看到你想要的东西,因为如果你连接一个灯或 LED,PWM 会更调光(但正如 Juraj 所说,还有许多其他编程错误)。如果要频闪仪,则必须将脉冲设置得尽可能低(tone() 的占空比为 50%,analogwrite() 的频率固定)。检查您的频闪仪:github.com/PaulStoffregen/TimerOne

标签: arduino encoder


【解决方案1】:

当您的程序启动时,函数setup 会执行一次。

然后在无限循环中执行函数loop。

由于您只有在setup 中拥有direction = myEnc.read();,因此您只会读取一次编码器。

来自编码器library's documentation:

/* Encoder Library - TwoKnobs Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these pin numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder knobLeft(5, 6);
Encoder knobRight(7, 8);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(9600);
  Serial.println("TwoKnobs Encoder Test:");
}

long positionLeft  = -999;
long positionRight = -999;

void loop() {
  long newLeft, newRight;
  newLeft = knobLeft.read();
  newRight = knobRight.read();
  if (newLeft != positionLeft || newRight != positionRight) {
    Serial.print("Left = ");
    Serial.print(newLeft);
    Serial.print(", Right = ");
    Serial.print(newRight);
    Serial.println();
    positionLeft = newLeft;
    positionRight = newRight;
  }
  // if a character is sent from the serial monitor,
  // reset both back to zero.
  if (Serial.available()) {
    Serial.read();
    Serial.println("Reset both knobs to zero");
    knobLeft.write(0);
    knobRight.write(0);
  }
}

注意您的代码和他们的代码之间的差异。

GitHub repository 的另一个更简单的例子来满足 Juraj。

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(5, 6);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

还要注意&lt;&lt; 是二进制左移运算符,而不是小于运算符&lt;!

在if (f &lt;&lt; 10)f = 10; 中,您将 f 向左移动 10 位。由于这会导致数字 > 0,这是真的,所以这个条件总是会得到满足。

&gt;&gt; 相同,它是按位右移运算符,而不是更大!

【讨论】:

  • @Juraj 我引用了文档。这就是他们给出的使用示例。
  • 但这与问题无关
  • @Juraj 它展示了如何使用编码器,特别是您需要循环读取它,而不是设置。我不明白你的意思。仅仅因为示例中有第二个编码器并不意味着它无关紧要。
  • 哦,是的,你是对的
猜你喜欢
  • 1970-01-01
  • 2019-05-20
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多