Advertisement
Ali_Nagy

Untitled

Apr 26th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. #define STANDBY_PHASE -1
  2. #define ERROR_PHASE 0
  3. #define CALCULATION_PHASE 1
  4.  
  5. #define NEGATIVE -1
  6. #define POSITIVE 1
  7.  
  8. #define DEFAULT_VALUE 500
  9. #define DIFF_SENSITIVITY 0.1
  10.  
  11. int phase = STANDBY_PHASE;
  12. int value = 0;
  13. int value_max = 0;
  14. int value_min = 1000;
  15. int value_diff = 0;
  16. int phase_ticks = 0;
  17. int previous_value_diff = 0;
  18.  
  19. boolean sensitivity_check() {
  20.   int upper_check = previous_value_diff * (1 + DIFF_SENSITIVITY);
  21.   int lower_check = previous_value_diff * (1 - DIFF_SENSITIVITY);
  22.   if (value_diff >= lower_check && value_diff <= upper_check) return 1;
  23.   else return 0;
  24. }
  25.  
  26. boolean presenceCheck(){
  27.  
  28. }
  29.  
  30.  
  31. void readCommand() {
  32.   if (Serial.available() > 0) {
  33.     char tempChar = Serial.read();
  34.     switch (tempChar) {
  35.       case 'A': {
  36.           phase = STANDBY_PHASE;
  37.           resetAll();
  38.         }
  39.       case 'B' : {
  40.           phase = ERROR_PHASE;
  41.         }
  42.     }
  43.   }
  44. }
  45.  
  46. void setup() {
  47.   // put your setup code here, to run once:
  48.   Serial.begin(4800);
  49. }
  50. void loop() {
  51.   readCommand();
  52.   value = analogRead(A2);
  53.   if (value > value_max) {
  54.     value_max = value;
  55.   } else if (value < value_min) {
  56.     value_min = value;
  57.   }
  58.   previous_value_diff = value_diff;
  59.   value_diff = value_max - value_min;
  60.   switch (phase) {
  61.     case ERROR_PHASE: {
  62.         debug_Print();
  63.         if (value_diff > 60 || value_max == 1000 || value_min == 0) {
  64.           reset_min();
  65.           reset_max();
  66.         } else {
  67.           if (sensitivity_check()) {
  68.             Serial.println("CHECK");
  69.             phase_ticks++;
  70.           }
  71.           if (phase_ticks == 20) {
  72.             Serial.println("DONE");
  73.             phase = CALCULATION_PHASE;
  74.           }
  75.         }
  76.       }
  77.     case CALCULATION_PHASE: {
  78.         int temp_diff = value -
  79.       }
  80.   }
  81.  
  82. }
  83.  
  84. void reset_min() {
  85.   value_min = DEFAULT_VALUE;
  86. }
  87. void reset_max() {
  88.   value_max = DEFAULT_VALUE;
  89. }
  90. void reset_value() {
  91.   value = 0;
  92. }
  93. void reset_value_diff() {
  94.   value_diff = 0;
  95. }
  96. void reset_previous_value_diff() {
  97.   previous_value_diff = 0;
  98. }
  99. void reset_phase_ticks() {
  100.   phase_ticks = 0;
  101. }
  102. void resetAll() {
  103.   reset_value();
  104.   reset_min();
  105.   reset_max();
  106.   reset_value_diff();
  107.   reset_previous_value_diff();
  108.   reset_phase_ticks();
  109. }
  110.  
  111.  
  112. void debug_Print() {
  113.   Serial.print("value: ");
  114.   Serial.print(value);
  115.   Serial.print(" value_max: ");
  116.   Serial.print(value_max);
  117.   Serial.print(" value_min: ");
  118.   Serial.print(value_min);
  119.   Serial.print(" value_diff: ");
  120.   Serial.println(value_diff);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement