Files
blog-code/godot-arduino-1-led-first-light/godot/ArduinoGodot/LedBasics.gd
T
tyler parker bb1d70c620 Godot + Arduino series: companion project code
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.
2026-08-09 21:14:55 +00:00

25 lines
843 B
GDScript

extends Control
# Part 1 — the minimal round trip: a Godot button that toggles an LED on the
# Arduino. Only the OUTBOUND (command) half of the link is used here; Part 2
# adds inbound telemetry from the joystick.
const COMMAND_ADDR := "127.0.0.1"
const COMMAND_PORT := 4243
@onready var led_button: Button = $Panel/LedButton
var _udp_out := PacketPeerUDP.new()
var _led_on := false
func _ready() -> void:
# To SEND with PacketPeerUDP in Godot 4, use set_dest_address — NOT
# connect_to_host, which is for receiving and silently drops outbound packets.
_udp_out.set_dest_address(COMMAND_ADDR, COMMAND_PORT)
led_button.pressed.connect(_on_led_pressed)
func _on_led_pressed() -> void:
_led_on = not _led_on
_udp_out.put_packet(("L1" if _led_on else "L0").to_utf8_buffer())
led_button.text = "LED: ON" if _led_on else "LED: OFF"