How can I continue to count RPS (rate per sec) then trigger buzzer?

I am having a lot of trouble continuing from here as i can only count the number of time the light is blocked for the LDR.

Preferably 5 per sec, then it will trigger the Buzzer.

here's my program till now.
p.s I am really new to this and i desperately have to complete this project by next week. so thank you if you're trying to help me. (: cheers from Singapore.

*/
int counter =0;
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 9; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int sensor_state=0;
int case_state=0;

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.println(counter);
// turn the ledPin on
if(sensorValue>500){
digitalWrite(ledPin, HIGH);
sensor_state=1;
}
// stop the program for milliseconds:
//delay(sensorValue);
// turn the ledPin off:
else {
digitalWrite(ledPin, LOW);
sensor_state=0;
}
// stop the program for for milliseconds:
delay(5);

switch (case_state){

case 1 : if (sensor_state==1) {
case_state=2;
counter++;
}
break ;

case 2 : if (sensor_state==0) {
case_state=1;

}
break ;
default : case_state=1;
break;
}

}

nicholastkx1995:
I am having a lot of trouble continuing from here as i can only count the number of time the light is blocked for the LDR.

Preferably 5 per sec, then it will trigger the Buzzer.

It's not obvious from this what the problem is. What is the code doing currently, and how is that different to what you want?

There's no problem till this stage.
till this stage this code will count the number of times the light is blocked.
i have no idea how to continue from here to count 300rpm and then sound the buzzer for warning.

cheers for replying.

The way you're detecting and counting state transition seems rather complicated. The State Change Detection shows a simpler way to implement the same thing.

There are several ways to work out the frequency of the pulses you're detecting. The easiest is to write some code that runs at regular intervals that reads the counter value, and then sets it back to zero. If you run the code once a second then the counter value gives you the frequency in Herts. The Blink without Delay example sketch shows you how to run code at regular intervals.

can you help me with this?

nicholastkx1995:
can you help me with this?

He did. He pointed you at Blink Without Delay, which shows you how to time things. It also shows you how to decide when to start or stop something.

Another way of calculating RPM is to time the duration between two events that hppen once per revolution. When the LDR becomes blocked, note the time (Blink Without Delay shows you how), then after it gord low, check the time of the next transition to blocked. This will give you the time in milliseconds.

If you know the time in milliseconds, you can easily calculate the number if times it will be blocked in seconds, minutes, hours, or whatever.

I have done the rpm code but i am having trouble continuing to activate the buzzer when a certain rpm is achieved. Please help me! Thank you.

int val;
long last=0;
int stat=LOW;
int stat2;
int contar=0;

int sens=950; // this value indicates the limit reading between dark and light,
// it has to be tested as it may change acording on the
// distance the leds are placed.
int nPalas=4; // the number of blades of the propeller

int milisegundos=500; // the time it takes each reading
void setup()
{
Serial.begin(9600);
pinMode(A13,OUTPUT);

}

void loop()
{
val=analogRead(0);
if(val<sens)
stat=LOW;
else
stat=HIGH;
digitalWrite(A13,stat); //as iR light is invisible for us, the led on pin 13
//indicate the state of the circuit.

if(stat2!=stat){ //counts when the state change, thats from (dark to light) or
//from (light to dark), remmember that IR light is invisible for us.
contar++;
stat2=stat;
}
if(millis()-last>=milisegundos){
double rps=((double)contar/nPalas)/2.01000.0/milisegundos;
double rpm=((double)contar/nPalas)/2.0
60000.0/(milisegundos);
Serial.print((contar/2.0));Serial.print(" RPS ");Serial.print(rps);
Serial.print(" RPM");Serial.print(rpm);Serial.print(" VAL ");Serial.println(val);
contar=0;
last=millis();
}

}