This sketch bricks Arduino Pro Micro.
#include "HID-Project.h"
#include <Stepper.h>
const int pinSteer = A3;
const int pinAcc = A1;
const int pinBrake = A2;
const int stepsPerRevolution = 96; // 360 / 3.75 = 96
const int maxSpeed = 500;
const int maxSteps = 9;
uint32_t printTimer;
uint32_t loopTimer;
uint32_t timeout = 100;
int steer, prevSteer;
int inc, incDir;
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);
void setup()
{
myStepper.setSpeed(0);
steer = 512;
prevSteer = 512;
inc = 0;
incDir = 1;
loopTimer = millis();
printTimer = millis();
Gamepad.begin();
//Serial.setTimeout(timeout);
Serial.begin(9600);
}
void loop()
{
if (millis() - loopTimer < timeout)
{
return;
}
loopTimer = millis();
readSensors();
motorStep();
}
void motorStep()
{
if (steer > 0x02FF || steer < 0x00FF)
{
return;
}
//int targetSteer = Serial.parseInt();
int targetSteer = 512;
//print1s(steer, 1000);
int dmp = dumper();
int diff = steer - targetSteer;
Serial.print(steer);
Serial.print(" ");
diff += dmp * 0.0;
int steps = constrain(diff * 1, 0, maxSteps);
myStepper.step(steps);
Serial.println(targetSteer);
}
int dumper()
{
int v = steer - prevSteer;
prevSteer = steer;
return v;
}
void readSensors()
{
int acc, brake;
steer = analogRead(pinSteer);
int x = map(steer, 0, 1023, -32767, 32767);
//print1s(steer, 20);
Gamepad.xAxis(x);
Gamepad.write();
}
void print1s(int val1, int delay)
{
if (millis() - printTimer < delay)
{
return;
}
printTimer = millis();
Serial.println(val1);
}
Device Manager reports: A request for the USB device descriptor failed.
I checked it twice. This sketch worked fine until I replaced the DC motor with a stepper.
Example for stepper works fine.
I have one more problem.
The sketch loading takes more than eight seconds. I haven’t been able to upload the sketch yet. The microcontroller revived on its own miraculously. After that, I uploaded this toxic sketch again and the board was bricked.
I noticed that the bootloader stops for 8s even after one click. Perhaps this explains something. I connected a button to the RST pin but the behavior didn’t change.
I also tried this https://www.circuitsgallery.com/arduino … st-failed/
And so, I have two questions.
-
Why is this script bricks the board?
-
How to revive the board?
Thanks in advance!