Code upload results in 'USB device has malfunctioned' Windows error - arduino

I am having the same problem as described in this post on the Arduino forums. I have a slight deviation in that I am using an Arduino Leonardo, but otherwise the core problem is the same.
Trying to upload a sketch to my board results in Windows stating my 'USB device has malfunctioned and Windows does not recognize it'. The COM port used for the board then disappears, as with the post above.
I tried the solution posted by Louis Davis in the linked post, which allowed me to successfully reset the board and upload a known good sketch. When this is completed, the board is able to be recognised by Windows again, and the COM port reappears; the board can be used without issue.
I have two Leonardos and I have confirmed by replicating steps across both that it is my specific code which is causing the Windows error to appear, not down to a hardware issue.
Could anyone offer pointers on what in the below code is causing this? (Code is fully commented to describe purpose/methods used)
//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() {
}
I have taken readings from each sensor individually using test sketches from the component manufacturers; the problem only appeared when I tried to combine them into one bit of code. Here's a hyperlink to the accelerometer breakout guide referenced in the above code.

Solved: The code was missing the line accel.init(); from the setup function.
I first ruled out the FSR & phototransistor I was using, as running code for only these components performed as expected. That left the MMA8452Q's code to look at.
I'd been using the manufacturer's guide as linked above for the accelerometer, and Example #3 (orientation reading) from its library to write my code out; I managed to drop the init and assumed the problem was with the new .readPL method I had put in.
The example code uses .begin instead of .init, and also uses this as part of a print statement, so I didn't immediately catch on that the purpose of its inclusion was the same.
The fixed code is as follows:
//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);
}

Related

Arduino HID keyboard keycodes not working properly

I'm working on a project where I can use an IR remote to hook up to my PC and control it because I have a projector connected to it that I want a remote for. I've figured out all of the IR remote stuff and have also been able to configure my Arduino UNO as an HID keyboard (I know there are better solutions for HID devices than Arduino UNO but the way I want this set up makes it so I have to use an UNO). However when I try to send key inputs to my PC it acts like its pressing the Control button (for example if I press A on my actual keyboard after pressing a button on the remote it selects everything on the page I am currently on). I've tried a bunch of different keycodes and looked at other people's code and couldn't find where I am going wrong here is the code I have for my project so far:
#include <IRremote.hpp>
#include <HID.h>
#include <IRremote.h>
int RECV_PIN = 7;
uint8_t buf[8] = { 0 };
void setup() {
Serial.begin(9600);
IrReceiver.begin(RECV_PIN, ENABLE_LED_FEEDBACK);
delay(200);
}
void loop(){
if (IrReceiver.decode()) {
int i;
int command = IrReceiver.decodedIRData.command;
IrReceiver.stop();
switch (command) {
case 70:
Serial.println("Volume +");
buf[2] = 0x80;
Serial.write(buf, 8);
break;
/*
There are a bunch more case statements with different IR commands that
correlate to functions I want the remote to do
*/
}
releaseKey();
delay(500);
IrReceiver.start(8000);
IrReceiver.resume();
}
}
void releaseKey() {
buf[0] = 0;
buf[2] = 0;
Serial.write(buf, 8);
}
Removing the line that prints "Volume +" was the solution. That was apparently not a valid command for the HID firmware. Remember to always simplify your code to the simplest possible thing that should work but doesn't, so you can solve problems like that yourself.

my serial port did not show my sensor data properly

Hey i got a bit problem with my Arduino and sensor
Here is what i tried ;
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
#include <SoftwareSerial.h>
SoftwareSerial blue(0,1);
const int PulseWire = 0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13; // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;
PulseSensorPlayground pulseSensor;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
blue.begin(9600);
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
pulseSensor.begin();
}
void loop() {
// put your main code here, to run repeatedly:
int myBPM = pulseSensor.getBeatsPerMinute();
if(myBPM>200){
myBPM-100;
}
if (pulseSensor.sawStartOfBeat()) {
Serial.println(myBPM);
blue.println(myBPM);
}
delay(10);
}
this code I got from the example library and modified it.
so i want to send data to my android using Bluetooth but this sensor kinda ticked me off because whenever i use it with my HC-06 Bluetooth module it suddenly got a hearth beat without i even touching it and it just sends so much data ignoring the delay I set.
I just need to slowly sending data just like a second but the data didn't show up
so anyone can help?
I read your code and I noticed this piece of code
if(myBPM > 200){ myBPM - 100; }
that is poorly written if (I understand correctly) you want to check the size of myBPM and if it is larger than 200 then it should be subtracted 100.
it should be:
myBPM = myBPM - 100; not myBPM - 100;
I hope my answer will help you. Have a nice day!

How to use Processing to store Arduinos serial output into a text file?

I have been trying to store data (real time, gas sensor data) into a .txt file so as to make graphs.
This is my arduino code:
const int gasPin = A0; //Gas sensor output pin to Arduino analog A0 pin
void setup()
{
Serial.begin(9600); //Initialize serial port - 9600 bps
}
void loop()
{
Serial.println(analogRead(gasPin));
delay(1000); // Print value every 1 sec.
}
And this is my Processing code:
import processing.serial.*;
Serial mySerial;
PrintWriter output;
void setup() {
mySerial = new Serial(this, "COM3", 9600);
output = createWriter( "data.txt" );
}
void draw() {
if (mySerial.available() > 0 ) {
String value = mySerial.readString();
if ( value != null ) {
output.println( value );
}
}
}
void keyPressed() {
output.flush();
output.close();
exit();
}
This doesn't work. I always get an empty data.txt file.
You should get into the habit of breaking your problem down into smaller steps and then taking those steps on one at a time.
For example, can you create a simple example sketch that stores a value in a file? Don't worry about Arduino yet. Just store a single value in a file.
Then make it so you store a bunch of values in the file. Maybe the value returned from millis(), or the mouse position. Again, don't worry about Arduino yet. Get this working perfectly before moving on.
Separately from that, can you make an Arduino program that sends values to a Processing sketch that simply prints those values to the console?
When you have those working separately, then you can combine them into a single program.
Right now, there's no way to know which part of your code is failing: is it the Arduino code? Is it the file storage? So you need to isolate those pieces so we (you) can test them by themselves.
If you still can't figure it out, then post a MCVE of just a single step in a new question post, and we'll go from there. Good luck.

Saving endless loop EKG data as .txt file

I am using Olimex EKG Shield with Arduino Uno.
void setup() {
// put your setup code here, to run once:
// initialize serial communication at 9600 bits per second:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float value = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(value);
}
With this code provided here, I am getting a voltage value from 0-5V.
Since its a loop, the data keep shows in the serial monitor until it is disconnected.
So, what I am trying to do is that measure ECG for a certain amount of time (let's say 5 min) or data points (let's say a million points), and then save this data into a .txt file.
//From Arduino to Processing to Txt or cvs etc.
//import
import processing.serial.*;
//declare
PrintWriter output;
Serial udSerial;
void setup() {
udSerial = new Serial(this, Serial.list()[0], 115200);
output = createWriter ("data.txt");
}
void draw() {
if (udSerial.available() > 0) {
String SenVal = udSerial.readString();
if (SenVal != null) {
output.println(SenVal);
}
}
}
void keyPressed(){
output.flush();
output.close();
exit();
}
I found this processing code that imports data from Arduino serial monitor and saves as a .txt file, but it doesn's work somehow.
I think I need to make some change to the code on Arduino side and also on Processing side.
If anyone can help with me, I would really appreciate.
Thank you.
You need to be more specific than saying "it doesn't work somehow" - we have no idea what that means. What exactly did you expect this code to do? What exactly does it do instead?
You also need to split this up into smaller problems.
Can you create a simple example program that simply sends the values to Processing? Just print them to the console for now.
Can you create a separate example program that stores values in a text file? Just use hard-coded values or random values for now- don't worry about the arduino yet.
When you have both of those working perfectly, then you can think about combining them into one program that does both: sends values from the arduino and saves those values to a text file.
You can't just "find code" and expect it to work. You have to break your problem down and then approach each individual step by itself. Then if you get stuck on a specific step, you can post a MCVE and we can go from there. Good luck.

GPS module stops working, is unable to setMode to on

I have been using the GPS module on waspmotes, and I have been able to get data off it. However on a fine day, with clear visible skies(which I don't think matters), I cannot be able to set the power mode on.
On calling GPS.setMode(GPS_ON), it fails to set the power mode on, and on freezes on subsequent call to: gpsConnected = GPS.check();
I have tried changing the GPS unit to no avail. I am using the following code. (NB: I have removed the unnecessary bits of code for the sake of brevity)
void setup()
{
ACC.ON();
USB.begin(); // Opening UART to show messages using 'Serial Monitor'
//Initialize the GPS module
GPS.ON(); // Turn GPS on
GPS.setMode(GPS_ON); // set GPS on
if(!GPS.pwrMode) USB.println("Was unable to set on the GPS internal power mode."); //this message gets printed.
if(!GPS.setCommMode(GPS_NMEA_GGA)) USB.println("Was unable to set the GPS communication mode.");
// Power up the Real Time Clock(RTC), init I2C bus and read initial values
RTC.ON();
........
}
void loop(){
//declare the variables
len, i, j = 0;
char degree[4] = "", minutes[8] = "", *latitude, *longitude, *altitude;
uint8_t temperature = 0;
int8_t fileFound = 0;
double latitude_dd, longitude_dd;
byte accOk;
//check if the GPS has connected to the satellite
GPS.begin(); // open the uart
GPS.init(); // Inits the GPS module
gpsConnected = GPS.check();
........
........
}
What might have happened???
Any help will be appreciated.
This was caused by using the RTC.setTime function. In the code, as can be seen here: http://pastebin.com/RmA98MkD, on line 195, RTC.setTimeFromGPS(), I was setting the boards time with the time that I received from GPS. This ended up screwing the GPS connection up and the code only looped once. You can see detailed conversations from here: http://www.libelium.com/forum/viewtopic.php?f=16&t=9861
You Need to simple test. First you ensure that you are giving TTl input to the Tx and rx pin . If you upload this program It will gives GPS output string.If you are using GPS module with Serial output of RS 232 out, As you said it wont work.IT expecting TTl input only
void setup()
{
Serial.begin();
}
void loop()
{
}

Resources