Skip to content

Instantly share code, notes, and snippets.

@ProfAndreaPollini
Last active January 15, 2025 12:07
Show Gist options
  • Save ProfAndreaPollini/706ee01791e6e0d454d3bdb16687c31a to your computer and use it in GitHub Desktop.
Save ProfAndreaPollini/706ee01791e6e0d454d3bdb16687c31a to your computer and use it in GitHub Desktop.
arduino serial to raylib
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
auto x = random(1,100);
Serial.println(x);
delay(1000);
}
import multiprocessing as mp
import serial as sr
import raylib as rb
import random as rd
import time
data = 0
def serial_task(q: mp.Queue):
while True:
time.sleep(1)
n = rd.randint(1,100)
q.put(n)
if __name__ == "__main__":
mp.freeze_support()
q = mp.Queue() # creo la coda
p = mp.Process(target=serial_task,args=(q,))
p.start()
while True:
if not q.empty():
data = q.get()
print(data,end=" ")
import multiprocessing as mp
import serial as sr
import random as rd
import time
data = 0
def serial_task(q: mp.Queue):
arduino_serial = sr.Serial("COM6",9600)
while True:
from_serial = arduino_serial.readline()
from_serial = from_serial.decode("utf-8")
n = float(from_serial)
q.put(n)
from pyray import *
def app():
global data
init_window(800, 450, "Hello")
while not window_should_close():
if not q.empty():
data = q.get()
begin_drawing()
clear_background(WHITE)
draw_text(f"data = {data}", 190, 200, 20, VIOLET)
end_drawing()
close_window()
if __name__ == "__main__":
mp.freeze_support()
q = mp.Queue() # creo la coda
p = mp.Process(target=serial_task,args=(q,))
p.start()
app()
# while True:
# if not q.empty():
# data = q.get()
# print(data,end=" ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment