0

我使用 lm393 速度计和 arduino uno。并给出 250 rpm 电机的 2000 rpm 等值。这正常吗?我可以将其用作大满贯的里程计数据吗?是关于我的代码还是我的编码器位置。我使用 20 刻度编码器。我的位置是正确的,我检查了它。

这是我的arduino代码:

#include "TimerOne.h"
int pin1 = 4;
int pin2 = 5;
int pin3 = 6;
int pin4 = 7;
const byte MOTOR2 = 2;
const byte MOTOR2 = 3;  
unsigned int counter1 = 0;
unsigned int counter2 = 0;
float diskslots = 20.00;
// Interrupt Service Routines
// Motor 1 pulse count ISR
void ISR_count1()
{
counter1++;  // increment Motor 1 counter value
 }
// Motor 2 pulse count ISR
void ISR_count2()
{
  counter2++;
  // increment Motor 2 counter value
}
void ISR_timerone()
{
  Timer1.detachInterrupt();  // Stop the 
  Serial.print("Motor Speed 1: ");
  float rotation1 = (counter1 / diskslots) *60;  
  Serial.print(rotation1);
  Serial.print(" RPM - ");
  counter1 = 0;  //  reset counter to zero
  Serial.print("Motor Speed 2: ");
  float rotation2 = (counter2 / diskslots) * 60;  
  Serial.print(rotation2);
  Serial.println(" RPM");
  counter2 = 0;  //  reset counter to zero
  Timer1.attachInterrupt( ISR_timerone );  
}
void setup() {
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  Timer1.initialize(1000000); // set timer for 1sec
  attachInterrupt(digitalPinToInterrupt (MOTOR1), ISR_count1, RISING);  
  attachInterrupt(digitalPinToInterrupt (MOTOR2), ISR_count2, RISING);  
  Timer1.attachInterrupt( ISR_timerone );
  Serial.begin(9600);
}

4

0 回答 0