Advertisement
Guest User

arduino stuff

a guest
Jan 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #define MIDVAL 128
  2. #define FULLREV 240.0
  3. int digital1 = 0;
  4. int digital2 = 0;
  5. int previousValue = 0;
  6. int totalParts = 0;
  7. bool readAlready = false;
  8.  
  9. int pTotal = 0;
  10. int timer = 0;
  11. double intervalStart = 0;
  12. double intervalEnd = 0;
  13. int previousTime = 0;
  14.  
  15. void setup() {
  16.   // put your setup code here, to run once:
  17.   pinMode(0, INPUT);
  18.   pinMode(1, INPUT);
  19.   Serial.begin(115200);
  20.   previousValue = readCyclePart(digital1, digital2);
  21.   readAlready = true;
  22.   intervalStart = millis();
  23. }
  24.  
  25. void loop() {
  26.   timer = millis();
  27.   digital1 = digitalRead(2);
  28.   digital2 = digitalRead(3);
  29.   countStep();
  30.   if(timer % 200 == 0 && millis() != previousTime) {
  31.     previousTime = millis();
  32.     //Serial.print("Parts: ");
  33.     Serial.print(totalParts);
  34.     double rpm = calculateRpm();
  35.     //Serial.print(", RPM: ");
  36.     Serial.print(" ");
  37.     Serial.print(rpm);
  38.     Serial.println();
  39.     totalParts = 0;
  40.   }
  41.   pTotal = totalParts;
  42. }
  43.  
  44. int readCyclePart(int a, int b){
  45.   bool boolA = a==HIGH;
  46.   bool boolB = b==HIGH;
  47.  
  48.   if(!boolA && !boolB)
  49.     return 0;
  50.   else if(boolA && !boolB)
  51.     return 1;
  52.   else if(boolA && boolB)
  53.     return 2;
  54.   else if(!boolA && boolB)
  55.     return 3;
  56. }
  57.  
  58. void countStep(){
  59.   int currentValue = readCyclePart(digital1, digital2);
  60.   int diff = 0;
  61.   if(previousValue > currentValue){
  62.     diff = currentValue + 4 - previousValue;
  63.   }else{
  64.     diff = currentValue - previousValue;
  65.   }
  66.   totalParts += diff;
  67.   previousValue = currentValue;
  68. }
  69.  
  70. double calculateRpm(){
  71.     intervalEnd = millis();
  72.     double elapsedTime = intervalEnd - intervalStart;
  73.     double minuteScaleFactor = 60000/elapsedTime;
  74.     double rotationalFraction = totalParts/FULLREV;
  75.     double rpm = rotationalFraction/elapsedTime*minuteScaleFactor;
  76.     intervalStart = millis();
  77.     return rpm;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement