Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #define NOFIELD 505L // Calibration constant
  2. #define TOMILLIGAUSS 1953L // Unit conversion factor
  3.  
  4. #define P_led 13
  5. #define R_led 12
  6. #define N_led 11
  7. #define D_led 10
  8. #define S_led 9
  9. #define U_led 8
  10.  
  11. #define numLED 5
  12.  
  13. struct interval
  14. {
  15. int lower;
  16. int upper;
  17. };
  18.  
  19. struct state
  20. {
  21. char name;
  22. int led;
  23. interval gauss1;
  24. interval gauss2;
  25. } ;
  26.  
  27. state shift_condition[] = {
  28. { 'P', P_led, { 74, 95 }, { -86, -40 } },
  29. { 'R', R_led, { 61, 92 }, { -20, 10 } },
  30. { 'N', N_led, { -30, 6 }, { 36, 90 } },
  31. { 'D', D_led, { -80, -53 }, { 2, 50 } },
  32. { 'S', S_led, { -25, 15 }, { -80, -25 } },
  33. };
  34.  
  35. state prev_state;
  36. long prev_gauss1;
  37. long prev_gauss2;
  38. bool gear_defined;
  39.  
  40. bool inRange( long value, struct interval range )
  41. {
  42. return range.lower <= value && value <= range.upper;
  43. }
  44.  
  45. void selectGear(int raw1, int raw2)
  46. {
  47.  
  48. long gauss1 = ( raw1 - NOFIELD ) * TOMILLIGAUSS / 1000;
  49. long gauss2 = ( raw2 - NOFIELD ) * TOMILLIGAUSS / 1000;
  50.  
  51. // if no signal, no need to switch gear, bail out
  52. if ( prev_gauss1 == gauss1 && prev_gauss2 == gauss2 ) {
  53. return;
  54. }
  55.  
  56. // update values for next evaluation
  57.  
  58. prev_gauss1 = gauss1;
  59. prev_gauss2 = gauss2;
  60.  
  61. // if new readings in existing state range,
  62. // no need to switch gear, bail out
  63. if ( gear_defined && inRange(gauss1, prev_state.gauss1) && inRange(gauss2,prev_state.gauss2) ) {
  64. return;
  65. }
  66.  
  67. for( int i=0; i<numLED; i++) { // determine new state
  68.  
  69. state curr_state = shift_condition[i];
  70.  
  71. if ( inRange(gauss1, curr_state.gauss1) && inRange(gauss2, curr_state.gauss2) ) {
  72.  
  73. Serial.print("Switching from ");
  74. Serial.print(prev_state.name);
  75. Serial.print(" to ");
  76. Serial.println(curr_state.name);
  77.  
  78. digitalWrite(prev_state.led, LOW);
  79. digitalWrite(curr_state.led, HIGH);
  80.  
  81. prev_state = curr_state;
  82. gear_defined = true;
  83. return;
  84. }
  85. }
  86.  
  87. // no states matched, default to "undefined" state
  88.  
  89. gear_defined = false;
  90. digitalWrite(prev_state.led, LOW);
  91. digitalWrite(U_led, HIGH);
  92. }
  93.  
  94. void setup()
  95. {
  96. Serial.begin(9600);
  97. pinMode(P_led, OUTPUT);
  98. pinMode(R_led, OUTPUT);
  99. pinMode(N_led, OUTPUT);
  100. pinMode(D_led, OUTPUT);
  101. pinMode(S_led, OUTPUT);
  102. pinMode(U_led, OUTPUT);
  103. }
  104.  
  105. void loop()
  106. {
  107. selectGear(analogRead(1),analogRead(2));
  108. }
  109.  
  110. struct state
  111. {
  112. char name;
  113. int led;
  114. struct interval gauss1;
  115. struct interval gauss2;
  116. };
  117.  
  118. typedef struct interval_s
  119. {
  120. int lower;
  121. int upper;
  122. } interval;
  123.  
  124. state shift_condition[] = {
  125.  
  126. state shift_condition[numLED] = {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement