Having Processing send serially to Arduino

I'm very noob at all this so please don't assume any prior knowledge :)

I have a small program for the Arduino which has the Piezo element (poorly) replicate a bird tweet.

I've also been experimenting with TwitterJ which is a Java library that can used to access the Twitter API.

If I send or receive a tweet I would like to have the Arduino play the tweet sound.

I'm sure I can find a way to use the TwitterJ library to tell me whether I've received a new tweet. Then once I've received a new tweet presumably I can use the Serial class in processing to send a "1" to the Arduino.

Then in my Arduino code I'll have something like:

boolean tweet;

....

if(tweet==1) { //play tweet }

I'm looking for any pointers at all, but my first question would be if I send this "1" value to the Arduino, how do I plug that value into the tweet variable?

Thanks in advance!

If Processing writes data to the serial port, the Arduino can read it, using Serial.available() to determine how many bytes, if any, are available to read, and Serial.read() to read the bytes, one at a time.

The value read from the serial port is a byte, not a boolean. Boolean variables are for true or false, not 0 or 1. While internally the boolean value is a 0 or a 1, boolean variables are usually used like this:

boolean needToTweet;

void loop()
{
  needToTweet = false;
  if(Serial.available() > 0)
  {
     byte serVal = Serial.read();
     if(serVal == '1')
        needToTweet = true;
  }

  if(needToTweet)
  {
     // Make some awful noise
  }
}

Hi Paul

I've been trying something very much along those lines.

This is the Arduino sketch (if thats the right term) that I upload to my board:

int incomingByte = 0;
int ledPin = 12;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  
  if(Serial.available() > 0) {
  
    
      digitalWrite(ledPin, HIGH);
    
      
  }
}

And here's the Processing sketch, that I run once the Arduino sketch has been uploaded:

import processing.serial.*;

Serial myPort;

void setup() {
  
  myPort = new Serial(this, "COM3", 9600);
  myPort.write(1);
   
}

void draw() {
}

As you can see I'm just experimenting with the LED. In theory, when I run the processing sketch the LED should light up and stay lit... it doesn't so far.

When you create a open the serial port in Processing (or any other way), the Arduino resets.

There is a delay while the serial port software is initialized on the Arduino.

Meanwhile, the Processing application has sent data to the serial port before the connection has been accepted by the Arduino, resulting in a loss of that data.

Since no other data gets sent by Processing, the Arduino never receives any serial data.

Try adding a delay in the Processing sketch, between the new Serial statement and the write to the port, to wait for the Arduino to get ready.

Apparently the issue was that Serial.write() should have gone inside the draw method.

Apparently the issue was that Serial.write() should have gone inside the draw method.

That's certainly an option. In most cases, it's really the preferred way, since draw() is called in an endless loop, like loop() on the Arduino.

Without some way of making the sending of data to the Arduino conditional, though, you'll overwhelm the ability of the Arduino to handle serial data (especially as you don't actually read the data).

You can expand on what you have now by drawing on and off "buttons" in Processing, and sending 0 or 1 whenever a "button" is pressed. Be sure to actually read the data in the Arduino sketch, and turn the LED off in response to a 0.

Note also that the data is sent as characters to the Arduino, and that '0' and 0 are not the same. Nor are '1' and 1. But, also note that '0' - '0' is 0 and '1' - '0' is 1.

Cheers Paul

I've got to the stage where whenever a tweet is sent with "#whirligig" in it, the Arduino will light up the LED. Basically I check whether there are more tweets related to the tag compared to when the draw() method was last called. Then send a serial message to he Arduino for each tweet.

As I mentioned above, I have another sketch which simulates a tweeting bird. This is in a different sketch. Is there anyway to "call" that sketch and not throw all the code in the existing sketch (which would really confuse things).

Thanks.

You can't have two sketches loaded at once, which is what would be required if one sketch were to call another.

You can create a library, though, and call functions in the library. Then, include the library in both sketches.

Fab. Cheers.

OK, I've moved onto a new project. This time using OpenCV to detect a blob. I want to pass the x-value of the blob serially to the arduino and use that value as the position of the servo. Then put a puppet on the servo or something.

Am just experimenting at the moment.

How many bytes are sent whenever I write serially in Processing? Can I just pass that number directly to the servo? It would be good to check whether or not the numbers are coming through. But if I try to print to the Serial Monitor, I can't open the connection because Processing is already using the port. Is there a way to write to the console in Arduino?

Thanks. M

How many bytes are sent whenever I write serially in Processing?

Depends on what you write... If you are trying to write a byte in Processing, and read that as a byte on the Arduino, you can do that. You just need to make sure that you are indeed writing a byte in Processing as a byte rather than a string.

Is there a way to write to the console in Arduino?

Sure is. First, you attach a console to the Arduino...

That's why many of us have obtained serial LCDs.