Go Down

Topic: Processing Seriel communication Question (Read 833 times) previous topic - next topic

HammerH

Hi guys,

i have following question. Is there a way, that both Serial.write(port.write) and Serial.read(port.read) will work in one code?

Because until now, i can run either serial.write or serial.read but if i put them both in one code, just the first function is working....

I have to send from my Arduino Duemilanove some informations to Processing and on the other way some information from Processing to Arduino...

I hope somebody can help me. I'm pretty new in this.

Best Regards,

HammerH

spycatcher2k

Please upload you code for us to look at, and maybe we could help.



Drew.
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

PaulS

Quote
Please upload you code for us to look at, and maybe we could help.

Good suggestion. To answer the original question, though, the answer is yes, you can have Processing sent data to AND read data from the Arduino, and vice versa.

HammerH

Ok. I have to program the use of the DangerShield of ZachHoeken. The Images are just pictures of the DangerShield and some Leds on and so on.

I want to read the value of the first Slider. At first it didn't want to work, now Processing prints me the only the first value all the time and even though the slider is at its fullest position (has a value of 1023) it only sends 255 ...


Here is the processing Code: (by the way, I'm writing code with Eclipse, so don't wonder because of some differences :) )


Code: [Select]

public class DangerShield extends PApplet {

//import class to set up serial connection with wiring board


Serial port;
String test;
PImage Shield;
PImage[] LED1= new PImage[2];
int ld1 = 0;
PImage[] LED2=new PImage[2];
int ld2 = 0;
PImage[] SegLED = new PImage[10];
int i = 0;
int j = 0;
int[] stand = {0,0,0,0,0,0,0};
public void setup()
{
background(255);
Shield=loadImage("DShield.jpg");
LED1[0]=loadImage("LED1aus.png");
LED1[1]=loadImage("LED1an.png");
LED2[0]=loadImage("LED2aus.png");
LED2[1]=loadImage("LED2an.png");
for(int j = 0; j<10;j++)
SegLED[j]=loadImage(""+j+".png");
size(900,375);
println(Serial.list());
port = new Serial(this,Serial.list()[0],9600);
image(Shield,200,0);
}

int slider1 = 0;
public void draw()
{

fLED1();
fLED2();
fSegLed();
portwrite();
if(port.available()>0)
{
slider1 = port.read();
println(slider1);
}



//println("Mausx: " + mouseX + "\tMausy: "+mouseY);
fill(255);
rect(0,0,200,375);
rect(700,0,200,375);
texte();

}



public void fLED1() //Koordinaten: X von 558-580; Y von 163-191
{

boolean over = false;
if(mouseX>=558 && mouseX<=580 && mouseY>=162 && mouseY <=191)
over = true;
//println(over);
if(over==true )
{
i = 1;
image(LED1[stand[i]],555,160); // turns LED on
ld1 = stand[i];
}
}
public void fLED2() //Koordinaten: X von 550-576; Y von 134-167
{

boolean over = false;
if(mouseX>=550 && mouseX<=576 && mouseY>=134 && mouseY <=162)
over = true;
//print(over);
if(over==true )
{
i = 2;
image(LED2[stand[i]],553,133);  //turns LED on
ld2 = stand[i];
}

}
public void fSegLed() //7 Segment-LED
{
boolean over1 = false; // Maus über Button1 X: 380-401 Y:250-270
boolean over2 = false; // Maus über Button2 X: 446-467 Y:250-270
boolean over3 = false; // Maus über Button3 X: 508-531 Y:250-270

if(mouseX>=380 && mouseX<=401 && mouseY >=250 && mouseY <=270)
{
over1 = true;
i = 3;
if(mousePressed)
{
delay(150);
j++;
}

if(j>9)
j = 0;
}
if(mouseX>=446 && mouseX <= 467 && mouseY >=250 && mouseY <=270)
{
over2 = true;
i = 3;
if(mousePressed)
{
delay(150);
j--;
}
if(j<0)
{
j=9;
}
}
image(SegLED[j],301,212);

}
public void portwrite()
{
if(ld1 == 1)
port.write('A');
if(ld1 == 0)
port.write('B');
if(ld2 == 1)
port.write('C');
if(ld2 == 0)
port.write('D');
port.write(j);
}
public void texte()
{
fill(0);
line(110,20,110,355);
text("Wert von Slider1:",2,60);
}
public void mouseReleased()
{
stand[i] = 1 - stand[i];
}

}



and the Arduino Code:


Code: [Select]

#include <WProgram.h>
#include <stdio.h>

#define LatchPin 8
#define ClockPin 12
#define DataPin 13

byte chardata[] =
{
  B00111111, //0
  B00000110, //1
  B01011011, //2
  B01001111, //3
  B01100110, //4
  B01101101, //5
  B01111101, //6
  B00000111, //7
  B01111111, //8
  B01101111 //9
};

extern "C" void __cxa_pure_virtual()
{
  cli();
  for (;;);
}

int i = 0;
int led;
int oldled;
char ascii[6];

void setup() {
Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(LatchPin, OUTPUT);
pinMode(ClockPin, OUTPUT);
pinMode(DataPin, OUTPUT);
pinMode(A0, INPUT);
digitalWrite(LatchPin, LOW);
shiftOut(DataPin, ClockPin, MSBFIRST, 0);
digitalWrite(LatchPin,HIGH);
}

void loop()
{
int slider1 = analogRead(A0);
Serial.write(slider1);
if(Serial.available())
{
led = Serial.read();
if ( led == 'A')
digitalWrite(6, HIGH);
if (led == 'B')
digitalWrite(6, LOW);
if (led == 'C')
digitalWrite(5,HIGH);
if (led == 'D')
digitalWrite(5, LOW);
if(led!=oldled)
{
digitalWrite(LatchPin, LOW);
shiftOut(DataPin, ClockPin, MSBFIRST, chardata[led]);
digitalWrite(LatchPin,HIGH);
oldled = led;
}
}

}


int main (void)
{
init ();
setup();
for(;;)
loop();
}

PaulS

Code: [Select]
int slider1 = analogRead(A0);
Serial.write(slider1);

The analogRead function returns an int. The Serial.write() function expects a byte (there are 2 of them in an int).

It will strongly reinforce the need to understand matching argument types if you look at the code for the Serial instance, defined in HardwareSerial.h and HardwareSerial.cpp, and trace what happens when you call Serial.write() with an int.

HammerH

I have now modified the code so that my arduino sends a string and processing receives it and splits it into smaller strings :

Arduino:
Code: [Select]

slider1 = analogRead(A0);
int slider2 = analogRead(A1);
sprintf(ascii,"A%04d,%04dE", slider1, slider2);
Serial.write(ascii);



Processing:

Code: [Select]

if(port.available()>0)
{
String inBuffer = port.readString();
if(inBuffer!=null)
{
int posA = inBuffer.indexOf("A");
temp = inBuffer.substring(posA+1);
int posE = temp.indexOf("E");
if(posE>=0)
{
temp= temp.substring(0,posE);
values = split(temp,",");
                }


this works well but now i have the problem that my DangerShield is much slower acting to the port.writes of processing and the 7-segment led is blinking all the time and doesn't show the numbers properly...

PaulS

Please explain why you won't use Serial.print() on the Arduino.

HammerH

Do I need Serial.print?

I used it to control the Slider values, which were 1023 if in highest position but otherwise I don't know why I should use it?

Now i use the string, because i want to send more values to processing and this is the easiest way I know so far.


PaulS

You are sending data to the serial port. To do that, you need to use either Serial.print() or Serial.write(). Serial.write() is intended to send single bytes to the serial port. When called with anything larger than byte-sized, such as an array, it calls other versions of print() and write() internally.

Serial.print() is intended to send multiple bytes to the serial port, either as strings or multi-byte values, depending on the optional second argument.

Since you are trying to send multiple bytes to the serial port, it seems obvious that you should be using Serial.print().

While Serial.write() does something with the data you send it, it may not be doing what you expect. Use the tool that does what you (or, at least, I) expect.

Go Up