You can see the code we prepared for our project below.
The purpose of this code is to classify the input from IRsensorPin as 'blink (high)' or 'not blink (low)' according to the specified threshold;
only when blink is detected (high) outputs square pulse from Pin 9 from PWM mode.
I think the code should work and I can't see any errors but we are not getting the output we want.
We would be very happy if you could help.
#include <PWM.h>
int IRsensorPin = 0;// IR Sensor
int LedPin = 13; // Visualization
int latestLevel = 0; // Previous IR level
int latestChange = 0;// Change in IR level
int changeThreshold = 5;// How hard a rising edge do we need?
//stimulation
int led_pin_pwm= 9;
int frequency = 150;
bool blinkCheckingFlag = true; //blink checking
//visualization
int duration = 100; // Length of visualization
float startPoint = 0;// Last start of visualization
void setup() {
Serial.begin(9600); // Debug constructor
pinMode(13,OUTPUT); // Visualizatio constructor
//stimulation functions
InitTimersSafe();
SetPinFrequencySafe(led_pin_pwm, frequency);
}
void loop() {
// Read Data
int IRsensorValue = analogRead(IRsensorPin);
Serial.println(IRsensorValue);
// look for rising edges
latestChange = IRsensorValue - latestLevel;
latestLevel = IRsensorValue;
if (blinkCheckingFlag == true && latestChange >= changeThreshold)
{
digitalWrite( LedPin, HIGH ); //detection
startPoint = millis(); //start point value
pwmWrite(led_pin_pwm, 127);// stimulation
}
else if ( blinkCheckingFlag = false && millis() >= startPoint + duration )
{
digitalWrite( LedPin, LOW ); //no detection
pwmWrite(led_pin_pwm, 0); //no stimulation
}
}