I am currently working on my Senior Project for schooling. The project involves a shaft that will be rotating, and we are trying to use an Arduino UNO to measure the time of rotation. The code I am currently using functionally operates only part of the time. When the shaft of the encoder stops on the right pulsing location, the output continues to count. The timer will stop if the shaft ends in the right location. Here is a link to the website that helps explain the encoder we are using. Please offer any advise regarding the code or the encoder. How to connect optical rotary encoder with Arduino - Electric Diy Lab
void setup() {
Serial.begin (9600);
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
pinMode(3, INPUT_PULLUP); // internal pullup input pin 3
}
double i = 0;
double a;
double c;
void loop()
{
// Send the value of counter
if(digitalRead(2)==HIGH)
{
a = millis();
while(digitalRead(2)==HIGH)
while(digitalRead(3)==LOW)
while(digitalRead(3)==HIGH)
{
c = millis();
i = (c - a) / 1000;
Serial.println(c);
Serial.println(a);
Serial.println(i);
Serial.println("-------");
delay(100);
}