Values through Serial not matching from Arduino to Processing

edited September 2016 in Arduino

I'm trying to adapt some code that will speak tweets through Processing (using the twitter4j and guru.ttslib libraries) but ONLY when processing has detected the correct value from Arduino.

I've managed to get the tweets read out through Processing and I've also got Arduino sending values to Serial. The values I'm sending are simple integers of '1' and '2', but Processing picks these Serial values up as longer numbers. For example, when a '1' is sent from Arduino, Processing reads (and prints) this as '491310'.

Because the value of the int cannot exceed 255, any if statement I write in Processing to recognise '491310' doesn't work. Ideally, I'd like '1' to be sent from Arduino and for it to be read as '1' by Processing

Any help would be greatly appreciated. Absolute novice here. Thanks.

PROCESSING CODE:

import twitter4j.util.*;
import twitter4j.*;
import twitter4j.management.*;
import twitter4j.api.*;
import twitter4j.conf.*;
import twitter4j.json.*;
import twitter4j.auth.*;

import guru.ttslib.*;
import processing.serial.*;

java.util.List statuses = null;


TTS tts;
Serial myPort;

int sensor = 0;

void setup() {
  tts = new TTS();

  myPort = new Serial(this, Serial.list()[0], 9600);

  ConfigurationBuilder cb = new ConfigurationBuilder();

  cb.setOAuthConsumerKey("XXXX");
  cb.setOAuthConsumerSecret("XXXX");
  cb.setOAuthAccessToken("XXXX");
  cb.setOAuthAccessTokenSecret("XXXX");

  Twitter twitter = new TwitterFactory(cb.build()).getInstance();

  String userName ="@BBC";
  int numTweets = 19;
  String[] twArray = new String[numTweets];

  try {
    statuses = twitter.getUserTimeline(userName);
  }
  catch(TwitterException e) {
     e.printStackTrace();
  }
}

void serialEvent (Serial myPort) {
  int inByte = myPort.read();
  sensor = inByte;
  print(sensor);
}

void draw() {
  if (statuses != null) {
    for (int i=0; i<statuses.size(); i++) {
      Status status = (Status)statuses.get(i);

      if (sensor == 491310) {
        println(status.getUser().getName() + ": " + status.getText());
        tts.speak(status.getUser().getName() + ": " + status.getText());
      }
    }
  }
}

Answers

  • This question is a little old, but maybe you are still interested in an answer.

    The problem that you are facing arouses because you are sending characters/bytes from arduino. Then in processing you convert them to an integer. When you look up 49, 13 and 10 in an ascii-table, you will see that they represent a "1", a line-feed and a carriage-return.

    The variable "sensor" will never have the value of 491310, because it will only contain the last character that has been received. It just looks like one longer number in the console because they are printed out next to eachother.

  • Hello! Many thanks for getting back to this question; definitely still interested in an answer.

    That makes sense, thank you. Would you be able to explain how I'd be able to get around this?

  • Well there is a handy function to read from the serial-port until it receives a delimiter: Serial.readStringUntil()

    You will still have a string that includes LF and CR characters. To remove them, you can use String.trim()

    Here is an example, hope it helps:

    void serialEvent (Serial myPort) { 
    
      // reads from serial-port until line-break (ascii 13)
      String line = myPort.readStringUntil(13); 
    
      // check for nonsense
      if (line != null) {
    
        // remove whitespace-characters and convert to int
        int value = int(line.trim()); 
    
        println("received value: "+value);
      }
    }
    
Sign In or Register to comment.