/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Author: Jay Weeks * Created: 10-May-2016 * * Description: * This is a data logger for measuring encoder times for a motor * equipped with the super high-tech encoder from that one * Instructable I made! * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Variables * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ #define arrMax 2048 // This will define how many values we can // have in our array. // PWM value #define PWMval 255 unsigned int values[arrMax]; // This array will store our time // measurements. unsigned int count = 0; // This will count how many values we // have in our array. unsigned int i = 0; // Generic index. // Variables to record encoder times unsigned int encoder_prev = 0, encoder_dif = 0; /* ~~~~~~~~~~~~~~~~ * Sliding Cutoff Values * ~~~~~~~~~~~~~~~~ */ #define k_cutoff 0.50 #define curvature 2112000 #define PWMmin 45 #define Min_time 36000 unsigned int cutoff = k_cutoff * ((curvature/(PWMval - 39))+Min_time); /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Main Code Segment * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /* ~~~~~~~~~~~~~~~~ * Setup * ~~~~~~~~~~~~~~~~ */ void setup() { // Begin serial communication Serial.begin(9600); // Set our inputs pinMode(PIN_INT3, INPUT); // We're using interrupts 1 and 3 pinMode(PIN_INT1, INPUT); // These are on pins 3 and 2 (respectively) // Set our outputs pinMode(PIN_OC5, OUTPUT); // Create our interrupts attachInterrupt(1, logR, RISING); // int1: rising attachInterrupt(3, logF, FALLING); // int3: falling // Clear our encoder times for(count = 0; count < arrMax; count ++) { values[count] = 0; } // Reset count count = 0; // Initialize our timers encoder_prev = micros(); } /* ~~~~~~~~~~~~~~~~ * Loop * ~~~~~~~~~~~~~~~~ */ void loop() { // set the speed of the motor analogWrite(PIN_OC5, PWMval); // Check to see if we need to transmit if(count) { // Output our values for(i = 0; i < count; i ++) { Serial.println(values[i]); } // reset count count = 0; } } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Interrupt Service Routines * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /* ~~~~~~~~~~~~~~~~ * Falling * ~~~~~~~~~~~~~~~~ */ void logF() { // We only want to record the start time of the low segments encoder_prev = micros(); } /* ~~~~~~~~~~~~~~~~ * Rising * ~~~~~~~~~~~~~~~~ */ void logR() { // We've reached the end of our low segment // Get the time since we went low encoder_dif = micros() - encoder_prev; // Discard anything lower than our sliding cutoff if (encoder_dif >= cutoff) { // If our measure is greater than our sliding cutoff // Stick it into our values values[count] = encoder_dif; count ++; // Increment our count } } // I hate typing at the very bottom of the page, don't you?