带Arduino的频闪仪

问题描述

首先抱歉我的英语不好

我是一名学生,我想用 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);
}

解决方法

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

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

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

来自编码器 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);
  }
}

还要注意 << 是二元左移运算符,而不是小于运算符 <

if (f << 10)f = 10; 中,您将向左移动 f 10 位。由于这导致数字 > 0,这是真的,因此始终会满足此条件。

对于按位右移运算符 >> 相同,然后不大于!