使用四位七段显示器显示滚轮计数
作者:互联网
对前几天的实验的扩展,用七段显示器显示计数,用的是官方的库SevSeg.h,
实验现象: https://v.douyin.com/e5p2uGe/
代码
#include <MsTimer2.h>//加入计时器用来测量速度 #include "SevSeg.h"//引入七段显示器库 SevSeg sevseg;//构建七段显示器对象 #define DEBOUNCE_TIME 2 //延时用来过滤不正常的信号,数字越大闪烁越明显 // 定义引脚2.3 int APhase = 2; int BPhase = 3; long count = 0;//计数 long preverCount = 0;//上一次的计数 long cerrentCount = 0;//当前计数 void getSpeed(){ preverCount = cerrentCount; cerrentCount = count; Serial.println( "speed is " + String (cerrentCount - preverCount) ) ; } void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(APhase, INPUT_PULLUP); pinMode(BPhase, INPUT_PULLUP); pinMode(LED_BUILTIN,OUTPUT); MsTimer2::set(1000, getSpeed);//改变计时时间可以调整速度的刷新率 //开始计时 MsTimer2::start(); byte numDigits = 4;//四位七段式显示器 byte digitPins[] = {4, 5, 6, 7};//每位数字的针脚 byte segmentPins[] = {8, 9, 10, 11, 12, 13, A0, A1};//依顺序的针脚 bool resistorsOnSegments = false; // 'false' means resistors are on digit pins byte hardwareConfig = COMMON_ANODE; // See README.md for options,共阳极 bool updateWithDelays = false; // Default 'false' is Recommended bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros, disableDecPoint); sevseg.setBrightness(100); } void loop() { //put your main code here, to run repeatedly: int firstAPhaseState= digitalRead(APhase); //digitalWrite(LED_BUILTIN,firstAPhaseState); if (firstAPhaseState == 1 ) { delay(DEBOUNCE_TIME); int secendAPhaseState = digitalRead(APhase); //从1变成0 开始判断是正转还是反转 if (secendAPhaseState == 0) { //用B相信号判断正反转 if (digitalRead(BPhase) == 0) { count++; Serial.println(count); } if (digitalRead(BPhase) == 1) { count--; Serial.println(count); } } } //初始状态是0的判断,想改进的话可以用外部中断的上升沿事件 if (firstAPhaseState == 0) { delay(DEBOUNCE_TIME); int secendAPhaseState = digitalRead(APhase); if (secendAPhaseState == 1) { if (digitalRead(BPhase) == 1) { count++; Serial.println(count); } if (digitalRead(BPhase) == 0) { count--; Serial.println(count); } } } sevseg.setNumber(count, 0); sevseg.refreshDisplay();//不刷新的话不亮灯 }
标签:count,滚轮,false,BPhase,计数,digitalRead,Serial,七段 来源: https://www.cnblogs.com/kyo413/p/14810257.html