Please Help With Interfacing to Arduino to Print
in
Integration and Hardware
•
2 years ago
Hi all,
I have been really struggling with this problem for a few days now, and have read lots of threads and tutorials with interfacing with processing, but just can't get my sketch to work!?
I'm a bit of a noob, and it is probably something really easy I'm doing wrong, but an help would be appreciated so much.
I am trying to get processing to perform a yahoo search, and then send the result descriptions one at a time to arduino to be printed off. I then want the arduino to send back a "done" signal to processing to get the next description.
With my current code, the first description just keeps printing repeatedly!? Please help someone - getting very infuriated with it.
Processing Code:
import pyahoo.*;
import com.yahoo.search.WebSearchResults;
import processing.serial.*;
Serial port;
boolean done;
// Create a YahooSearch object. You have to pass in the API key given to you by Yahoo.
YahooSearch yahoo;
char [][]target;
void setup() {
size(200,200);
port = new Serial(this,Serial.list()[0], 9600);
// Make a search object, pass in your key
yahoo = new YahooSearch(this, "B5rzyEvV34EjE7c3OWj3BWOXBrP3f.b3HF9hLPGyB0r5CopLwlFLrvhdw8X1.wQnPE3mtihee94-");
}
void mousePressed() {
// Search for a String. By default you will get back 10 results.
// If you want more (or less), you can request a specific number by saying:
yahoo.search("peter thomas", 5);
}
void draw() {
noLoop();
}
// When the search is complete
void searchEvent(YahooSearch yahoo) {
// Get Titles and URLs
//String[] titles = yahoo.getTitles();
// Search results arrive as an array of Strings.
// You can also get the summaries with getSummaries().
//String[] urls = yahoo.getUrls();
String[] summaries = yahoo.getSummaries();
println(summaries);
int maxSummaryLength = 0;
for (int i=0; i<summaries.length; i++) {
if ( maxSummaryLength < summaries[i].length()) {
maxSummaryLength = summaries[i].length();
}
}
char[][] target = new char[summaries.length][maxSummaryLength];
for (int i=0; i<summaries.length; i++) {
for (int j=0; j<summaries[i].length(); j++) {
target[i][j] = summaries[i].charAt(j);
}
}
for (int k=0; k<summaries[0].length(); k++) {
port.write(target[0][k]);
port.clear();
//delay (1000);
checkDone();
if (done == true) {
port.write(target[1][k]);
}
else {
port.clear();
//noLoop();
}
}
}
void checkDone() {
boolean done = false;
if(port.available() > 0) {
int serVal = port.read();
if (serVal == 111) {
done = true;
}
}
}
Arduino Code:
#include <SoftwareSerial.h>
#define rxPin 6
#define txPin 7
SoftwareSerial printer = SoftwareSerial(rxPin, txPin);
const byte command = 0x1B;
const byte fullcut = 0x69;
const byte partialcut = 0x6D;
const byte doubleprint = 0x47;
const byte flipchars = 0x7B;
const byte rotatechars = 0x56;
const byte commandBarcode = 0x1D;
const byte commandBarcodePrint = 0x6B;
const byte commandBarcodeWidth = 0x77;
const byte commandBarcodeHeight = 0x68;
const byte commandBarcodeTextPosition = 0x48;
const byte barcodeNarrow = 0x02;
const byte barcodeMedium = 0x03;
const byte barcodeWide = 0x04;
const byte barcodePrintNone = 0x00;
const byte barcodePrintAbove = 0x01;
const byte barcodePrintBelow = 0x02;
const byte barcodePrintBoth = 0x03;
// (0 = UPC-A, 1 = UPC-E, 2 = JAN13 (EAN), 3 = JAN 8 (EAN),
// 4 = code 39, 5 = ITF, 6 = codabar, 7 = code 128)
const byte barcodeModeUPCA = 0x00;
const byte barcodeModeUPCE = 0x01;
const byte barcodeModeJAN13AEN = 0x02;
const byte barcodeModeJAN8EAN = 0x03;
const byte barcodeModeCODE39 = 0x04;
const byte barcodeModeITF= 0x05;
const byte barcodeModeCODEABAR= 0x06;
const byte barcodeModeCODE128 = 0x07;
int done = 0;
char inData[200];
char inChar= -1;
byte index = 0;
void setup() {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
printer.begin(9600);
Serial.begin(9600); //open the USB connection too
delay(1000);
feed();
//println("Hello, World!");
//feed();
}
void printOnPrinter(char text[]) {
printer.print(text);
}
void printlnOnPrinter(char text[]) {
printer.println(text);
}
void feed() {
printer.println("");
printer.println("");
printer.println("");
}
void cut() {
printer.print(command, BYTE);
printer.print(fullcut, BYTE);
}
void partialCut() {
printer.print(command, BYTE);
printer.print(partialcut, BYTE);
}
void setBarcodeTextPosition(byte position) {
if (position < 0) position = 0;
if (position > 3) position = 3;
printer.print(commandBarcode, BYTE); // "[1d]H + [48]H + N"
printer.print(commandBarcodeTextPosition, BYTE);
printer.print(position, BYTE); // 0 = none, 1 = above, 2 = below, 3 = both
}
void setDoublePrintOn() {
setDoublePrint(0x01);
}
void setDoublePrintOff() {
setDoublePrint(0x00);
}
void setDoublePrint(byte mode) {
printer.print(command, BYTE);
printer.print(doubleprint, BYTE);
printer.print(mode, BYTE);
}
void loop() {
while(Serial.available() > 0) { //only read if there is data available
if(index < 199) { //one less than size of array
inChar = Serial.read(); //read in a character
inData[index] = inChar; //store character in inData
index++; //increment where to write next character
inData[index] = '\0'; //null terminate the string
}
}
// while there are bytes to read from the computer, retransmit them
printOnPrinter(inData);
delay(1000);
char inData[200];
int done = 111;
Serial.write(done);
}
1