Advertisement
grist

Coil Counter

Apr 29th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. /*
  2.  
  3. Using a non-latching analog Hall Effect sensor to count rotations.
  4.  
  5. Using my ATtiny2313 4 digit 7 segment display (I2C)
  6. Arduino -> board
  7. A4 -> MOSI (orange)
  8. A5 -> MISO (yellow)
  9.  
  10. */
  11.  
  12. #include <Wire.h>
  13.  
  14. #define SENSOR_PIN 0 // analog pin connected to the Hall Effect sensor
  15. #define RESET_PIN 11 // digital pin for a counter reset button
  16. #define INCR_PIN 10 // digital pin to manually increment the counter
  17. #define DECR_PIN 12 // digital pin to manually decrement the counter
  18.  
  19. const int SLAVE_ADDRESS = 0x01;
  20. const uint8_t FORMAT_DECIMAL =  0b01000000; // for sending to the display
  21.  
  22. int counter = 0; // How many coils have been wound
  23. int sensor_threshold = 540; // how close the magnet has to be to trigger a count increase
  24. int sensor_tolerance = 5; // used for basic hysteresis
  25. int triggered = 0; // whether the count has been triggered this revolution
  26.  
  27. volatile unsigned long last_button_press = 0; // for button debouncing
  28. int debounce_interval = 100; // milliseconds
  29.  
  30. void setup() {
  31.   // put your setup code here, to run once:
  32.   Wire.begin();
  33.   Serial.begin(9600);
  34.   pinMode(RESET_PIN, INPUT);
  35.   pinMode(INCR_PIN, INPUT);
  36.   pinMode(DECR_PIN, INPUT);
  37.   digitalWrite(RESET_PIN, HIGH); // enable internal pullup
  38.   digitalWrite(INCR_PIN, HIGH);
  39.   digitalWrite(DECR_PIN, HIGH);
  40.   sendCount(counter); // initialise the display
  41. }
  42.  
  43. void loop() {
  44.   // put your main code here, to run repeatedly:
  45.   //Serial.print("Starting\n");
  46.   int hall_value = analogRead(SENSOR_PIN);
  47.   if (hall_value > sensor_threshold + sensor_tolerance) {
  48.       if (triggered == 0) {
  49.           counter++;
  50.           triggered = 1;
  51.           sendCount(counter);
  52.           Serial.print(counter);
  53.           Serial.print("\n");
  54.       }
  55.   } else if (hall_value < sensor_threshold - sensor_tolerance) {
  56.       triggered = 0;
  57.   }
  58.   // Check for manual adjustments
  59.   if (digitalRead(RESET_PIN) == LOW) {
  60.       Serial.print("Reset\n");
  61.       reset_counter();
  62.   } else if (digitalRead(INCR_PIN) == LOW) {
  63.       Serial.print("Increment\n");
  64.       increment_counter();
  65.   } else if (digitalRead(DECR_PIN) == LOW) {
  66.       Serial.print("Decrement\n");
  67.       decrement_counter();
  68.   }
  69.  
  70. }
  71.  
  72. void sendCount(int counter) {
  73.     // todo: send counter to display
  74.     uint8_t bytes[5] = {FORMAT_DECIMAL, 0, 0, 0, 0}; // the display expects 5 bytes
  75.     bytes[3] = counter/256;
  76.     bytes[4] = counter % 256;
  77.     Wire.beginTransmission(SLAVE_ADDRESS);
  78.     for (int i=0;i<5;i++) {
  79.         Wire.write((byte)bytes[i]);
  80.         Serial.print(bytes[i]);
  81.         Serial.print(" | ");
  82.     }
  83.     Wire.endTransmission();
  84.     Serial.print("\n");
  85. }
  86.  
  87. void counterTest(int limit) {
  88.     for (int i=0;i<limit;i++) {
  89.         sendCount(i);
  90.         Serial.print(i);
  91.         delay(100);
  92.     }
  93.    
  94. }
  95.  
  96.  
  97. void reset_counter() {
  98.  
  99.   if (millis() > last_button_press + debounce_interval) {
  100.       counter = 0;
  101.       last_button_press = millis();
  102.   }
  103.   sendCount(counter);
  104. }
  105.  
  106. void increment_counter() {
  107.   if (millis() > last_button_press + debounce_interval) {
  108.       counter++;
  109.       last_button_press = millis();
  110.   }
  111.   sendCount(counter);
  112. }
  113.  
  114. void decrement_counter() {
  115.   if (millis() > last_button_press + debounce_interval) {
  116.       counter--;
  117.       last_button_press = millis();
  118.   }
  119.   sendCount(counter);
  120. }
  121.  
  122. void show_hall_effect() {
  123.     // endless loop for now, just displays the reading from the sensor
  124.      int reading = 0;
  125.  
  126.     for(;;) {
  127.         reading = analogRead(SENSOR_PIN);
  128.         sendCount(reading);
  129.         delay(100);  
  130.  
  131.     }    
  132.    
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement