Self-contained project code for the "Godot Isn't Just for Games" series: Arduino sketches, the Python serial<->UDP bridge, and the Godot 4 projects, one folder per article.
45 lines
1.3 KiB
Arduino
45 lines
1.3 KiB
Arduino
/*
|
|
* LedBasics.ino — Part 1: the minimal round trip.
|
|
*
|
|
* Godot sends "L1" / "L0" over USB serial (via the Python bridge) and this
|
|
* sketch toggles an LED. No sensors yet — Part 2 (JoystickBridge.ino) adds the
|
|
* joystick telemetry going the other way.
|
|
*
|
|
* PROTOCOL
|
|
* IN (host -> Uno): "L1" = LED on, "L0" = LED off (newline-terminated)
|
|
*
|
|
* WIRING
|
|
* D13 -> 220 ohm resistor -> LED anode (+, long leg)
|
|
* LED cathode (-, short leg) -> GND
|
|
* (D13 is also the Uno's onboard LED, so it flashes during every upload —
|
|
* a free "is this pin alive" check before you've wired anything.)
|
|
*/
|
|
|
|
const uint8_t PIN_LED = 13; // LED via 220 ohm resistor (also the onboard LED)
|
|
|
|
char cmdBuf[8];
|
|
uint8_t cmdLen = 0;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
pinMode(PIN_LED, OUTPUT);
|
|
digitalWrite(PIN_LED, LOW);
|
|
}
|
|
|
|
void loop() {
|
|
// Commands in from Godot: "L1" / "L0", one per line.
|
|
while (Serial.available() > 0) {
|
|
char c = Serial.read();
|
|
if (c == '\n' || c == '\r') {
|
|
if (cmdLen >= 2 && cmdBuf[0] == 'L') {
|
|
digitalWrite(PIN_LED, cmdBuf[1] == '1' ? HIGH : LOW);
|
|
}
|
|
cmdLen = 0;
|
|
} else if (cmdLen < sizeof(cmdBuf) - 1) {
|
|
cmdBuf[cmdLen++] = c;
|
|
} else {
|
|
cmdLen = 0; // overflow guard: drop the runt line
|
|
}
|
|
}
|
|
}
|