Assignment: DC Motor: Actuation Assignment 1
Collaborators:
I took some inspiration from our recent weather and made a fan that spins/dances around on the table for one second every time someone tweets the word "wind."
Materials:
Arduino
Plastic fan blade cut out of an old DVD case
Fan stand made out of an old cereal box
Battery case/batteries
DC Motor
Transistor
Diode
Wires
Arduino Code:
/*
Spin the motor for one second every time the board gets a '1' over serial
Created 14 October, 2009
By Ian McDowell
*/
int motorPin = 9; // select the pin for the Motor
// The setup() method runs once, when the sketch starts
void setup() {
Serial.begin(9600); //initialize serial comm
pinMode(motorPin, OUTPUT); // initialize the digital pin as an output:
}
void loop() {
if (Serial.available() > 0) { // if we have input
int inByte = Serial.read();
if (inByte == '1'){
analogWrite(motorPin, 250); // analogWrite can be between 0-255
delay(1000);
analogWrite(motorPin, 0);
}
}
}
Python Code
from twython import twython
import time
import serial
import termios, fcntl, sys, os
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
MOST_RECENT = 0
class tweet():
def __init__(self, result_dict):
self.text = result_dict['text']
self.create_date = result_dict['created_at']
self.poster = result_dict['from_user']
self.id = result_dict['id']
def make_api():
api = twython.setup()
return api
def search(api, search):
global MOST_RECENT
everything = api.searchTwitter(search, rpp=10, since_id=MOST_RECENT) #rpp sets the results per page
results = everything['results']
tweets = []
if len(results):
MOST_RECENT = results[0]['id']
for result in results:
t = tweet(result)
tweets.append(t)
return tweets
if __name__ == '__main__':
ser = serial.Serial('/dev/cu.usbserial-A9007LR8', 9600) # initialize the serial connection to arduino
ser.write('1') # write a '1', which the current arduino prog interprets as turning on the fan.
api = make_api() # Make a twitter API to run searches on.
while 1:
tweets = search(api, 'wind') # search for 'wind'
if len(tweets) > 0: # If we turn up any new tweets, tell arduino via serial
ser.write('1')
t= tweets[0]
print "user %s says: %s" % (t.poster, t.text)
else:
time.sleep(.2)
time.sleep(.8) #Check every second (.8 + .2)
try:
c = sys.stdin.read(1)
except:
c = 0
if c:
break
ser.close()