Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float rps[4]={0.00,0.00,0.00,0.00};
- long int PRINT_PERIOD = 1000;
- long int lastTimePrinted = 0;
- void setup(){
- Serial.begin(9600);
- DDRD &= ~_BV (7); // pinMode (7, INPUT); // rps_1
- DDRB &= ~_BV (0); // pinMode (8, INPUT); // rps_2
- DDRC &= ~_BV (1); // pinMode (A1, INPUT); // rps_3
- DDRD &= ~_BV (2); // pinMode (2, INPUT); // rps_4
- }
- float rps_out(int k){
- static int lastReedState;
- static unsigned long lastTransition;
- static float rps_result;
- int reedState;
- unsigned long now;
- unsigned long revolutionTime;
- if ( k == 1 ) reedState = (PIND & _BV (7)) == 0; // digitalRead (7);
- else if ( k==2 ) reedState = (PINB & _BV (0)) == 0; // digitalRead (8);
- else if ( k==3 ) reedState = (PINC & _BV (1)) == 0; // digitalRead (A1);
- else if ( k==4 ) reedState = (PIND & _BV (2)) == 0; // digitalRead (2);
- // On a rising transition of the reed switch:
- if (reedState == HIGH && lastReedState == LOW) { // HIGH LOW measuring the low period // LOW HIGH measuring the high period
- // Compute time since last valid transition.
- now = micros();
- revolutionTime = now - lastTransition; // measuring the High period
- // Remember this transition.
- lastTransition = now;
- }
- // Remember last state.
- lastReedState = reedState;
- return rps_result;
- }
- void loop() {
- rps[0] = rps_out(1);
- rps[1] = rps_out(2);
- rps[2] = rps_out(3);
- rps[3] = rps_out(4);
- if (millis() - lastTimePrinted >= PRINT_PERIOD) {
- Serial.println(rps[0]);
- Serial.print('.');
- Serial.println(rps[1]);
- Serial.print('.');
- Serial.print(rps[2]);
- Serial.print('.');
- Serial.println(rps[3]);
- lastTimePrinted = millis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement