Processing to Arduino (strings bigger than 64 bytes)

Processing to Arduino Serial example

Below is some code which allow you to send a large string form processing to arduino. This is problomatic as the arduino serial buffer is restricted to 64 bytes, which in terms of text ain’t even a tweet. You will need to upload the arduino code and then run the processing sketch to get it working.

enjoy.

/*************************
    processing:
**************************/
import processing.serial.*;
Serial myPort;
//the string I want to send
String stringOut = "";

char[] strChar;

void setup() {
  // punt my string from a string to a char array
  strChar = stringOut.toCharArray();
  //set up serial port for your arduino
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

//global index into the char array
int i=0;

void serialEvent(Serial sp) 
{
  //if we have a serial connectiong
  if ( sp.available() > 0) {
    //grab a line
    String b = sp.readStringUntil('\n');
    if (b!= null) { 
      // trim off any excess white space and check if
      // the arduino is 'ready' for a char'
      if (b.trim().equals("ready")) {
        //if it is fire it down the serial
        myPort.write(strChar[i]);
        
        // check if we are at the end of our string
        if (i<stringOut.length()-1) {
          //if not step on
          i++;
        }
        else {
          //if we are go back to the begining
          i=0;
        }
      }
    }
  }
}


void draw() {
// nothing to draw all don upstairs in serial event
}

/*************************
    Arduino:
**************************/
#include 
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

#define STR_LEN 500

char str[STR_LEN];
int charsRead=0;
boolean reading = false;

void setup() {
  Serial.begin(9600);
  Serial.println("8x8 LED Matrix Test");
  pinMode(13,OUTPUT);
  initStr(str,STR_LEN);

  matrix.begin(0x70);  // pass in the address
  //  scrollText("hello",5);
}


void loop() {
  //fire a shout up the serial to processing to send me a char
  Serial.println("ready");
  
  //if said char had been sent and we have space.....
  if(Serial.available() && charsRead<STR_LEN){
    
    //grab the char
    char ch = Serial.read();

    //opening angle bracket marks begingin of string
    if(ch == ''){
      //stop reading chars
      reading= false;
      //do what you want with the wholes string
      //i have a nice wee led martix
      scrollText(str,charsRead);
      //put the buffer index back to 0
      charsRead = 0;
      //flush the buffer
      initStr(str,STR_LEN);
    }
    
    //when in read mode and  dont have a beging or end of string marker
    if(reading && ch != ''){
      //put the current char on the buffer and 
      //step the buffer index on one
      str[charsRead++] = ch;
    }
  }
}


//this functino is uses the adafut led matrix library to scroll text
//on my 8x8 matrix
void scrollText(char* str, int len){


  matrix.setTextSize(1);
  matrix.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
  matrix.setTextColor(LED_ON);
  for (int8_t x=9; x>=-(8*len); x--) {
    matrix.clear();
    matrix.setCursor(x,0);
    matrix.print(str);
    matrix.writeDisplay();
    delay(50);
  }
}

void scrollText(char ch, int len){
  matrix.setTextSize(1);
  matrix.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
  matrix.setTextColor(LED_ON);
  matrix.clear();
  matrix.setCursor(1,0);
  matrix.print(ch);
  matrix.writeDisplay();
}

void initStr(char* str, int len){
  for(int i=0;i<len;i++){
    str[i] = '';  
  }
}

void flash(int times){
  for(int i=0;i<times;i++){
    digitalWrite(13,HIGH);
    delay(300);
    digitalWrite(13,LOW);
    delay(300); 
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s