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

In summary: Read(encoderIn)) count++; //read the pulse output if (digitalRead(pulseOutput)) count /= 2; //if pulse output is on, divide by two to make it a per-minute value //send the data to the serial port Serial.write(count); //clear the variable }In summary, the code reads the encoder statuses and the pulse output, and sends the data to the serial
  • #1
Weightofananvil
34
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
  • #2
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.
 
  • #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.
 

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

What is a motor encoder?

A motor encoder is a device that measures the rotational position and speed of a motor. It typically consists of a rotating disc with evenly spaced markings and a sensor that detects the movement of the markings.

Why is C coding used for motor encoders?

C is a widely used programming language that is known for its speed and efficiency. It is commonly used in embedded systems, making it a suitable choice for coding motor encoders, which are often used in industrial and robotics applications.

How do you code a motor encoder in C?

To code a motor encoder in C, you will need to first initialize the necessary variables and set up the input and output pins. Then, you will need to use a timer interrupt to read the data from the encoder and calculate the position and speed of the motor. Finally, you can output the data to a display or use it for further processing.

What are the potential challenges when coding a motor encoder in C?

One of the main challenges when coding a motor encoder in C is ensuring the accuracy and reliability of the data. This can be affected by factors such as noise, vibrations, and mechanical errors. It is important to carefully design and test the code to minimize these issues.

Are there any alternatives to C for coding motor encoders?

Yes, there are other programming languages that can be used for coding motor encoders, such as C++, Python, and Java. However, C is still a popular choice due to its speed, efficiency, and widespread use in embedded systems.

Similar threads

Replies
7
Views
1K
  • Electrical Engineering
Replies
3
Views
8K
Replies
22
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
Replies
3
Views
10K
  • Programming and Computer Science
Replies
11
Views
4K
  • Computing and Technology
Replies
1
Views
6K
  • Electrical Engineering
Replies
5
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top