Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MIDVAL 128
- #define FULLREV 240.0
- int digital1 = 0;
- int digital2 = 0;
- int previousValue = 0;
- int totalParts = 0;
- bool readAlready = false;
- int pTotal = 0;
- int timer = 0;
- double intervalStart = 0;
- double intervalEnd = 0;
- int previousTime = 0;
- void setup() {
- // put your setup code here, to run once:
- pinMode(0, INPUT);
- pinMode(1, INPUT);
- Serial.begin(115200);
- previousValue = readCyclePart(digital1, digital2);
- readAlready = true;
- intervalStart = millis();
- }
- void loop() {
- timer = millis();
- digital1 = digitalRead(2);
- digital2 = digitalRead(3);
- countStep();
- if(timer % 200 == 0 && millis() != previousTime) {
- previousTime = millis();
- //Serial.print("Parts: ");
- Serial.print(totalParts);
- double rpm = calculateRpm();
- //Serial.print(", RPM: ");
- Serial.print(" ");
- Serial.print(rpm);
- Serial.println();
- totalParts = 0;
- }
- pTotal = totalParts;
- }
- int readCyclePart(int a, int b){
- bool boolA = a==HIGH;
- bool boolB = b==HIGH;
- if(!boolA && !boolB)
- return 0;
- else if(boolA && !boolB)
- return 1;
- else if(boolA && boolB)
- return 2;
- else if(!boolA && boolB)
- return 3;
- }
- void countStep(){
- int currentValue = readCyclePart(digital1, digital2);
- int diff = 0;
- if(previousValue > currentValue){
- diff = currentValue + 4 - previousValue;
- }else{
- diff = currentValue - previousValue;
- }
- totalParts += diff;
- previousValue = currentValue;
- }
- double calculateRpm(){
- intervalEnd = millis();
- double elapsedTime = intervalEnd - intervalStart;
- double minuteScaleFactor = 60000/elapsedTime;
- double rotationalFraction = totalParts/FULLREV;
- double rpm = rotationalFraction/elapsedTime*minuteScaleFactor;
- intervalStart = millis();
- return rpm;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement