First, I hooked up one LED to control turning it on and off with the button.
Once I got one LED to turn on, I proceeded to create the d-pad with one button each to control forward, backward, left, and right on the motors.
I had one issue when I was running the code, which was that for some reason the wheels would be stuck on going left immediately after uploading the code (without me pushing any buttons). I was confused about what could be wrong or how to debug it. When I commented out the code that made the car turn left, everything else worked fine.
In order to debug the code, I had to print out what was happening in the code. To do this, I had to begin serial monitor 9600 with this code: "Serial.begin(9600);" which would then print to the output the serial monitor "console". To open the serial monitor, I had to go to Tools > Serial Monitor, and select the baud of 9600 from the bottom right dropdown menu.
I then created function to dump the current state of each of the movements. It helped to separate each line using "/t" or else everything would print to console on one line. Additionally, adding a delay would help slow down the outputs.
void dumpState() {
Serial.print("\t");
Serial.print(buttonStateBack);
Serial.print("\t");
Serial.print(buttonStateLeft);
Serial.print("\t");
Serial.print(buttonStateFwd);
Serial.print("\t");
Serial.print(buttonStateRight);
Serial.println();
delay(50);
}
After implementing this debugging code, I was much better able to see that the "left" button (motorPin 3) was always in the "on" state. However, this code still didn't give me a hint as to why.
I decided to double check the hardware, and it turned out that the resistor was connected to power instead of ground. After connecting the resistor to ground, everything worked perfectly, except sometimes the wheels would get stuck -- there weren't enough constraints around the wheels or the motors, so sometimes the wheels would tilt and hit the walls of the chassis, getting it stuck. That would need to be fixed in a later stage.
Here is the final vehicle being controlled with the d-pad push buttons:
Comments
Please log in or sign up to comment.