首页 > 解决方案 > 如何根据光电管值更改的次数将显示的值增加到七段显示?

问题描述

所以我一直在研究一个应该模拟激光绊线的 arduino 项目。激光照射到光敏电阻上,每次输入板的电压由于没有光而增加,板应该将counter变量显示到七段显示器。

但无论出于何种原因,只要电阻器被激活,它就会counter = counter + 1无限次地执行该行。我只希望计数器在每次激活时增加一次,不是在光敏电阻增加电压时激活。

这里有人可以帮忙吗?

#include "SevSeg.h"
SevSeg sevseg;

const int pResistor = A0;
//the photoresistor is connected to analog input pin A0

int count; //number of times voltage is increased
int value; //voltage reading of A0

void setup() {
  pinMode(pResistor, INPUT);

  byte numDigits = 1;
  byte digitPins[]={};
  byte segmentPins[]={6, 5, 2, 3, 4, 7, 8, 9};
  bool resistorsOnSegments = true;

  byte hardwareConfig = COMMON_CATHODE;
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  sevseg.setBrightness(90);

}

void loop() {
  value=analogRead(pResistor);
//here's where my problem starts
  while (count < 9){
    if (value>1022){
        //it should be noted that the photoresistor is preceded by a 100k Ω resistor, which 
        //explains the large value
      sevseg.setNumber(count);
    }
    else{
      count = count + 1;
      sevseg.setNumber(count);
    }

  }
//here's where my problem ends

  sevseg.refreshDisplay();
}

标签: c++arduino

解决方案


推荐阅读