loading

Step 2: Arduino

// twitterMentionMoodLight_arduino
// for use as is with either:
// twitterMentionMoodLight_processing
// twitterMentionMoodLight_python
//
// Generate a peaceful glow until someone on twitter mentions you.
//
// Requires a circuit with: two buttons and a pwm rgb led light, and
// 3 resistors at 220 ohm; 2 resistors at 100 ohm; 2 resistors at 10k ohm.
//
// Shout out to Tom Igoe, Adafruit, lurkers and msg boards everywhere.
// learn more at: http://www.instructables.com/member/pdxnat/
/*
#####################################################################
--------------- TWO BUTTONS AND A LIGHT ----------------------------
Button One: Hold until WHITE LIGHT; Sends a message to Twitter.
The Light: Glow peacefully until commanded by Twitter to change.
Button Two: Resets LED to peacefulGlow.

peacefulGlow() - the default state of the mood light
mention() - someone has mentioned @yourUsername
buttonSend() - update Twitter status
buttonReset() - return to peacefulGlow()
--------------------------------------------------------------------
#####################################################################
*/

const int rButton = 10; // reset button
int reset_btn_val = 0;
const int sButton = 11; // send button
int send_btn_val = 0;
String state = "peacefulGlow";

int ledAnalogOne[] = {3, 5, 6}; // PWM RGB LED pins
// Analog LED 3 = redPin, 5 = greenPin, 6 = bluePin

// Defined Colors
const byte BLACK[] = {0, 0, 0};
const byte WHITE[] = {255, 255, 255};
const byte RED[] = {255, 0, 0};
const byte GREEN[] = {0, 255, 0};
const byte BLUE[] = {0, 0, 255};
const byte ORANGE[] = {83, 4, 0};
const byte YELLOW[] = {255, 255, 0};
const byte MAGENTA[] = {255, 0, 255};

void setup(){ // begin
Serial.begin(9600);
pinMode(rButton, INPUT);
pinMode(sButton, INPUT);
for(int i = 0; i < 3; i++){ // set the 3 LED pins as outputs
pinMode(ledAnalogOne[i], OUTPUT);
}
}

void loop(){
listenToSerial();
buttonSend();
buttonReset();
setState(state);
}
void setState(String s){
if (s == "peacefulGlow") peacefulGlow();
if (s == "mention") mention();
}
void listenToSerial(){ // Twitter commands enter here
int serialMsg = 0;
if (Serial.available()){
serialMsg = Serial.read();
if (serialMsg == 1) state = "mention"; // processing
if (serialMsg == 49) state = "mention"; // python
}
}
void buttonSend(){ // Twitter posts sent here
send_btn_val = digitalRead(sButton);
if (send_btn_val == HIGH){
Serial.print("#peacefulGlow");
delay(200);
sent();
}
}
void buttonReset(){
reset_btn_val = digitalRead(rButton);
if (reset_btn_val == HIGH){
state = "peacefulGlow";
}
}
void peacefulGlow(){
state = "peacefulGlow";
if (state == "peacefulGlow") {
fadeToColor(ledAnalogOne, RED, BLUE, 6);
} else {
setState(state);
}
listenToSerial();
buttonSend();
if (state == "peacefulGlow") {
fadeToColor(ledAnalogOne, BLUE, GREEN, 6);
} else {
setState(state);
}
listenToSerial();
buttonSend();
if (state == "peacefulGlow") {
fadeToColor(ledAnalogOne, GREEN, YELLOW, 6);
} else {
setState(state);
}
listenToSerial();
buttonSend();
if (state == "peacefulGlow") {
fadeToColor(ledAnalogOne, YELLOW, ORANGE, 6);
} else {
setState(state);
}
listenToSerial();
buttonSend();
if (state == "peacefulGlow") {
fadeToColor(ledAnalogOne, ORANGE, RED, 6);
} else {
setState(state);
}
listenToSerial();
buttonSend();
}
void mention(){
state = "mention";
if (state == "mention"){
fadeToColor(ledAnalogOne, RED, BLACK, 1);
} else {
setState(state);
}
listenToSerial();
buttonSend();
buttonReset();
if (state == "mention"){
fadeToColor(ledAnalogOne, BLACK, RED, 0);
} else {
setState(state);
}
listenToSerial();
buttonSend();
buttonReset();
}
void sent(){
setColor(ledAnalogOne, WHITE);
delay(500);
}

// *************************************************************
// ***** COLOR FUNCTIONS - DO NOT TOUCH ***********

void setColor(int* led, byte* color){
for(int i = 0; i < 3; i++){
analogWrite(led[i], 255 - color[i]);
}
}

void setColor(int* led, const byte* color){
byte tempByte[] = {
color[0], color[1], color[2]};
setColor(led, tempByte);
}

void fadeToColor(int* led, byte* startColor, byte* endColor, int fadeSpeed){
int changeRed = endColor[0] - startColor[0];
int changeGreen = endColor[1] - startColor[1];
int changeBlue = endColor[2] - startColor[2];
int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue)));

for(int i = 0 ; i < steps; i++){
byte newRed = startColor[0] + (i * changeRed / steps);
byte newGreen = startColor[1] + (i * changeGreen / steps);
byte newBlue = startColor[2] + (i * changeBlue / steps);
byte newColor[] = {newRed, newGreen, newBlue};
setColor(led, newColor);
delay(fadeSpeed);
}
setColor(led, endColor);
}

void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed){
byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
fadeToColor(led, tempByte1, tempByte2, fadeSpeed);
}

<p>thanks for your great work man, but i tried your code and it only prints out the tweets that I make. If a friend mentions me in their tweet I don't see a thing.</p>
<p>Any guidance with the GET request delay?</p><p>Per Twitter's Dev site:</p><p>Rate limits in version 1.1 of the API are divided into 15 minute intervals, which is a change from the 60 minute blocks in version 1.0. Additionally, all 1.1 endpoints require authentication, so no longer will there be a concept of unauthenticated calls and rate limits.</p><p>While in version one of the API, an OAuth-enabled application could initiate 350 GET-based requests per hour per access token, API v1.1's rate limiting model allows for a wider ranger of requests through per-method request limits. There are two initial buckets available for GET requests: 15 calls every 15 minutes, and 180 calls every 15 minutes. Those primary buckets are outlined <a href="https://dev.twitter.com/docs/rate-limiting/1.1/limits" rel="nofollow">in detail here</a>.</p><p>I keep going over the rate limit :(</p>
I would really like to get this project to work, but up until now I haven't had any luck. <br> <br>At first I tried downloading the library and putting it in the Processing sketchbook. After some trial and error I found out I had to put it in My Documents/Processing/Libraries/Twitter4j/Library. <br> <br>When I tried to start the Processing sketch, I got the message: &quot;the package &quot;twitter4j&quot; does not exist. <br>I tried putting the .jar files that were in the &quot;lib&quot; folder in the &quot;Library&quot; folder, but that generated the same message. <br> <br>I then tried to download the &quot;twitter4j.jar&quot; file that can be found in the instructible (downloaded the file and renamed it to twitter4j.jar) <br>After I put that in the &quot;Library&quot; file and tried to run the sketch, a small window opened and I got the message: &quot;NullPointerExeption&quot;. In the code &quot;Status status = (status)mentions.get(0) was highlited. <br> <br>I have some experience programming arduino's, but little to no experience with Processing. Could someone please point me in the right direction?
Download the twitter4j.jar and just drag the file onto the processing sketch window.
Traceback (most recent call last): <br> File &quot;/Users/mcasanas/Desktop/FG1LI7VGOW49P6U.py&quot;, line 51, in <br> getMention() <br> File &quot;/Users/mcasanas/Desktop/FG1LI7VGOW49P6U.py&quot;, line 35, in getMention <br> status = api.GetReplies() <br> File &quot;/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twitter.py&quot;, line 2885, in GetReplies <br> return [Status.NewFromJsonDict(x) for x in data] <br> File &quot;/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twitter.py&quot;, line 620, in NewFromJsonDict <br> return Status(created_at=data.get('created_at', None), <br>AttributeError: 'unicode' object has no attribute 'get' <br> <br>Im getting this comment. please help :(
Wonder if you could help me out. I got your code working nicely. So i thank you for making this tutorial as i didnt know where to start with the twitter api. Unfortunaly the API its still rather confusing for me so i can't really debug this myself. RIght when i start the &quot;server&quot; i guess you might call it. It pulls a tweet and sets off my arduino, i reset my arduino and its all fine. Randomly later it'll go off for a REALLY old mention. Reset again. Then it will pull the first tweet. This will keep happening at random intervals. Have you had this issue? Or know of an easy fix? I think i might attempt to use the ID of the tweet and make sure that the new mention has a &gt; id.
Dear all, <br> <br>When trying out the python script I get the following error: <br> <br>Traceback (most recent call last): <br> File &quot;C:\Users\RMB\Downloads\FG1LI7VGOW49P6U(1).py&quot;, line 51, in <br> getMention() <br> File &quot;C:\Users\RMB\Downloads\FG1LI7VGOW49P6U(1).py&quot;, line 36, in getMention <br> newID = str(status[0].id) <br>IndexError: list index out of range <br> <br>Could use some help. Thanks! <br> <br>Cheers <br> <br>RJ
Hello. <br>Great tutorial, finally got it working for both Arduino+python and arduino+processing :) I moved the rgb LED from 5v to GND and it just started working? <br> <br>Only problems now is that I can't seem to find were to change your tweet from (default for mines is #peaceful glow) <br>And also some of the LED descriptive colors on arduino code aren't actually changing to that color. <br> <br>E.g ; for alert my arduino should flash red, black (off) but insted flashes light blue, white? <br> <br>Can you help me please
Howdy. Good job getting this far! As for the default message, look in the Arduino code (Step 2) at &quot;void buttonSend()&quot;, particularly at the Serial.print() command.... <br> <br>void buttonSend(){ // Twitter posts sent here <br>send_btn_val = digitalRead(sButton); <br>if (send_btn_val == HIGH){ <br>Serial.print(&quot;#peacefulGlow&quot;); <br>delay(200); <br>sent(); <br>} <br>} <br> <br>As for the LED colors, I don't have a good answer but make sure the physical pins are the same as the pins listed in the code.... <br> <br>int ledAnalogOne[] = {3, 5, 6}; // PWM RGB LED pins <br>// Analog LED 3 = redPin, 5 = greenPin, 6 = bluePin <br> <br>If your LED is configured differently, then just keep messing with the connection points on the LED, on the board, and in the code. <br> <br>Good Luck!
This tutorial looks fantastic. Any chance you'd be able to provide a list of the necessary parts? I'm new to this, and I wasn't sure I had all the parts by just looking at the photo. Thanks and great job!
LED -<br> PWM RGB LED. This LED has 4 pins. Pin 1 Green; Pin 2 Power; Pin 3 Blue; Pin 4 Red. There is no pin to ground.<br> <br> Resistors -<br> 3x 220ohm<br> 2x 100ohm<br> 2x 10kohm<br> <br> Buttons -<br> 2x the little guys&nbsp;<a href="https://www.adafruit.com/products/367" rel="nofollow">https://www.adafruit.com/products/367</a><br> <br> You can use an Arduino with a breadboard or you can solder a circuit board from scratch using 16 or 18 gauge wire, I don't remember which. The single LED can be replaced with multiple LEDs but that'd require some redesign on your behalf and modification to the code, which you *should* be able to do after going through it once.
Does Twitter4j work on Mac?
Yes it does.
Thanks a lot man. I'll do research into the analog v digital, is there anything I should know about them. Things I might miss on the internet. <br><br>I also have a RGB Piranha Super Flux Super Flux LED - Common Anode, will this work for the LED. <br><br>Thanks a lot for the help, Appreciate it. <br><br>Thanks
Have a look at the diagram here:<br>http://www.instructables.com/id/Twitter-Mention-Mood-Light/step6/Circuit-Board/<br><br>At the bottom of the diagram you'll see &quot;Digital Pins --&gt;&quot; and the LED plugs in to pins 3, 5, 6, while the buttons plug into pins 10, 11. I think you should try sticking to the digital pins for this project. I don't remember but I think the LED won't work properly on the analog pins, or something, <br><br>I don't know about the Piranha LED you mention, but you should test it out. I had to test every configuration before I understood what to do with the specific LED I used. <br><br>The breadboards are great for testing testing testing, don't be shy, prototype it until you understand it, that's really the best way to learn. You know, besides asking questions. <br><br>Good Luck!!
Thank you. Sorry for all the questions. When I first asked about the breadboard, is there any tips on how I would change the layout of the circuit. I only have the small breadboard that comes with the Arduino uno ?<br><br>Many thanks <br>
Well first off you can totally do this. Small bb will work just as well as anything, you just have less room for clumsy fingers. :-) yes, if you put the pins in different holes than what the code says, then change the code to match reality. In fact I encourage you to mess with it and see what happens. You won't break anything. But there is the whole analog vs digital pin-hole business that will limit where you connect to your arduino.
Thank you for getting back to me. I was also wondering, I have an ethernet board as well. Do I use that with this tutorial ? <br><br>Many Thanks
I did not use an Ethernet board w this tutorial. I used a regular Arduino plugged into laptop w usb cable. Not sure if Ethernet would require extra work, but the fundamental code above shouldn't change.
Can you use a simple Bread Board, or do you need a circuit board?<br><br>Many Thanks
Yes, you can totally do this w/ a breadboard. <br>Good luck!
Love your work man! can you do it with 4 normal leds?
Yes, you can do it w/ 4 normal leds. They have 2 prongs instead of 4. You might need different resistors between the LED &amp; the microprocessor, might not. The wiring would be different, of course, but basically the same. The code to control it would be different, simpler probably. Test your buttons before soldering them in place, they don't always line up the way you think they will. <br><br>Check out the diagram...<br><a href="http://www.instructables.com/id/Twitter-Mention-Mood-Light/step6/Circuit-Board/" rel="nofollow">http://www.instructables.com/id/Twitter-Mention-Mood-Light/step6/Circuit-Board/</a><br>For each Button / LED pair:<br>Microprocessor -&gt; resistor -&gt; buttton -&gt;(line A) resistor -&gt; ground.<br>Microprocessor -&gt; resistor -&gt; buttton -&gt;(line B) LED -&gt; resistor -&gt; microprocessor.<br><br>If you put it together, four buttons &amp; four LEDs, what would you do with it?<br><br>Thanks for the props.
I want to create the mood light like you have but what color you tweet, is what color light, lights up. I am really new to all of this and only got my first Arduino yesterday. And I am a twitter fanatic, so wanted to do this, but do not have RGB LED's.
Congrats on your first Arduino! You're going to have so much fun. The brilliant thing about making stuff from the ground up is that if you can dream it then you can make it. <br><br>FYI - you can get RGB LEDs for $2 ea here: http://bit.ly/upYpiY at Adafruit. That's a competitive price. This is the LED I used but I've got a funky cover on it that I found at a local electronic hobby shop.<br><br>Mood lights are a fun and pretty straight forward concept and a great start to integrating with twitter. <br><br>Do you prefer coding in &quot;Processing&quot; or &quot;Python&quot; or some other language? This mood light project includes the code for each. I was still learning Python so I thought it'd be fun to explore the differences. There are enough tutorials out there and all coding follows the same basic structure. I've also got <a href="http://bit.ly/rPuqHF" rel="nofollow">SimpleTweet_00 (Processing)</a> and <a href="http://bit.ly/ua9ZML" rel="nofollow">SimpleTweet_01 (Python.)</a><br><br>Have fun!
oops, that 2nd line should include power...<br><br>Microprocessor -&gt; resistor -&gt; buttton -&gt;(line B) &gt;power&lt; LED -&gt; resistor -&gt; microprocessor.

About This Instructable

15,679 views

29 favorites

License:

More by pdxnat: Twitter Mention Mood Light simpleTweet_01 python How to install Python packages on Windows 7
Add instructable to: