Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Using a non-latching analog Hall Effect sensor to count rotations.
- Using my ATtiny2313 4 digit 7 segment display (I2C)
- Arduino -> board
- A4 -> MOSI (orange)
- A5 -> MISO (yellow)
- */
- #include <Wire.h>
- #define SENSOR_PIN 0 // analog pin connected to the Hall Effect sensor
- #define RESET_PIN 11 // digital pin for a counter reset button
- #define INCR_PIN 10 // digital pin to manually increment the counter
- #define DECR_PIN 12 // digital pin to manually decrement the counter
- const int SLAVE_ADDRESS = 0x01;
- const uint8_t FORMAT_DECIMAL = 0b01000000; // for sending to the display
- int counter = 0; // How many coils have been wound
- int sensor_threshold = 540; // how close the magnet has to be to trigger a count increase
- int sensor_tolerance = 5; // used for basic hysteresis
- int triggered = 0; // whether the count has been triggered this revolution
- volatile unsigned long last_button_press = 0; // for button debouncing
- int debounce_interval = 100; // milliseconds
- void setup() {
- // put your setup code here, to run once:
- Wire.begin();
- Serial.begin(9600);
- pinMode(RESET_PIN, INPUT);
- pinMode(INCR_PIN, INPUT);
- pinMode(DECR_PIN, INPUT);
- digitalWrite(RESET_PIN, HIGH); // enable internal pullup
- digitalWrite(INCR_PIN, HIGH);
- digitalWrite(DECR_PIN, HIGH);
- sendCount(counter); // initialise the display
- }
- void loop() {
- // put your main code here, to run repeatedly:
- //Serial.print("Starting\n");
- int hall_value = analogRead(SENSOR_PIN);
- if (hall_value > sensor_threshold + sensor_tolerance) {
- if (triggered == 0) {
- counter++;
- triggered = 1;
- sendCount(counter);
- Serial.print(counter);
- Serial.print("\n");
- }
- } else if (hall_value < sensor_threshold - sensor_tolerance) {
- triggered = 0;
- }
- // Check for manual adjustments
- if (digitalRead(RESET_PIN) == LOW) {
- Serial.print("Reset\n");
- reset_counter();
- } else if (digitalRead(INCR_PIN) == LOW) {
- Serial.print("Increment\n");
- increment_counter();
- } else if (digitalRead(DECR_PIN) == LOW) {
- Serial.print("Decrement\n");
- decrement_counter();
- }
- }
- void sendCount(int counter) {
- // todo: send counter to display
- uint8_t bytes[5] = {FORMAT_DECIMAL, 0, 0, 0, 0}; // the display expects 5 bytes
- bytes[3] = counter/256;
- bytes[4] = counter % 256;
- Wire.beginTransmission(SLAVE_ADDRESS);
- for (int i=0;i<5;i++) {
- Wire.write((byte)bytes[i]);
- Serial.print(bytes[i]);
- Serial.print(" | ");
- }
- Wire.endTransmission();
- Serial.print("\n");
- }
- void counterTest(int limit) {
- for (int i=0;i<limit;i++) {
- sendCount(i);
- Serial.print(i);
- delay(100);
- }
- }
- void reset_counter() {
- if (millis() > last_button_press + debounce_interval) {
- counter = 0;
- last_button_press = millis();
- }
- sendCount(counter);
- }
- void increment_counter() {
- if (millis() > last_button_press + debounce_interval) {
- counter++;
- last_button_press = millis();
- }
- sendCount(counter);
- }
- void decrement_counter() {
- if (millis() > last_button_press + debounce_interval) {
- counter--;
- last_button_press = millis();
- }
- sendCount(counter);
- }
- void show_hall_effect() {
- // endless loop for now, just displays the reading from the sensor
- int reading = 0;
- for(;;) {
- reading = analogRead(SENSOR_PIN);
- sendCount(reading);
- delay(100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement