Step 10: 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=&apos;yourusername&apos;, password=&apos;yourpassword&apos;)## Change this to match your serial port#ser = serial.Serial(&apos;/dev/tty.usbserial-A70063pF&apos;, 19200)serialArray = []global gassygassy = 0def checkokay():	ser.flushInput()	time.sleep(3)	line=ser.readline()	time.sleep(3)	if line == &apos; &apos;:		line=ser.readline()	print &apos;here&apos;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 &apos;half minute&apos;		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&apos;t hear that coming", "allow me to repeat... ppppfffffffffftttttttttttt", "It wasn&apos;t always like this."]		pickSaying = random.randint(0,9)		print pickSaying		status = api.PostUpdate(sayings[pickSaying])	loopy = 0	print &apos;done&apos;	if __name__ == "__main__":		main(serialArray, loopy)main(serialArray, gassy)
....over 3000 people are following your farts. wow.
Now its 3460 :)
<p>Now 8705...</p>
<p>Awesomely disgusting...</p>
<p>Haha love this!</p>
Thats superior...
hahaha I wonder what takes someone to build this &gt;.&lt; but it's creative :D
silly haha
Man...people around you are gonna get blond! XD
17 farts in one day?!?!?!?! i really hope you don't have co-workers in the same room.
Super silly!!!!!!!
I'm very concerned about this. What if all chairs were equipped wtih such sensors, then once a threshold was reached, a carbon tax would be executed. What if some companies were more taxed than others - would they cancel rice and beans day in the company cafeteria? No more chilli cookoffs as a part of team events? Think of the repercussions man! ;)
I have to agree with mith91 ... absolutely hilarious! <br> <br>sammy jr - never heard of that company but can recommend http://www.margolisfurniture.co.uk/office_storage <br> <br>Of course if I was to sit on that chair you'd never hear a tweet out of me all day <br>;-)
Really nice post. Just gave you 5 stars :)
nice easter egg
my god, this is GREAT&nbsp;:D<br /> <br /> where can we follow that chair??<br />
Omg that is hilarious!....I wonder if it could be expanded to coworkers chairs as well. lol<br />
just found this site. The effort some people make to make an &quot;interesting&quot; tweet is hilarious :) <br /> <br /> actually I&nbsp;am looking for a Chair (that's how I laned on this page), does anyone know this company?<br /> <br /> <a href="http://www.chworkspace.co.uk/" rel="nofollow">www.chworkspace.co.uk/</a>
Congratz, you can now send "twoots".
WIN<br />
&nbsp;cool i'm gonna do one of these.<br /> Can you help me find some kind of way to make it detect when i sit, stand, and how much weight is put upon it?
Sitting and standing is easy! Weight is going to be very difficult. I recommend starting with Plusea's projects.<br /> <br /> https://www.instructables.com/member/plusea<br />
&nbsp;Thanx a Lot, I'm currently following OfficeChair on Twitter, for some further information, my idea of the weight measurement is to save some ranges of weight so that somehow the chair knows who of the people that usually sits on it sits. Also, I recently thought making this in my family's couch, it would be so cool 'cause its a big couch for like 4 or 5 people, it could tweet like, &quot;the one on the further right farted on me&quot;.<br /> Thanks again
LOL<br />
Great idea but you could do this without any&nbsp;dependencies and python pain&nbsp;in php.
Adding dependencies in Python takes only a few minutes. Learning PHP would take a few days. Besides, I found that Python is much friendlier than PHP<br />
My favorite part about this fart related post is the advertisement for &quot;high fiber&quot; soup. Now that's targeted advertising!<br />
Hahaha, Nice one!
&nbsp;Got a kick out of this one. Cool idea! :)<br /> Would be BA if it could tell what you ate..&nbsp;
You haven't farted since May 31st,,, Hmm...
I don't get tweets anymore.
I just had a look on Twitter and you fart a LOT<br />
Could you put sensors on so it tweets when you spin, sit down, slide or any other thing like that?<br />
Yes, the next modification I want to make to it is to sense when someone is sitting in it and for how long and then comment upon that. <br />
It would be so cool!!!<br /> When you spin it could say &quot;I'm getting reeeeaaaally dizzy&quot; and when you sit on it it could say &quot;God he's so fat&quot; (no offense and I'm not saying you are fat)<br />
If you're using a Potentiometer as a variable resistor, you should <em>really</em> connect the wiper to one end of the resistive element. The wiper in pots occationaly looses contact with the resistive element, and in certain situations it can cause bad things to happen.<br/><br/>While it's not so critical here, it <em>is</em> good electronics hygiene.<br/>
Do you mean that a SPARK might occur!? Whooof! BLAM!
Yeah, its not smart to keep electronics near your farting butt.
The chair should be fitted with something that produces a flame instead of a tweet. That would be funny as hell.<br/><br/>Maybe a mesh office chair like one here: <a rel="nofollow" href="http://www.intercounty.com">http://www.intercounty.com</a> could make linking the whole thing up to a human easier?<br/>
Someone should make a version of this that uses a capacitor to cause a spark when it detects gas....
How do you know it would still be around to work the second time?
Nothing quite so spectacular. More likely, just bad data, offset drift or something similar.
You didn't fart since Mar 31.
ok i might of read it wrong but are you realy poasting every time on twitter when some one farts?
Sir, you are a genius, and I salute you!!
This is absolutely BRILLIANT!!!!!
sadly, i am follwing this ;P
That's weird! What kind of strange people go around labeling property with cryptic stickers?

About This Instructable




Bio: My name is Randy and I founded the Instructables Design Studio. I'm also the author of the books 'Simple Bots,' and '62 Projects to ... More »
More by randofo:Spooky Whispering Prank Custom Print Kimono Mad Scientist Extension Cord 
Add instructable to: