代码上传导致'USB device has malfunctioned' Windows 错误



我遇到了与Arduino论坛上这篇文章中描述的相同的问题。我有一个轻微的偏差,因为我使用的是Arduino Leonardo,但除此之外,核心问题是相同的。

尝试将草图上传到我的看板会导致 Windows 显示我的"USB 设备出现故障,Windows 无法识别它"。然后,用于主板的COM端口消失,如上面的帖子所示。

我尝试了路易斯戴维斯在链接帖子中发布的解决方案,这让我能够成功重置电路板并上传已知良好的草图。完成此操作后,该板能够再次被Windows识别,并且COM端口再次出现;该板可以毫无问题地使用。

我有两个Leonardos,我已经通过复制两个步骤来确认是我的特定代码导致Windows错误出现,而不是硬件问题。

任何人都可以提供有关以下代码中导致此问题的原因的指示吗?(代码有完整的注释来描述使用的目的/方法(

//Code including basic setup/loop and a function I created, asking for readings to be taken from 3 sensors 
//when called, and to then assign the results to global variables
//The loop function should then print the global variables in question and wait for a while before repeating 
//the process
#include <Wire.h> //using an I2C breakout (accelerometer)
#include "SparkFun_MMA8452Q.h" //accelerometer breakout's library
MMA8452Q accel; //create an instance of this accelerometer
int FSR_pin = A1; //force resistor pin
const int PHOTO_pin = A0; //phototransistor pin
//declare variables to use to take a base reading, to later measure against for changes
int base_PHOTO = 0;
int base_FSR = 0;
byte base_ORIEN = 0; //using the method recommended in the accelerometer's startup page to get orientation 
//readings, which they say is passed back as a byte; section 'Reading Portrait/Landscape' 
//on this page https://learn.sparkfun.com/tutorials/mma8452q-accelerometer-breakout-hookup-guide
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
}
void baseReading() {
base_FSR = analogRead(FSR_pin);
base_PHOTO = analogRead(PHOTO_pin);
base_ORIEN = accel.readPL();
}
void loop() {
// put your main code here, to run repeatedly:
baseReading(); //call my own function to get base readings
Serial.println(base_FSR);
Serial.println(base_PHOTO);
Serial.println(base_ORIEN);
delay(5000);
}

int takeReading() {
}

我使用组件制造商的测试草图分别从每个传感器获取读数;只有当我尝试将它们组合成一点代码时,问题才出现。下面是指向上述代码中引用的加速计分线指南的超链接。

已解决:代码缺少设置函数中的行accel.init();

我首先排除了我正在使用的FSR和光电晶体管,因为只有这些组件的运行代码才能按预期执行。这留下了MMA8452Q的代码来查看。

我一直在使用上面链接的加速度计制造商指南,以及其库中的示例 #3(方向读数(来写出我的代码;我设法放弃了init,并认为问题出在我输入的新.readPL方法上。

示例代码使用.begin而不是.init,并且还将其用作 print 语句的一部分,所以我没有立即意识到它包含的目的是相同的。

固定代码如下:

//Code including basic setup/loop and a function I created, asking for readings to be taken from 3 sensors when called and assigned to global variables
//The loop function should then print the global variables in question and wait for a while before repeating the process
#include <Wire.h>
#include "SparkFun_MMA8452Q.h"
MMA8452Q accel;
int FSR_pin = A1; 
const int PHOTO_pin = A0; 
//variables to use to take a base reading, to later measure against for changes
int base_PHOTO = 0; 
int base_FSR = 0;
byte base_ORIEN = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
accel.init(); //The new line, which allows this to run as intended
}
void baseReading() {
base_FSR = analogRead(FSR_pin);
base_PHOTO = analogRead(PHOTO_pin);
base_ORIEN = accel.readPL();
}
void loop() {
baseReading();
Serial.println(base_FSR);
Serial.println(base_PHOTO);
Serial.println(base_ORIEN);
delay(5000);
}

相关内容

最新更新