Send data to Arduino then Wait for new data and send new data

edited May 2014 in Arduino

Hey everyone right now I'm running a processing sketch that converts speech to text and then sends that text to my Arduino (uno), the Arduino then takes that text and sends it to a thermal printer.

Right now the processing sketch sends the result over and over again, I need it to send the first text result and then wait for a new result (text) and send that to the arduino and repeat.

Here's the processing code:

        /*
        // This is a basic example to demonstrate how the Speech-To-Text Library 
        // can be used. See www.getflourish.com/sst/ for more information on
        // available settings.
        //
        // Florian Schulz 2011, www.getflourish.com
        */
        import processing.serial.*;
        import com.getflourish.stt.*;
        import ddf.minim.*;
        import javax.sound.sampled.*;

    Serial myPort;

    STT stt;
    String result;
    String dataReading = "";
    String [] dataOutput = {};

    void setup ()
    {
      size(600, 200);
       myPort = new Serial(this, "/dev/tty.usbmodem1411", 9600);
      // Init STT automatically starts listening
      stt = new STT(this);
      stt.enableDebug();
      stt.setLanguage("en"); 



      // Some text to display the result
      textFont(createFont("Arial", 24));
      result = "Say something!";

      // Display available Inputs with id and name
      Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();

      for(int i = 0; i < mixerInfo.length; i++)
      {
        println(i + ": " + mixerInfo[i].getName());
      }  

      // Set input (id: 0)  
      Mixer mixer = AudioSystem.getMixer(mixerInfo[2]);

      // Update the Minim instance that STT is using
      Minim minim = stt.getMinimInstance();
      minim.setInputMixer(mixer);

    }

    void draw ()
    {
      background(0);
      text(result, mouseX, mouseY);
      myPort.write(result);

      }


    // Method is called if transcription was successfull 
    void transcribe (String utterance, float confidence) 
    {
      println(utterance);
      result = utterance;
    }
    public void keyPressed () {
      stt.begin();
    }
    public void keyReleased () {
      stt.end();
    }

and heres the Arduino code if needed:

#include "SoftwareSerial.h"


int printer_RX_Pin = 2;  // this is the green wire
int printer_TX_Pin = 3;  // this is the yellow wire
SoftwareSerial Thermal(2, 3);

#define FALSE  0
#define TRUE  1
int printUpSideDown = TRUE;
int heatTime = 255; //80 is default from page 23 of datasheet. Controls speed of printing and darkness 
int heatInterval = 255; //2 is default from page 23 of datasheet. Controls speed of printing and darkness 
char printDensity = 20; //Not sure what the defaut is. Testing shows the max helps darken text. From page 23. 
char printBreakTime = 15; //Not sure what the defaut is. Testing shows the max helps darken text. From page 23. 
char val;

void setup()
{

  Serial.begin(9600); // for debug info to serial monitor   
  Thermal.begin(19200); // to write to our new printer 
}

void loop()
{
if (Serial.available()) {
  val = Serial.read(); // read it and store it in val 
  Thermal.print(val);


}

 }

Thank you guys for any help you can provide its greatly appreciated!

Answers

  • What you can do simply use an integer named state. If the value of state changes then and only then send data to the arduino.

    You can use something like following.

    if(state != prvstate)
    {
      myport.write(result);
      prvstate = state;
    }
    
    void transcribe (String utterance, float confidence) 
    {
      println(utterance);
      result = utterance;
      state = state +1;
    }
    

    I hope this solves your problem.

Sign In or Register to comment.