Python Animation Program:
#-----------------------------
# This is to find all the red locations in all the different red #
# pictures. I needed them all in one list, rather than a tuple, thus #
# the for loops. Then I found out I could do the same thing by drawing #
# a red line and using all the red pixels in that line rather than a #
# bunch of different pictures
#-----------------------------
##AllRedPositions1 = []
##for var in redloc1:
## AllRedPositions1.append(var)
##for var in redloc2:
## AllRedPositions1.append(var)
##for var in redloc3:
## AllRedPositions1.append(var)
##for var in redloc4:
## AllRedPositions1.append(var)
##
##AllRedPositions2 = []
##for var in redloc5:
## AllRedPositions2.append(var)
##for var in redloc6:
## AllRedPositions2.append(var)
##for var in redloc7:
## AllRedPositions2.append(var)
##
##AllRedPositions = (redloc1[0], redloc2[0], redloc3[0], redloc4[0], redloc5[0], redloc6[0], redloc7[0])
#-----------------------------
# greenColor(picture) finds the (x,y) of the green pixels in picture #
# I made this before figuring out that I could just add this to the #
# "redColor" function, which is now called "colorPositions" #
#-----------------------------
##def greenColor(picture):
## W = getWidth(picture)
## H = getHeight(picture)
## green_positions = []
## for x in range(W):
## for y in range(H):
## r,g,b = picture[x,y]
## color = Color(r,g,b)
## green = Color(0,255,0)
## if color.distance(green) < 100:
## green_positions.append((x,y))
## return green_positions
##
##
##greencurves = greenColor(redgreen)
##
##AllGreenPositions1 = greencurves[0:25]
##AllGreenPositions2 = greencurves[25:62]
###-------------------------
# I made this in the process of trying to figure out how to animate #
# the fish without making it animate for each "position in positions" #
# for the drawSun function. But then I got that. #
#-----------------------------
##def fishAnimation():
## frames = []
## for position in greenloc1:
## frame = duplicatePicture(ocean)
## drawFish(frame, position[0], position[1])
## frames.append(frame)
## return frames
#---------------------Real Code--------------------------
import math
import os
import glob
import time
#-------------------------
yellow = makeColor(255,255,0)
red = makeColor(255,0,0)
green = makeColor(0,255,0)
blue = makeColor(0,0,255)
skin = makeColor(239, 202, 146)
darkyellow = makeColor(225, 179, 17)
watercolor = makeColor(0, 128, 131)
gray = makeColor(83,84,71)
fish = makeColor(168, 18, 175)
black = makeColor(0,0,0)
#custom_Color = pickAColor()
#color_distance = distance(color1, color2)
##
###--------------------------
sunWidth = 150
sunHeight = 130
sunXOffset = sunWidth/2
#--------------------------
water = makePicture("water.jpg")
shark = makePicture('shark2.jpg')
happyshark = makePicture('sharkshut.jpg')
ocean = makePicture('oceanafter.jpg')
field = makePicture('newfield.bmp')
newfield = makePicture('newfield2.bmp')
#----------------------------
# These dots1-dots7 became useless after I figured out drawing the #
# red and green lines
#-----------------------------
dots1 = makePicture("redgreen1.bmp")
dots2 = makePicture("redgreen2.bmp")
dots3 = makePicture("redgreen3.bmp")
dots4 = makePicture("redgreen4.bmp")
dots5 = makePicture("redgreen5.bmp")
dots6 = makePicture("redgreen6.bmp")
dots7 = makePicture("redgreen7.bmp")
redgreen = makePicture("redgreen8.bmp")
#-----------------------------
# getDotPictures() retrieves all the files containing #
# "redgreen*.bmp"
#-----------------------------
def getDotPictures():
mpath = getMediaPath()
dotFiles = glob.glob(os.path.join(mpath,'
dotFileNumbers = []
for i,fname in enumerate(dotFiles):
num = int(fname[len(mpath)+8:-4])
dotFileNumbers.append(num)
dotFileNumbers.sort()
dotPictures = []
for dfn in dotFileNumbers:
dotPicture = makePicture('redgreen%s.bmp'%(
dotPictures.append(dotPicture)
return dotPictures
allRedDotPictures = getDotPictures()
#-----------------------------
# colorPositions(picture) finds the (x,y) of the red and green #
# pixels in picture
#-----------------------------
red_positions = []
green_positions = []
blue_positions = []
black_positions = []
def colorPositions(picture):
W = getWidth(picture)
H = getHeight(picture)
for x in range(W):
for y in range(H):
r,g,b = picture[x,y]
color = Color(r,g,b)
red = Color(255,0,0)
if color.distance(red) < 100:
red_positions.append((x-
if color.distance(green) < 100:
green_positions.append((x,y))
if color.distance(blue) < 100:
blue_positions.append((x,y))
if color.distance(black) < 100:
black_positions.append((x,y))
#-----------------------------
# The variables for the separated color positions #
#-----------------------------
allpositions = colorPositions(redgreen)
redloc1 = red_positions[0:22]
redloc2 = red_positions[22:45]
greenloc1 = green_positions[0:22]
greenloc2 = green_positions[22:45]
blueloc = blue_positions[:]
blackloc = black_positions[:]
#----------------------------
def drawSun(picture, x, y):
#x,y should change, but for now:
Width = sunWidth
Height = sunHeight
addOvalFilled(picture, x, y, Width, Height, darkyellow)
addLine(picture, x-10, y+80, x-110, y+80, darkyellow)
addLine(picture, x, y+20, x-100, y-25, darkyellow)
addLine(picture, x+15, y+133, x-60, y+208, darkyellow)
addLine(picture, x+90, y+145, x+90, y+235, darkyellow)
addLine(picture, x+140, y+115, x+210, y+185, darkyellow)
addLine(picture, x+161, y+74, x+250, y+74, darkyellow)
addLine(picture, x+157, y+22, x+212, y-21, darkyellow)
addLine(picture, x+100, y-4, x+118, y-25, darkyellow)
addLine(picture, x+45, y-4, x+25, y-25, darkyellow)
#-----------------------------
#----------------------------
def drawFish(picture, x, y):
W = 43
H = 45
addOvalFilled(picture, x, y, W, H, fish)
addArc(picture, x+10, y+10, 30, 30, 40, 115, color=(0,0,0))
addOvalFilled(picture, x+35, y+18, 4, 4, color=(0,0,0))
addOvalFilled(picture, x+28, y+18, 4, 4, color=(0,0,0))
addLine(picture, x+5, y+5, x-20, y-20, fish)
addLine(picture, x-20, y-20, x-20, y+40, fish)
addLine(picture, x-20, y+40, x+5, y+28, fish)
addLine(picture, x-15, y-15, x-15, y+37, fish)
addLine(picture, x-11, y-11, x-10, y+35, fish)
addLine(picture, x-5, y-5, x-5, y+33, fish)
addLine(picture, x-3, y-2, x-3, y+30, fish)
#-----------------------Ball--
def drawBall(picture, x, y):
W = 20
H = 20
addOvalFilled(picture, x, y, W, H, color=(0, 6, 116))
#-----------------------Ghost Dog/Bird----------------------
def drawGhost(picture, x, y):
W = 40
H = 53
addOvalFilled(picture, x, y, W, H, color=(255,255,255))
addOvalFilled(picture, x+25, y, 25, 21, color=(255,255,255))
addOvalFilled(picture, x+40, y+5, 3, 3, color=(0,0,0))
addLine(picture, x+12, y+53, x-7, y+60, color=(0,0,0))
addLine(picture, x+12, y+53, x+20, y+60, color=(0,0,0))
addLine(picture, x+25, y+53, x+25, y+65, color=(0,0,0))
addLine(picture, x+25, y+53, x+13, y+65, color=(0,0,0))
#-----------------------------
def makeEmptyCopy(picture):
W = getWidth(picture)
H = getHeight(picture)
empty = makeEmptyPicture(W,H)
return empty
def duplicatePicture(picture):
W = getWidth(picture)
H = getHeight(picture)
empty = makeEmptyPicture(W,H)
target = empty
for x in range(W):
for y in range(H):
r,g,b = picture[x,y]
target[x,y] = r,g,b
return target
def makeFrame(W,H):
frame = makeEmptyPicture(W,H)
return frame
#---------------------Animates the Sun---------------------------
def erinsAnimation():
frames = []
for position in range(22):
frame = duplicatePicture(ocean)
drawSun(frame, redloc1[position][0]-60, redloc1[position][1]-128)
if greenloc1[position][1] < 147:
drawFish(frame, greenloc1[position][0], greenloc1[position][1])
frames.append(frame)
for position in range(19):
frame = duplicatePicture(shark)
drawSun(frame, redloc2[position][0]-60, redloc2[position][1]-128)
if greenloc2[position][1] < 147:
drawFish(frame, greenloc2[position][0], greenloc2[position][1])
frames.append(frame)
frames.append(happyshark)
frames.append(happyshark)
frames.append(ocean)
return frames
def secondAnimation():
frames = []
for position in range(65):
frame = duplicatePicture(field)
drawBall(frame, blueloc[position][0], blueloc[position][1])
drawGhost(frame, blackloc[position][0]-50, blackloc[position][1]+95)
frames.append(frame)
frames.append(newfield)
return frames
#-----------------------------
# Above:
# imports colors Sun parameters #
# Images drawSun(pic,x,y) makeEmptyCopy(pic)#
# duplicatePicture(pic) makeFrame(W,H) getDotPictures() #
# allreds redCOlor() AllRedPositions #
# draw_line(pic, x, color) SunAnimation(pic, positions) #
# drawFish(pic,x,y)
#
#-----------------------------
Arduino Code for an E.T. Sculpture I made
/*
When the piezo senses a knock, it will play the ET theme song and light his finger.
Ingredients:
2 piezo sensors
220 ohm resistor
1 megaohm resistor
LEDs
....possible
Connect ET's finger (LEDs) to input 10 and ground with 220 ohm resistor
Can't use the same piezo for both because the sound it produces also creates a
vibration that it detects! As far as I could tell.
1 piezo for analog input - vibration
1 piezo for digital output - sound
*/
//======================== MUSIC DATA ==================================
#include "pitches.h"
// notes in the first melody:
int melody1[] = {
NOTE_C4, NOTE_G4,NOTE_F4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_C4, NOTE_G3, 0, NOTE_A3, NOTE_A4, NOTE_G4, NOTE_FS4, NOTE_E4, NOTE_FS4, NOTE_D4, NOTE_B4};
/*
Went through a lot of trouble trying to figure out how to play song AND turn
on LEDs simultaneously. Broke up the song to two melodies over and over, trying to figure
it out
*/
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
1, 1, 5, 5, 5, 5, 1, 1, 3, 1, 1, 5, 5, 5, 5, 1, 1};
//======================SENSOR DATA===================================
const int sensorPin = A0; //the sensor attaches to the analog pin 0
const int ledPin = 10; // pin (10) that the the LED is connected to
const int ledPin1 = 7; //green LED on pin 7 for testing if sound will work, until I get another piezo
const int THRESHOLD = 10; //how sensitive the piezo is to vibration
//=====================SPEAKER DATA=========================
byte speakerPin = 9; //Speaker attaches to pin 9
//byte ledPin = 10; // which pin the LED attaches to. don't know the difference b/w this and
// "const int ledPin = 10"
void setup()
{
pinMode(ledPin, OUTPUT); //declaring LED pin as an output
pinMode(sensorPin, INPUT); //declaring piezo as input
pinMode(ledPin1, OUTPUT); //declaring green LED as output
}
void music1()
{
for (int thisNote = 0; thisNote < 17; thisNote++) //starting at 0, iterate through the 17 notes
{
int noteDuration = 1000/noteDurations[thisNote]; //integer of each note's length
tone(9, melody1[thisNote], 1500); // play notes from pin 9 for length of 1500
int pauseBetweenNotes = noteDuration * 1.010; //time between notes
delay(pauseBetweenNotes); // delay that amount of time
noTone(9);} //stop the tone playing, but I don't think I could tell a difference
}
//=====================================Fade LED 1 & 2===============
//broken up so it will seem the light and music both start and end together
void fadeLED1(){
for (int brightness = 0; brightness < 255; brightness++)
{
analogWrite(ledPin, brightness);
delay(6); //doesn't take long to fade in so the music will start quickly
}
}
//LED stays lit while the music plays
//Then it starts to fade out
void fadeLED2(){
for (int brightness = 255; brightness >= 0; brightness--) //if LED is >= 0, decrement it
{
analogWrite(ledPin, brightness); //change the LEDs' brightness
delay(15); //how long it takes to decrement
}
}
//=================================================
//=================== IF STATEMENT ==========================
// When ET is slapped,
//fade the LED's on, play the theme song, then fade the LED's off
void loop() { //always the last line of code in Arduino
int val = analogRead(sensorPin); //finds integer value from piezo sensor
if (val >= THRESHOLD) { //if the value is >= THRESHOLD, then
fadeLED1(); // turn on LEDs
music1(); // play song
fadeLED2(); // turn off LEDs
}
else //don't think this is necessary, but it doesn't hurt
{
digitalWrite(ledPin, LOW);
}
}
No comments:
Post a Comment