Serial and pixels for breakfast. Arduino + pygame.

Taking a byte of bits of Serial, along with Green screen with Sam, pygame, and an Arduino hooked up to a light sensor and a motor thing.

Coffee too?  Naturally.

Where do we begin?  At the end of course.  A video of the result...

The arduino code

#include <Servo.h>  
Servo myservo;
int potpin = 0;
int val;

void setup() { 
void loop() { 
  val = analogRead(potpin);
  val = map(val, 62, 540, 0, 179);  

python pygame code

Extended the "green eggs and ham" code I made recently to read serial data from the Arduino, and paint the screen different shades of green, whilst also making some silly sounds depending on the value read from the light sensor.

import os
import pygame
import serial

# guessing serial ports names. Linux or macosx.
ser = serial.Serial('/dev/tty.usbmodem1411',
                    timeout = 0)
serial_buffer = ""

# an event number for the SERIAL

from pygame.locals import *
Event = pygame.Event
split = os.path.split

pygame.mixer.pre_init(44100, 8, 2, 1024)
screen = pygame.display.set_mode((640, 480))
message = "Press q to quit, b for blue, r for red."

import pygame.examples
example_path = split(pygame.examples.__file__)[0]
example_data = os.path.join(example_path, "data")
sounds = [pygame.mixer.Sound("wiff.wav"), 

going = True
while going:
    serial_data =
    #print (repr(serial_data))
    while serial_data:
        serial_buffer += serial_data
        # if there is a new line in it, then 
        if "\r\n" in serial_buffer:
            #print (repr(serial_buffer))

            evt = Event(SERIAL, 
                        {'line': serial_buffer})
            serial_buffer = ""
        serial_data =

    events = pygame.event.get()

    for event in events:
        print (event)
        if event.type == KEYDOWN and event.key == K_q:
            going = False
        if event.type == KEYDOWN and event.key == K_b:
        if event.type == KEYDOWN and event.key == K_r:
        if event.type == SERIAL:
            # read the line from the serial event.
            print (repr(event.line))

            # clean up, and do sanity checking.
            # It could be corrupt or garbage.
            line = event.line.replace("\r\n", "")
            line = line.replace("\n", "")
            line = line.replace("\r", "")
            # it could be empty.
            if line:
                val = int(line)
                if val < 1024 and val > 0:
                    print (repr(val))
                    # map 0 and 1023 of analog read to
                    #  0-255 colour colour range.
                    green = int((val/float(1023))*255 )
                    screen.fill((0, green, 0))
                    if val < 100:
                    if val > 300:



Popular posts from this blog

Is PostgreSQL good enough?

pygame on pypy usable

Experiments with new low latency PyPy garbage collector in a thread.