Loading...
Logo
Processing Forum
Hi Everybody!

im trying to create a post on twitter when i receive data from my accelerometer.

everything works great;

my arduino code runs and recognizes my accelerometer.
my processing sketch recognizes my accelerometer, and posts to twitter.
 
the problem is that because the accelerometer always has output of around 550 i am constantly posting to twitter. 

how do i set a limit in processing. can you write if statements in processing the same way you do in arduino? (im newish to arduino and this is my first attempt at using processing) in my arduino sketch i have if xvalue is greater than 600 serial print in  "ollie"

this works in arduino but i don't know how to translate it in processing. 

Arduino Code

Copy code
  1. // these constants describe the pins. They won't change:
  2. const int groundpin = 2;             // analog input pin 2 -- ground
  3. const int powerpin = 4;              // analog input pin 4 -- voltage
  4. const int zpin = A2;                  // z-axis of the accelerometer
  5. const int ypin = A1;                  // y-axis
  6. const int xpin = A0;                  // x-axis (only on 3-axis models)
  7. int sensorValue;

  8. void setup() {
  9.   // initialize the serial communication:
  10.   Serial.begin(9600);
  11. }

  12. void loop() {

  13.  
  14.   
  15.   if(analogRead(zpin)>600)
  16.    Serial.println("Ollie");

  17.   
  18.   Serial.println();
  19.   // delay before next reading:
  20.   delay(100);
  21. }

Processing code (Twitter4j + graph)
Copy code


  1. /*
  2. Posts a message to a Twitter account when you press the mouse button.
  3. Uses Twitter4j, http://twitter4j.org.
  4. For more info: http://tinkerlondon.com/now/2010/09/13/oauth-twitter-and-processing/
  5. Daniel Soltis, September 2010
  6. */

  7. import twitter4j.conf.*;
  8. import twitter4j.internal.async.*;
  9. import twitter4j.internal.org.json.*;
  10. import twitter4j.internal.logging.*;
  11. import twitter4j.http.*;
  12. import twitter4j.api.*;
  13. import twitter4j.util.*;
  14. import twitter4j.internal.http.*;
  15. import twitter4j.*;

  16. import processing.serial.*; //new 

  17. String msg = "Ollie"; //my tweet

  18. //copy and paste these from your application in dev.twitter.com
  19. String consumer_key = "cv50u99znWOAOnOEUkhDA";
  20. String consumer_secret = "dzVnD4nLzQd9FAUg47AyEVJKPQgY6ey6dsdOZDsj2FY";
  21. String oauth_token = "276648172-s7m9Z4BPDlREyGJkllFKu84lguDeWkJU4ZKm6Y9E";
  22. String oauth_token_secret = "zKN1i9SGNeh6PDjrEmv8BzeAYrErRac3Mjfs9XjU";

  23. color bgcolor = color(255);
  24. long timer;
  25.  Serial myPort;        // The serial port
  26.  
  27. void setup() {
  28. size(640,480);

  29. //new below
  30.  // List all the available serial ports
  31.  println(Serial.list());
  32.  // I know that the first port in the serial list on my mac
  33.  // is always my  Arduino, so I open Serial.list()[0].
  34.  // Open whatever port is the one you're using.
  35.  myPort = new Serial(this, Serial.list()[0], 9600);
  36.  // don't generate a serialEvent() unless you get a newline character:
  37.  myPort.bufferUntil('\n');
  38.  //new above

  39. }

  40. void draw() {
  41. background(bgcolor);

  42. bgcolor = color(255);
  43. }

  44. void 

  45. serialEvent (Serial myPort) {
  46.  
  47. Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance (
  48. consumer_key, consumer_secret,
  49. new AccessToken( oauth_token, oauth_token_secret) );
  50. try {
  51. Status st = twitter.updateStatus(msg + " "  + second());
  52. println("Successfully updated the status to [" + st.getText() + "].");
  53. bgcolor = color(0,0,255);
  54. timer = millis();
  55. }
  56. catch (TwitterException e) {
  57. println(e.getStatusCode());
  58. }
  59. }
please help, Thanks.

Replies(10)

Hi Again. 


I need to figure out what to replace mousePressed() with. so instead of mousePressed() it would be accelerometer data >550.
Hi Mike!
To do what you want to do, you can write the same statement as you had in your Arduino sketch (if xValue > 600...).

:)
See you tomorrow!


i added if((zpin)>600) and it said Void was unexpected so i changed it to void loop and that seems to be working. also i added  int zpin = 2; at the top-ish right under  Serial myPort; because i didn't know what zpin was. it ran fine but when i tried it nothing happened, no post....i guess its not reading the if statement properly. 
Hi everyone!

Here is the code im trying to put together but it doesn't seem to work... as in nothing happens when i move the acceleromter

Copy code


  1. /*
  2. Posts a message to a Twitter account when you press the mouse button.
  3. Uses Twitter4j, http://twitter4j.org.
  4. For more info: http://tinkerlondon.com/now/2010/09/13/oauth-twitter-and-processing/
  5. Daniel Soltis, September 2010
  6. */
  7. import cc.arduino.*;
  8. import twitter4j.conf.*;
  9. import twitter4j.internal.async.*;
  10. import twitter4j.internal.org.json.*;
  11. import twitter4j.internal.logging.*;
  12. import twitter4j.http.*;
  13. import twitter4j.api.*;
  14. import twitter4j.util.*;
  15. import twitter4j.internal.http.*;
  16. import twitter4j.*;
  17. Arduino arduino;
  18. import processing.serial.*; //new 


  19. String msg = "Ollie"; //my tweet

  20. //copy and paste these from your application in dev.twitter.com
  21. String consumer_key = "cv50u99znWOAOnOEUkhDA";
  22. String consumer_secret = "dzVnD4nLzQd9FAUg47AyEVJKPQgY6ey6dsdOZDsj2FY";
  23. String oauth_token = "276648172-s7m9Z4BPDlREyGJkllFKu84lguDeWkJU4ZKm6Y9E";
  24. String oauth_token_secret = "zKN1i9SGNeh6PDjrEmv8BzeAYrErRac3Mjfs9XjU";

  25. color bgcolor = color(255);
  26. long timer;
  27.  Serial myPort;        // The serial port

  28. int zpin = 2;                  // z-axis of the accelerometer
  29. int ypin = 1;                  // y-axis
  30. int xpin = 0; 
  31.  

  32.  
  33.  void setup() {
  34. size(640,480);

  35. //new below
  36.  // List all the available serial ports
  37.  println(Serial.list());
  38.  // I know that the first port in the serial list on my mac
  39.  // is always my  Arduino, so I open Serial.list()[0].
  40.  // Open whatever port is the one you're using.
  41.  myPort = new Serial(this, Serial.list()[0], 9600);
  42.  // don't generate a serialEvent() unless you get a newline character:
  43.  myPort.bufferUntil('\n');
  44.  //new above

  45. }

  46. void draw() {
  47. background(bgcolor);

  48. bgcolor = color(255);
  49. twitter();
  50. }

  51. void twitter(){

  52. if(zpin > 600) {
  53.  
  54. Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance (
  55. consumer_key, consumer_secret,
  56. new AccessToken( oauth_token, oauth_token_secret) );
  57. try {
  58. Status st = twitter.updateStatus(msg + " "  + second());
  59. println("Successfully updated the status to [" + st.getText() + "].");
  60. bgcolor = color(0,0,255);
  61. timer = millis();
  62. }
  63. catch (TwitterException e) {
  64. println(e.getStatusCode());
  65. }
  66. }}
You are checking if zpin, which is 2, is superior to 600. No wonder you don't send messages...
You must read serial data from the Arduino to know the value of the accelerometer. And of course, the Arduino must send it too.
Beside, you should add a time constraint, to limit the number of tweets per minute (I believe there is a daily limit).
phi.lho i don't think i need a time constraint because im using the accelerometer to collect realtime data and if it does what its suppose to do it posts to twitter.

i'v checked to make sure that the code can link to twitter by deleting the if statement and it worked. (i put it back)

i've added an analogread for the zpin its hovering around 550 (like all accelerometers) and printing in the processing serial monitor 

so it must be a problem with my if statement (i think) im used to arduino coding and not that familiar with the processing language what am i doing wrong?

here is my updated code.

Copy code


  1. /*
  2. Posts a message to a Twitter account when you press the mouse button.
  3. Uses Twitter4j, http://twitter4j.org.
  4. For more info: http://tinkerlondon.com/now/2010/09/13/oauth-twitter-and-processing/
  5. Daniel Soltis, September 2010
  6. */
  7. import cc.arduino.*;
  8. import twitter4j.conf.*;
  9. import twitter4j.internal.async.*;
  10. import twitter4j.internal.org.json.*;
  11. import twitter4j.internal.logging.*;
  12. import twitter4j.http.*;
  13. import twitter4j.api.*;
  14. import twitter4j.util.*;
  15. import twitter4j.internal.http.*;
  16. import twitter4j.*;
  17. Arduino arduino;
  18. import processing.serial.*; //new 


  19. String msg = "Ollie"; //my tweet

  20. //copy and paste these from your application in dev.twitter.com
  21. String consumer_key = "cv50u99znWOAOnOEUkhDA";
  22. String consumer_secret = "dzVnD4nLzQd9FAUg47AyEVJKPQgY6ey6dsdOZDsj2FY";
  23. String oauth_token = "276648172-s7m9Z4BPDlREyGJkllFKu84lguDeWkJU4ZKm6Y9E";
  24. String oauth_token_secret = "zKN1i9SGNeh6PDjrEmv8BzeAYrErRac3Mjfs9XjU";

  25. color bgcolor = color(255);
  26. long timer;
  27.  Serial myPort;        // The serial port

  28. int zpin = 2;                  // z-axis of the accelerometer

  29.  

  30.  
  31.  void setup() {
  32. size(640,480);

  33. //new below
  34.  // List all the available serial ports
  35.  println(Serial.list());
  36.  // I know that the first port in the serial list on my mac
  37.  // is always my  Arduino, so I open Serial.list()[0].
  38.  // Open whatever port is the one you're using.
  39.  myPort = new Serial(this, Serial.list()[0], 57600);
  40.  // don't generate a serialEvent() unless you get a newline character:
  41.  myPort.bufferUntil('\n');
  42.  //new above
  43.   arduino = new Arduino(this, Arduino.list()[0], 57600); // you may need to change the "0" depending on your computer.
  44. }

  45. void draw() {
  46. background(bgcolor);

  47. bgcolor = color(255);
  48. twitter();
  49. }

  50. void twitter(){

  51. println (arduino.analogRead(zpin));

  52. if(zpin>600) {
  53.   
  54. println("entering Twitter method"); 

  55. println("Twitter auth complete");

  56. Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance (
  57. consumer_key, consumer_secret,
  58. new AccessToken( oauth_token, oauth_token_secret) );
  59. try {
  60. Status st = twitter.updateStatus(msg + " "  + second());
  61. println("Successfully updated the status to [" + st.getText() + "].");
  62. bgcolor = color(0,0,255);
  63. timer = millis();
  64. }
  65. catch (TwitterException e) {
  66. println(e.getStatusCode());
  67. }
  68. }}


I invite you to re-read fully my answer.
To my knowledge (limited in the Arduino field, I reckon), zpin won't be magically updated. So your 'if' condition will be always false.
how do i update the zpin then?
By reading the value on the serial port and assigning it to zpin or some other variable to use in the condition. Basically, you do the reading in the println(), no?
Got it thanks!!

here is the final code

Copy code


  1. /*
  2. Posts a message to a Twitter account when you press the mouse button.
  3. Uses Twitter4j, http://twitter4j.org.
  4. For more info: http://tinkerlondon.com/now/2010/09/13/oauth-twitter-and-processing/
  5. Daniel Soltis, September 2010
  6. */
  7. import cc.arduino.*;
  8. import twitter4j.conf.*;
  9. import twitter4j.internal.async.*;
  10. import twitter4j.internal.org.json.*;
  11. import twitter4j.internal.logging.*;
  12. import twitter4j.http.*;
  13. import twitter4j.api.*;
  14. import twitter4j.util.*;
  15. import twitter4j.internal.http.*;
  16. import twitter4j.*;
  17. Arduino arduino;
  18. import processing.serial.*; //new 


  19. String msg = "Ollie"; //my tweet

  20. //copy and paste these from your application in dev.twitter.com
  21. String consumer_key = "cv50u99znWOAOnOEUkhDA";
  22. String consumer_secret = "dzVnD4nLzQd9FAUg47AyEVJKPQgY6ey6dsdOZDsj2FY";
  23. String oauth_token = "276648172-s7m9Z4BPDlREyGJkllFKu84lguDeWkJU4ZKm6Y9E";
  24. String oauth_token_secret = "zKN1i9SGNeh6PDjrEmv8BzeAYrErRac3Mjfs9XjU";

  25. color bgcolor = color(255);
  26. long timer;
  27.  Serial myPort;        // The serial port

  28. int zpin=2;                  // z-axis of the accelerometer

  29.  

  30.  
  31.  void setup() {
  32. size(640,480);

  33. //new below
  34.  // List all the available serial ports
  35.  println(Serial.list());
  36.  // I know that the first port in the serial list on my mac
  37.  // is always my  Arduino, so I open Serial.list()[0].
  38.  // Open whatever port is the one you're using.
  39.  myPort = new Serial(this, Serial.list()[0], 57600);
  40.  // don't generate a serialEvent() unless you get a newline character:
  41.  myPort.bufferUntil('\n');
  42.  //new above
  43.   arduino = new Arduino(this, Arduino.list()[0], 57600); // you may need to change the "0" depending on your computer.
  44. }

  45. void draw() {
  46. background(bgcolor);

  47. bgcolor = color(255);
  48. twitter();
  49. }

  50. void twitter(){

  51. println (arduino.analogRead(zpin));

  52. if((arduino.analogRead(zpin)) > 600) 
  53. {  
  54. println("entering Twitter method"); 

  55. println("Twitter auth complete");

  56. Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance (
  57. consumer_key, consumer_secret,
  58. new AccessToken( oauth_token, oauth_token_secret) );
  59. try {
  60. Status st = twitter.updateStatus(msg + " "  + second());
  61. println("Successfully updated the status to [" + st.getText() + "].");
  62. bgcolor = color(0,0,255);
  63. timer = millis();
  64. }
  65. catch (TwitterException e) {
  66. println(e.getStatusCode());
  67. }
  68. }}