I connected a wave generator to the analog pins to read the frequencies of the waves generated. I used different frequencies of sine waves with a minimum of 0V and maximum of 5V.
Whenever the voltage is greater than 2.5V, I made one of the digital pins HIGH, and LOW when less than 2.5V. Now by measuring the square wave from the digital pin, it always has the same frequency as the sine waves until 500Hz.
After 500Hz, the square wave's frequency doesn't agree with the sine wave, which made me think that the problem is in the analogRead(). Is there a way to have higher frequency?
Code:
const int sensorPin1 = A0;
const int sensorPin2 = A1;
const float maximumPower = 2.5;
int calibration = 1;
void setup() {
Serial.begin(57600);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
}
void loop() {
int voltage2 = 3;
int voltage1 = 3;
int sensorVal = abs(analogRead(sensorPin1) - analogRead(sensorPin2));
float ratio = abs(analogRead(sensorPin2) / analogRead(sensorPin1));
float voltageDifference = (sensorVal / 1024.0) * 5.0;
float power = calibration * voltageDifference;
if (power >= maximumPower || voltage1 <= 1 || voltage2 <= 1) {
digitalWrite(6, LOW);
}
else {
digitalWrite(6, HIGH);
}
}