How to Handle Data from a Motor Encoder with C in Arduino?

AI Thread Summary
The discussion focuses on reading motor RPM data using an Arduino and how to process that data effectively in C. The user seeks to sample the data for half a second, double the count for a per-minute reading, and then clear the variable, while acknowledging the sequential nature of C code. Suggestions include creating a timer to gather data points efficiently and using a loop to continuously poll the encoder input, incrementing a counter and averaging the results after a set number of samples. The importance of sampling frequency and the potential inefficiency of a half-second polling interval are also highlighted. Overall, the conversation emphasizes logical approaches to data handling in Arduino programming.
Weightofananvil
Messages
34
Reaction score
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
}

}
 
Technology news on Phys.org
Weightofananvil said:
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...
}

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.
 
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top