2008 304: Difference between revisions
No edit summary |
No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 21: | Line 21: | ||
void setup() // run once, when the sketch starts | void setup() // run once, when the sketch starts | ||
{ | { | ||
Serial.begin(9600); | |||
pinMode(resetButton, INPUT); // sets the digital pin as output | |||
pinMode(serveButton, INPUT); // sets the digital pin as output | |||
} | } | ||
void loop() // run over and over again | void loop() // run over and over again | ||
{ | { | ||
if (Serial.available() > 0) { | |||
int inByte = Serial.read(); | |||
a1_value = analogRead(a1_port); | |||
reset = digitalRead(resetButton); | |||
serve = digitalRead(serveButton); | |||
Serial.print(a1_value); | |||
Serial.print(","); | |||
Serial.print(reset); | |||
Serial.print(","); | |||
Serial.println(serve); | |||
} | |||
} | } | ||
</source> | </source> | ||
Line 46: | Line 46: | ||
Python / [[PyGame]] | Python / [[PyGame]] | ||
A simple Python script to test a "call & respond" session with the arduino: | |||
<source lang="python"> | |||
import serial | |||
ser = serial.Serial('/dev/ttyUSB0', 9600) # your serial port may be different | |||
while 1: | |||
ser.write('x') # call | |||
line = ser.readline() # and read the response | |||
print line | |||
</source> | |||
And now combined with a simple PyGame program to visualize the sensor data received from Arduino: | |||
<source lang="python"> | <source lang="python"> | ||
Line 53: | Line 67: | ||
import serial | import serial | ||
ser = serial.Serial('/dev/ | ser = serial.Serial('/dev/ttyUSB0', 9600) | ||
def main(): | def main(): | ||
Line 66: | Line 79: | ||
background = pygame.Surface(SCREENRECT.size).convert() | background = pygame.Surface(SCREENRECT.size).convert() | ||
background.fill((0, 255, 0)) | background.fill((0, 255, 0)) | ||
# keep track of time | # keep track of time | ||
clock = pygame.time.Clock() | clock = pygame.time.Clock() | ||
Line 94: | Line 101: | ||
s1 = int(s1) | s1 = int(s1) | ||
s2 = int(s2) | s2 = int(s2) | ||
except | except ValueError: | ||
pass | pass | ||
# print analog | # print analog | ||
if min_analog == None or analog | if min_analog == None or analog < min_analog: | ||
min_analog = analog | min_analog = analog | ||
if max_analog == None or analog | if max_analog == None or analog > max_analog: | ||
max_analog = analog | max_analog = analog | ||
Line 119: | Line 126: | ||
pygame.draw.circle(screen, (255, 255, 0), pos, r) | pygame.draw.circle(screen, (255, 255, 0), pos, r) | ||
# draw to screen! | |||
pygame.display.update() | pygame.display.update() | ||
# maintain frame rate | # maintain frame rate | ||
clock.tick(30) | clock.tick(30) | ||
if __name__ == "__main__": main() | if __name__ == "__main__": main() | ||
</source> | </source> |
Latest revision as of 11:36, 22 May 2008
Some websites:
Hardware hacking with arduino
Example Programs
Arduino:
int a1_port = 0; // LED connected to digital pin 13
int a1_value = 0;
int resetButton = 2;
int serveButton = 3;
int reset = 0;
int serve = 0;
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
pinMode(resetButton, INPUT); // sets the digital pin as output
pinMode(serveButton, INPUT); // sets the digital pin as output
}
void loop() // run over and over again
{
if (Serial.available() > 0) {
int inByte = Serial.read();
a1_value = analogRead(a1_port);
reset = digitalRead(resetButton);
serve = digitalRead(serveButton);
Serial.print(a1_value);
Serial.print(",");
Serial.print(reset);
Serial.print(",");
Serial.println(serve);
}
}
Python / PyGame
A simple Python script to test a "call & respond" session with the arduino:
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600) # your serial port may be different
while 1:
ser.write('x') # call
line = ser.readline() # and read the response
print line
And now combined with a simple PyGame program to visualize the sensor data received from Arduino:
import pygame
from pygame.locals import *
SCREENRECT = Rect(0, 0, 640, 480)
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
def main():
(min_analog, max_analog) = (None, None)
avalue = 0
pygame.init()
screen = pygame.display.set_mode(SCREENRECT.size, 0)
# make background
background = pygame.Surface(SCREENRECT.size).convert()
background.fill((0, 255, 0))
# keep track of time
clock = pygame.time.Clock()
# game loop
while 1:
# get input
for event in pygame.event.get():
if event.type == QUIT or \
(event.type == KEYDOWN and \
event.key == K_ESCAPE):
return
ser.write("x")
arduino_data = ser.readline()
# print arduino_data
try:
(analog, s1, s2) = arduino_data.split(",")
analog = int(analog)
s1 = int(s1)
s2 = int(s2)
except ValueError:
pass
# print analog
if min_analog == None or analog < min_analog:
min_analog = analog
if max_analog == None or analog > max_analog:
max_analog = analog
if (max_analog != min_analog):
avalue = float(analog - min_analog) / (max_analog - min_analog)
bgcolor = (0, 0, 0)
if s1: bgcolor=(255, 255, 0)
background.fill(bgcolor)
screen.blit(background, (0,0))
r = avalue * SCREENRECT[3] / 2
pos = (SCREENRECT[2] / 2, SCREENRECT[3]/2)
# print r, pos
pygame.draw.circle(screen, (255, 255, 0), pos, r)
# draw to screen!
pygame.display.update()
# maintain frame rate
clock.tick(30)
if __name__ == "__main__": main()