I'm new when it comes to electronics and Arduino, so the best way is to just play with it, right?
I started a small project that uses the LDR (Light Density Resistor), and I want to use it to calculate the blocking frequency or turn off the light beam.
For debugging purposes, I set up a small LED that flashes at a certain frequency (5 Hz, etc.) and uses the LCD to output the output.

... , . , , , 5 (5000 ). , 24 - , , ( , [5 . 5 = 25], ). 24,0 9 ..
: YouTube
... , . "". 24.0
:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);
int booBlocked = 0;
int counter = 0;
int checkValue = counter + 1;
int ledPin = 3;
int value = LOW;
long previousMillis = 0;
long freqency = 5;
long thousand = 1000;
long interval = thousand / freqency;
int tValue = 0;
long pMillis = 0;
long inter = 5000;
int pCount = 0;
float freq = 0;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0,1); lcd.print(interval);
lcd.setCursor(4,1); lcd.print("ms");
pinMode(ledPin, OUTPUT);
lcd.setCursor(0,0); lcd.print(freqency);
lcd.setCursor(4,0); lcd.print("Hz");
}
void loop() {
int sensorValue = analogRead(A0);
lcd.setCursor(7,1);
lcd.print(sensorValue);
delay(100);
if (millis() > 5000){
doCount(sensorValue);
updateFreq();
lcd.setCursor(7+5,0);
lcd.print(freq);
} else {
setThresholdValue(sensorValue);
lcd.setCursor(7+5,1);
lcd.print(tValue);
}
if (millis() - previousMillis > interval) {
previousMillis = millis();
if (value == LOW)
value = HIGH;
else
value = LOW;
digitalWrite(ledPin, value);
}
}
void updateFreq(){
long now = millis();
long t = now - pMillis;
if (t >= 10000) {
freq = (float) (counter - pCount);
pMillis = now;
pCount = counter;
}
}
void setThresholdValue(int sensorValue){
if (sensorValue > int(tValue/0.90)){
tValue = int (sensorValue*0.90);
}
}
void doCount(int sensorValue){
if (sensorValue < tValue){
booBlocked = 1;
} else {
booBlocked = 0;
}
if (booBlocked == 1) {
if (counter != checkValue){
counter = counter + 1;
lcd.setCursor(7,0);
lcd.print(counter);
}
} else {
if (counter == checkValue){
checkValue = checkValue + 1;
}
}
}
UPDATE
"" (. )
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);
long updateInterval = 150;
long updateTime = 0;
int ledPin = 3;
int value = LOW;
long previousMillis = 0;
long freqency = 16;
long thousand = 1000;
long blinkInterval = thousand / freqency;
static int counter = 0;
int booBlocked = 0;
int checkValue = counter + 1;
long onBootCalibrationTime = 5000;
static int threshold = 0;
float cutValue = 0.90;
float freq = 0;
long frequencyInterval = 5000;
long pMillis = 0;
int pCount = 0;
void setup() {
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);
lcd.setCursor(0,0); lcd.print(freqency);
lcd.setCursor(4,0); lcd.print("Hz");
lcd.setCursor(0,1); lcd.print(blinkInterval);
lcd.setCursor(4,1); lcd.print("ms");
Serial.begin(9600);
}
void loop() {
long time = millis();
int sensorValue = analogRead(A0);
blinkLED(time);
if (time < onBootCalibrationTime){
setThresholdValue(sensorValue);
} else {
doCount(sensorValue);
updateFreq(time);
}
if (time > updateTime){
updateTime += updateInterval;
lcd.setCursor(7,1); lcd.print(sensorValue);
lcd.setCursor(7+5,1); lcd.print(threshold);
lcd.setCursor(7,0);
lcd.print(counter);
lcd.setCursor(7+5,0); lcd.print(freq);
}
}
void blinkLED(long t){
if (t - previousMillis > blinkInterval) {
previousMillis = t;
if (value == LOW)
value = HIGH;
else
value = LOW;
digitalWrite(ledPin, value);
}
}
void setThresholdValue(int sValue){
if (sValue > int(threshold/cutValue)){
threshold = int (sValue*cutValue);
}
}
void doCount(int sValue){
if (sValue < threshold){
booBlocked = 1;
} else {
booBlocked = 0;
}
if (booBlocked == 1) {
if (counter != checkValue){
counter = counter + 1;
}
} else {
if (counter == checkValue){
checkValue = checkValue + 1;
}
}
}
void updateFreq(long t){
long inter = t - pMillis;
if (inter >= frequencyInterval) {
freq = (counter - pCount) / (float) (inter/1000);
pMillis = t;
pCount = counter;
}
}
, .