我目前正在尝试设置一个Arduino Uno和一个霍尔效应传感器(A3144)来测量电机的转速。我测试了传感器,当感应磁铁时,它显示"0“,当磁铁被移除时,它显示"1”。我还编写了我认为可以工作的代码,但当我测试我的程序时,串行监视器上什么也没有出现。如果任何人有任何想法,我可以改变我的代码,使其工作,我非常感谢一些建议!
下面是我的代码:
int sensorPin = 2; //Hall Effect Sensor at Digital Pin 2
float hall_thresh = 100.0; //Set number of hall trips for RPM reading
void setup() {
Serial.begin (115200); // setup serial - diagnostics - port
pinMode (sensorPin, INPUT); // setup pins
}
void loop() {
// preallocate values for tachometer
float hall_count = 1.0;
float start = micros();
bool on_state = false;
// counting the number of times the hall sensor is tripped
while (true) {
if (digitalRead(sensorPin) == 0) {
if (on_state == false) {
on_state = true;
hall_count += 1.0;
}
} else {
on_state = false;
}
if (hall_count >= hall_thresh) {
break;
}
}
// print information about Time and RPM
float end_time = micros();
float time_passed = ((end_time-start)/1000000.0);
Serial.print("Time Passed: ");
Serial.print(time_passed);
Serial.println("s");
float rpm_val = (hall_count / time_passed) * 60.0;
Serial.print(rpm_val);
Serial.println(" RPM");
delay(1); // set delay in between reads for stability
}发布于 2020-11-12 06:58:18
这是你的代码的重现,如果它不能工作,请不要恨我,我只是确认它可以编译并且没有调试完成。我有一个非常类似的设置,它在Teensy 4.0上作为引擎控制单元(ECU)操作,所以使用类似的方法获得RPM被证明是有效的。
int sensorPin = 3; // interupt pin on UNO, adjust according to board.
unsigned long value = 0;
int hall_thresh = 100; //Set number of hall trips for RPM reading
unsigned long startTime = 0;
void setup() {
Serial.begin(115200);
pinMode(sensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(sensorPin), addValue, RISING);
}
void loop() {
if (value >= hall_thresh) { // Math copied from StackOverflow question.
unsigned long endTime = micros();
unsigned long time_passed = ((endTime - startTime) / 1000000.0);
Serial.print("Time Passed: ");
Serial.print(time_passed);
Serial.println("s");
float rpm_val = (value / time_passed) * 60.0;
Serial.print(rpm_val);
Serial.println(" RPM");
value = 0;
startTime = micros();
}
}
void addValue() {
value++;
}请将您的问题,如果你有任何,我将很乐意帮助调试。
发布于 2020-12-08 00:51:26
我也有同样的问题,但我使用了stm32f103c8t6 (但这并不重要)。
问题出在"hall_thresh“变量中。由于从数字1 (float hall_count = 1.0;)开始计数,while循环等待了99次(将磁铁放在传感器附近和离开的99次),然后它会将其与"hall_thresh“进行比较。
如果hall_count等于hall_thresh,则while循环将中断,并将时间和转速发送到串行监视器。
因此,尝试将hall_thresh变量更改为10或5,并用手中的磁铁进行尝试。
下一次使用motor时,您将把hall_thresh变量改为更高的数字,因为motor的速度要快得多。
希望它能有所帮助:)
https://stackoverflow.com/questions/64786301
复制相似问题