I am new to arduino and this is my first project, or rather a project that my friend is working on and I decided to help him with the coding. I am studying coding so decided to try my hand at the language that is Arduino and it would seem that I have already hit a wall.
The code bellow is for a spinning gear with 36 "teeth" and the purpose of it is to reset the counter every time it makes a full circle. The current iteration of the code is doing what it is supposed to, how ever, it only works if it starts spinning from the starting position of the gear. What I need is, for it to be able to recognize where the beginning of the gear is even if it starts spinning from the middle. (the start has about 4x wider gap between the last and the first "teeth" than the ones in between)
From what I gather, I am supposed to use the pulseIn function, but I might be completely off, and even if I am not, I still don't know where or how to begin using the aforementioned function.
As the title says I am using a Photo Interrupter switch to determine the location of the "teeth" and a Arduino Leonardo, if that matters.
Would appreciate any help I could get with this, and I do apologize, if my description has not been helpful enough or if I am using the wrong section of the forum for this question.
volatile unsigned int counter = 0;
int n=37;
void setup(){
Serial.begin(9600);
pinMode(2,INPUT);
attachInterrupt(digitalPinToInterrupt(2),ai0,RISING);
}
void loop(){
Serial.println (counter);
if(counter==n){
counter=1;
}
}
void ai0(){
counter++;
}