Instructables

The Twittering Office Chair

Featured

Step 10: Code

Picture of Code
This project was a great excuse for me to start to learn Python. As a novice, this code is not perfect, but it gets the job done. If you any have suggestions as to how this may be improved, please let me know.

That said, some helpful things I learned about Python include:
One of Python's greatest features (or possibly the worst?) is its lack of syntax. You rarely need to remember a closing or opening tag. You can just keep typing out lines of code and Python will try its best to process it.

Python processes blocks of code based on their indentation. Improper indentation can result in catastrophe. For instance, the only way the code knows that something is part of a for loop is because it is indented the same as everything else that is also part of that loop. This can start to get tricky with nested loops (loops inside of loops) and lead to confusion and bad code.

Python's function system is confusing on a number of levels (for instance some variables are mutable and some not). I don't fully understand the complexities of functions, but if you are interested, you should definitely look it up.

The code we are going to use includes a number of packages, a collection of functions that can be invoked at will (see last step for more information about installing packages).

We are importing the following packages:
- Python Twitter
- PySerial
- Numpy
- Time
- Random

The code is as follows:

#!/usr/bin/python #Filename: TwitterChair.pyimport twitterimport serialimport numpyimport timeimport random## Your username and password here#api = twitter.Api(username='yourusername', password='yourpassword')## Change this to match your serial port#ser = serial.Serial('/dev/tty.usbserial-A70063pF', 19200)serialArray = []global gassygassy = 0def checkokay():	ser.flushInput()	time.sleep(3)	line=ser.readline()	time.sleep(3)	if line == ' ':		line=ser.readline()	print 'here'def startvalues(i):	while i <= 50:		#time.sleep(.25)		line=ser.readline()		serialArray.append(int(line))		ser.flushInput()		i=i+1	print serialArraycheckokay()startvalues(0)def main(i, i1):	ArrayTransfer = numpy.array(i)	ComparatorValue = int(numpy.mean(ArrayTransfer))		print ComparatorValue	lastReading = ComparatorValue	print lastReading		loopy = i1	print loopy	y = 0	x = 0	while y < 10:		while x < 5:			line=ser.readline()			currentReading = int(line)           			if currentReading > lastReading:				lastReading = currentReading				print lastReading			print currentReading			time.sleep(6)			ser.flushInput()			x=x+1		print 'half minute'		x=0		y=y+1	if lastReading > (ComparatorValue + 50):		i1 = 1        	if i1 == 1:		sayings = ["He farted again with success and vigor. Woest me.", "Pfffffffffft", "Ugh. That was a gross one", "Someone please disassemble me", "If I could puke my foam stuffing, I would", "I wish I were some other chair",  "He farted right on me again", "Didn't hear that coming", "allow me to repeat... ppppfffffffffftttttttttttt", "It wasn't always like this."]		pickSaying = random.randint(0,9)		print pickSaying		status = api.PostUpdate(sayings[pickSaying])	loopy = 0	print 'done'	if __name__ == "__main__":		main(serialArray, loopy)main(serialArray, gassy)
 
Remove these adsRemove these ads by Signing Up
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!