Serial.write 导致无效循环程序停止(数字引脚停止响应)
Serial.write causing void loop program to stop (Digital Pins stop responding)
我在尝试通过 HC05 蓝牙模块通过 tx 和 rx 将一些串行数据发送到另一个 arduino 时遇到问题。
整个项目正在开发混合动力卡丁车,并将 arduino 用作简单的 ECU 单元,通过 PID 速度控制控制直流电机的 PWM 输出。我一直在逐步进行这个项目,甚至用 arduino 设置了一个脚踏板并直接控制了电子速度控制器 (ESC)。我为此添加了一个简单的 PID 功能以及一个简单的霍尔传感器来检测速度并且确实需要调整但到目前为止效果很好。现在,当我尝试通过串行端口发送数据时,问题就来了。
我已经将蓝牙模块连接到单独的 arduino,并成功地将数据从一个带有电位器输入的 arduino 发送到另一个带有 3.5 英寸 TFT 屏幕的 arduino。当我尝试将项目的主端集成到 PID 控制的直流电机时,系统冻结。从那以后我删除了 PID 控制并回到直接控制但它仍然失败,我已经尝试注释掉编码器的中断序列并为 RPM 设置一个静态值但仍然冻结。如果我不尝试使用任何数字输出,则发送序列有效。我真的很困惑。可以在下面找到我尝试调试的代码。这不是完整的代码,已被切碎以尝试消除此错误。但是在下面的这段代码中,如果我注释掉 sendData 函数,系统就会工作,并且电机会以相对于踏板输入的速度旋转。一旦我尝试发送数据,系统就会运行几秒钟然后冻结。数据仍在发送,静态读数仅显示数字输出开始工作。
#include <TimerOne.h>
int previous = 0;
int Tavg = 0; // the average
int Tout = 0;
int throttle = A0;
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int ESCPin = 5;
unsigned int counter=0;
int RPM;
long Time = 0;
long ReadInt = 0;
void docount() // counts from the speed sensor
{
counter++; // increase +1 the counter value
}
void timerIsr()
{
Timer1.detachInterrupt(); //stop the timer
Serial.print("Motor Speed: ");
RPM = (counter*75 ); // RPM= counterx10*60/8 (x10 for second, 8 counts in encoder, 60 minutes === 75x counter)
Serial.print(RPM);
Serial.println(" Rotation per min"); Serial.print(Tout);Serial.print("= "); Serial.print(Tout*0.01961);Serial.println("V");
counter=0; // reset counter to zero
Timer1.attachInterrupt( timerIsr ); //enable the timer
}
void ReadEnc (){
Timer1.initialize(100000); // set timer for 0.1sec
attachInterrupt(0, docount, RISING); // increase counter when speed sensor pin goes High
Timer1.attachInterrupt( timerIsr ); // enable the timer
}
void sendData(){
if (Serial.available()>0) {
if (Serial.read() == 0){
//Serial.println(sendChars);
int RPMout = RPM;
Serial.write("<");
//delay(2);
//Serial.write(sendChars);
Serial.write("Data is,");
//delay(2);
Serial.write( itoa (RPMout, 4,10));
//delay(2);
Serial.write(", 30, 48.35");
//delay(2);
Serial.write(">");
//delay(10);
Serial.println("");
}
}
}
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
pinMode(ESCPin, OUTPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0; }
Time = millis();
ReadInt = -100;
}
void ReadSensor (){
// get the sensor value
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(throttle);
//Serial.println(readings[readIndex]);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
Tavg = total / numReadings;
}
void loop(){
ReadSensor();
ReadEnc();
RPM = 1800;
Tout = map(Tavg, 180, 860, 0, 200);
if (Tout>0){
analogWrite(ESCPin, Tout);
}
if (Time > ReadInt + 5000) {
sendData (); // when this is commented it works fine, tried moving it everywhere
ReadInt = Time;
}
Time = millis();
}
如果有人有任何想法,请告诉我,我知道我可能没有很好地解释我的问题,所以如果他们有任何问题或需要更多详细信息,请询问。
我在尝试通过 HC05 蓝牙模块通过 tx 和 rx 将一些串行数据发送到另一个 arduino 时遇到问题。
整个项目正在开发混合动力卡丁车,并将 arduino 用作简单的 ECU 单元,通过 PID 速度控制控制直流电机的 PWM 输出。我一直在逐步进行这个项目,甚至用 arduino 设置了一个脚踏板并直接控制了电子速度控制器 (ESC)。我为此添加了一个简单的 PID 功能以及一个简单的霍尔传感器来检测速度并且确实需要调整但到目前为止效果很好。现在,当我尝试通过串行端口发送数据时,问题就来了。
我已经将蓝牙模块连接到单独的 arduino,并成功地将数据从一个带有电位器输入的 arduino 发送到另一个带有 3.5 英寸 TFT 屏幕的 arduino。当我尝试将项目的主端集成到 PID 控制的直流电机时,系统冻结。从那以后我删除了 PID 控制并回到直接控制但它仍然失败,我已经尝试注释掉编码器的中断序列并为 RPM 设置一个静态值但仍然冻结。如果我不尝试使用任何数字输出,则发送序列有效。我真的很困惑。可以在下面找到我尝试调试的代码。这不是完整的代码,已被切碎以尝试消除此错误。但是在下面的这段代码中,如果我注释掉 sendData 函数,系统就会工作,并且电机会以相对于踏板输入的速度旋转。一旦我尝试发送数据,系统就会运行几秒钟然后冻结。数据仍在发送,静态读数仅显示数字输出开始工作。
#include <TimerOne.h>
int previous = 0;
int Tavg = 0; // the average
int Tout = 0;
int throttle = A0;
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int ESCPin = 5;
unsigned int counter=0;
int RPM;
long Time = 0;
long ReadInt = 0;
void docount() // counts from the speed sensor
{
counter++; // increase +1 the counter value
}
void timerIsr()
{
Timer1.detachInterrupt(); //stop the timer
Serial.print("Motor Speed: ");
RPM = (counter*75 ); // RPM= counterx10*60/8 (x10 for second, 8 counts in encoder, 60 minutes === 75x counter)
Serial.print(RPM);
Serial.println(" Rotation per min"); Serial.print(Tout);Serial.print("= "); Serial.print(Tout*0.01961);Serial.println("V");
counter=0; // reset counter to zero
Timer1.attachInterrupt( timerIsr ); //enable the timer
}
void ReadEnc (){
Timer1.initialize(100000); // set timer for 0.1sec
attachInterrupt(0, docount, RISING); // increase counter when speed sensor pin goes High
Timer1.attachInterrupt( timerIsr ); // enable the timer
}
void sendData(){
if (Serial.available()>0) {
if (Serial.read() == 0){
//Serial.println(sendChars);
int RPMout = RPM;
Serial.write("<");
//delay(2);
//Serial.write(sendChars);
Serial.write("Data is,");
//delay(2);
Serial.write( itoa (RPMout, 4,10));
//delay(2);
Serial.write(", 30, 48.35");
//delay(2);
Serial.write(">");
//delay(10);
Serial.println("");
}
}
}
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
pinMode(ESCPin, OUTPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0; }
Time = millis();
ReadInt = -100;
}
void ReadSensor (){
// get the sensor value
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(throttle);
//Serial.println(readings[readIndex]);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
Tavg = total / numReadings;
}
void loop(){
ReadSensor();
ReadEnc();
RPM = 1800;
Tout = map(Tavg, 180, 860, 0, 200);
if (Tout>0){
analogWrite(ESCPin, Tout);
}
if (Time > ReadInt + 5000) {
sendData (); // when this is commented it works fine, tried moving it everywhere
ReadInt = Time;
}
Time = millis();
}
如果有人有任何想法,请告诉我,我知道我可能没有很好地解释我的问题,所以如果他们有任何问题或需要更多详细信息,请询问。