Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <SoftwareSerial.h>
- //#define rxPin 9
- //#define txPin 10
- //
- //SoftwareSerial mySerial(rxPin, txPin);
- unsigned long start;
- const byte encoderPinA = 2;//A pin -> interrupt pin 0
- const byte encoderPinB = 4;//B pin -> digital pin 4
- volatile long pulse;
- volatile bool pinB, pinA, dir;
- const byte ppr = 8, upDatesPerSec = 10;
- const int fin = 100;
- const float konstant = (60.0 * upDatesPerSec / (16));
- double rpm = 0, rpm2 = 0, rpm3 = 0, rpm4 = 0, rpm5 = 0,
- rpm6 = 0, rpm7 = 0, rpm8 = 0, rpm9 = 0, rpm10 = 0,
- rpm11 = 0, rpm12 = 0, rpm13 = 0, rpm14 = 0, rpm15 = 0,
- rpm16 = 0, rpm17 = 0, rpm18 = 0, rpm19 = 0, rpm20 = 0,
- rpm21 = 0, rpm22 = 0, rpm23 = 0, rpm24 = 0, rpm25 = 0,
- rpm26 = 0, rpm27 = 0, rpm28 = 0, rpm29 = 0, rpm30 = 0,
- rpm31 = 0, rpm32 = 0, rpmt = 0;
- int val, previousState , currentState, readEncoder = 2;
- void setup() {
- // pinMode(rxPin, INPUT);
- // pinMode(txPin, OUTPUT);
- // mySerial.begin(9600);
- Serial.begin(9600);
- // Serial1.begin(9600);
- // attachInterrupt(0, readEncoder, RISING);
- pinMode(encoderPinA, INPUT);
- pinMode(encoderPinB, INPUT);
- pinMode(readEncoder, INPUT);
- }
- void loop() {
- // analogWrite(10, 255);
- val = digitalRead(readEncoder); // read input value
- if (val == HIGH) { // check if the input is HIGH (button released)
- // digitalWrite(ledPin, HIGH); // turn LED on
- currentState = 1;
- }
- else {
- // digitalWrite(ledPin, LOW); // turn LED off
- currentState = 0;
- }
- if (currentState != previousState) {
- if (currentState == 1) {
- pulse++;
- // counter = counter + 1;
- // Serial.println(counter);
- }
- }
- previousState = currentState;
- if (millis() - start > fin)
- {
- start = millis();
- rpm = pulse * konstant;
- rpmt = (rpm + rpm2 + rpm3 + rpm4 + rpm5 + rpm6 + rpm7 + rpm8 + rpm9 + rpm10 + rpm11 + rpm12 + rpm13 + rpm14 + rpm15 + rpm16) / 16;
- // rpmt = (rpm + rpm2 + rpm3 + rpm4 + rpm5 + rpm6 + rpm7 + rpm8 + rpm9 + rpm10 + rpm11 + rpm12 + rpm13 + rpm14 + rpm15+rpm16 + rpm17 + rpm18+ rpm19 + rpm20 + rpm21 + rpm22 + rpm23 + rpm24 + rpm25 + rpm26 + rpm27 + rpm28 + rpm29 + rpm30 + rpm31 + rpm32) / 32;
- Serial.println(rpmt);
- // mySerial.println("asup");
- // Serial.print(rpm);
- // Serial.print("\t");
- // Serial.println(pulse);
- // rpm32 = rpm31;
- // rpm31 = rpm30;
- // rpm30 = rpm29;
- // rpm29 = rpm28;
- // rpm28 = rpm27;
- // rpm27 = rpm26;
- // rpm26 = rpm25;
- // rpm25 = rpm24;
- // rpm24 = rpm23;
- // rpm23 = rpm22;
- // rpm22 = rpm21;
- // rpm21 = rpm20;
- //
- // rpm20 = rpm19;
- // rpm19 = rpm18;
- // rpm18 = rpm17;
- // rpm17 = rpm16;
- rpm16 = rpm15;
- rpm15 = rpm14;
- rpm14 = rpm13;
- rpm13 = rpm12;
- rpm12 = rpm11;
- rpm11 = rpm10;
- rpm10 = rpm9;
- rpm9 = rpm8;
- rpm8 = rpm7;
- rpm7 = rpm6;
- rpm6 = rpm5;
- rpm5 = rpm4;
- rpm4 = rpm3;
- rpm3 = rpm2;
- rpm2 = rpm;
- pulse = 0;
- }
- }
- //void readEncoder() {
- // // state = !state;
- // // delay(10);
- // static unsigned long last_interrupt_time = 0;
- // unsigned long interrupt_time = millis();
- // // If interrupts come faster than 200ms, assume it's a bounce and ignore
- // if (interrupt_time - last_interrupt_time > 10)
- // {
- // pulse++;
- // }
- // last_interrupt_time = interrupt_time;
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement