Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C Coding a motor encoder

  1. Dec 11, 2016 #1
    Hi Everyone,
    I'm coding an circuit in arduino that reads a sensor to gauge a motors RPM.
    I have the arduino reading the speed and blinking an LED. Now I need some way of taking that data
    reading it for say half a second, perform calculations to double the count (to make it per minute)
    send the data and then clear the variable but I can't think of how to do that since C code
    is sequencial.....

    Im more looking for logical answers then someone to spit me out some code...
    Im starting to think Just make 2 variables... and make them alternate "turns" of counting
    then the one's loop will clear the others variable...

    any suggestions?
    Thanks



    int count = 0;

    const int encoderIn = 8; // input pin for the interrupter
    const int statusLED = 13; // Output pin for Status indicator
    const int pulseOutput = 12; // Pulse output pin for external interfacing
    int detectState=0; // Variable for reading the encoder status
    void setup()
    {
    pinMode(encoderIn , INPUT); //Set pin 8 as input
    pinMode(statusLED, OUTPUT); //Set pin 13 as output
    pinMode(pulseOutput, OUTPUT); // Set Pin 12 as output
    }
    void loop() {
    detectState=digitalRead(encoderIn);
    if (detectState == HIGH) { //If encoder output is high
    digitalWrite(statusLED, HIGH); //Turn on the status LED
    digitalWrite(pulseOutput,HIGH); // Give a logic-High level output

    }
    else {
    digitalWrite(statusLED, LOW); //Turn off the status LED
    digitalWrite(pulseOutput,LOW); // Give a logic-Low level output
    }

    }
     
  2. jcsd
  3. Dec 12, 2016 #2
    When you say 'reading the data for half a second' what you are actually saying is you are sampling the data at a certain frequency for 0.5 seconds and taking the average or some other means to turn the statistical data and turn it into a value. If so the following should help you:
    I would suggest creating a timer which is set to the number of samples per second as to gather enough datapoints for the time period you like to sample in. Once the sampling is done you trigger the statistical function and output it accordingly.
    An example in c code can be found here http://stackoverflow.com/questions/17167949/how-to-use-timer-in-c
    It leaves you free to do other stuff while the data is gathered and perform other tasks during that time.
     
  4. Dec 13, 2016 #3
    Half a second seems like a very long time for polling. Why so long? Most samples like that return returns every couple of milliseconds.

    What you want is a loop that continually polls the port. It'll read the data, add it to a variable, then increment a counter. After a few loops, divide the variable by the counter to get an average.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C Coding a motor encoder
  1. Critique my C++ code (Replies: 11)

Loading...