I have a rotary encoder connected to my arduino to measure velocity, how can I read that output with matlab?

11 次查看(过去 30 天)
Hello, I am new to Arduino and connection to matlab, but i have a project in which one part required reading velocity with a rotary encoder. Is there a way to read that output and save it with matlab? thanks!
Here is the code
void setup() {
Serial.begin (9600);
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
pinMode(3, INPUT_PULLUP); // internalเป็น pullup input pin 3
pinMode(7, OUTPUT);
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
attachInterrupt(1, ai1, RISING);
}
void loop() {
// Send the value of counter
// if( counter != temp ){
// Serial.println (counter);
// temp = counter;
newposition = encoder0Pos;
newtime = millis();
vel = abs((newposition-oldposition)) * 1000 /(newtime-oldtime);
Serial.print ("old position:");
Serial.print (oldposition);
Serial.print ("new position:");
Serial.print (newposition);
Serial.print ("speed = ");
Serial.println (vel);
digitalWrite (7,vel);
oldposition = newposition;
oldtime = newtime;
delay(250);
}
//}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(3)==LOW) {
//counter++;
newposition++;
}else{
//counter--;
newposition--;
}
}
void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(2)==LOW) {
//counter--;
newposition--;
}else{
//counter++;
newposition++;
}
}

回答(1 个)

Darshan Ramakant Bhat

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by