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

Hi all!

Please help me with a code issue I'm having :')

I was having the same problem as described in this post, and Louis Davis' solution worked for me also - which is a relief as I thought I had managed to actually break not one but two boards out of seemingly nowhere.

I know how to fix the problem now, and this means I've confirmed beyond doubt the problem is with my code.

Does anyone have any suggestions for why the below is making my boards go on the fritz?

//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> //using a 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

//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() {
  
}

Sorry - should have stated in the original post that I'm using the Leonardo; I was able to get readings out of each sensor independently before trying to combine the code together, so I know my hookup etc should be sound.

(deleted)

I mean: I expect my code to upload to the board, but instead I see a 'The USB device has malfunctioned, Windows does not recognise it' error when I try to do that.

When I roll back to known good code ('Blink' example sketch, in this case) while pressing the reset button, as suggested in the post I've linked, the board reconnects to my PC without issue/error messaging.

I've therefore ruled out a hardware/cabling issue, and I've managed to replicate it across two Leonardos, so I'm absolutely certain there is something in the code I've posted which is causing the device to 'malfunction' as Windows puts it, and be unable to recognise my board.

Solved.

The code was missing the line 'accel.begin();' from the setup function.