将模拟读取转换为范围到时间



我正试图用模拟传感器使led每分钟闪烁8到40次

我尝试过这个代码,但我意识到我必须将valoSensor转换为时间。怎么做?

int led = 13;
int pinAnalogo = A0;
int analogo;
int valorSensor;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogo = analogRead(pinAnalogo);
valorSensor = map(analogo, 0, 1023, 4, 80);
digitalWrite(led, HIGH);   
delay(valorSensor);                       
digitalWrite(led, LOW);    
delay(valorSensor);                      
}

这里的问题不在于代码,而在于生物学。要想看到闪烁(而不仅仅是闪烁,你需要250毫秒及以上的时间(,24帧/秒被视为运动,所以要想"眨眼",你可以从4帧/秒(=250毫秒(开始因此,我的建议是作为功能和用于测试的调整参数(blinkSpeedMultiplyer(进行无延迟闪烁

/* Blink without Delay as function
Turns on and off a light emitting diode (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.*/
// constants won't change. Used here to set a pin number:
const int blinkSpeedMultiplyer = 50; // very low
const int ledPin = 13;
const int pinAnalogo = A0;
// Variables will change:
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long startTime = millis();        // will store last time LED was updated
unsigned long blinkTime;           // interval at which to blink (milliseconds)
int analogo;
int valorSensor;
int ledState = LOW;             // ledState used to set the LED
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
analogo = analogRead(pinAnalogo);
valorSensor = map(analogo, 0, 1023, 4, 80);
blinkLed(valorSensor); // call the function
}
// Function to blink without blocking delay
void blinkLed(int inValue) {
blinkTime = blinkSpeedMultiplyer * inValue;
if (millis() - startTime >= blinkTime) {
// save the last time you blinked the LED
startTime = millis();
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}

相关内容

  • 没有找到相关文章

最新更新