Go Down

Topic: Serial marker (Read 1 time) previous topic - next topic

bluebubble

Dec 22, 2011, 07:16 pm Last Edit: Dec 26, 2011, 09:42 pm by bluebubble Reason: 1
Dear forum

I'm working on some projects inculding processing and came to problem I can't  nor find any informations on how to solve them.

The scenario:

Processing got 4 faders which send their values continously to the arduino in 4 different bytes. The arduino takes those values and stores them in an arraylist which then put in another function which includes delays. At this point the problem beginns, because there is no beginning nor and end so after the function with the delays haft passed, the arduino starts to store the values not from the beginning.


The arduino code to store the values:

Code: [Select]

void serial()
{
 if(Serial.available())
 {
   counter = 0;
   anfang = Serial.read();
   if(anfang == 'H')
   {
     while(counter < 4)
     {
       byteincomming[counter++] = Serial.read();
     }
   
   }
   ton1 = map(byteincomming[0], 0, 255, 300, 500);
   ton2 = map(byteincomming[1], 0, 255, 300, 500);
   ton3 = map(byteincomming[2], 0, 255, 300, 500);
   ton4 = map(byteincomming[3], 0, 255, 300, 500);
 }
}


The arduino funuction with the delays:

Code: [Select]

void Ton()
{
 if(an == true || dauer == true)
 {
   tone(toner, ton1);
   delay(300);
   tone(toner, ton2);
   delay(300);
   tone(toner, ton3);
   delay(300);
   tone(toner, ton4);
   delay(300);
   an = !an;
 }
 else noTone(toner);
}
   
   



The processing code:

Code: [Select]

import processing.serial.*;

Schieberegler[] regler = new Schieberegler[4];

Serial port;

void setup()
{
 background(255);
 size(500, 500);
 smooth();
 port = new Serial(this, Serial.list()[1], 9600);
 for(int i = 0; i < regler.length; i++)
 {
   regler[i] = new Schieberegler((width / regler.length) * (i+ 0.5), 250, i);
 }
}


void draw()
{
 background(255);
 for(int i = 0; i < regler.length; i++)
 {
   regler[i].fadenzeigen();
   regler[i].schieben();
   regler[i].kugelzeigen();
   regler[i].wertanzeigen();
 }
 
}

class Schieberegler
{
 float x, yfaden;
 float ykugel;
 float hoehe = 100;
 int wert;
 int platz;
 
 Schieberegler(float xpos, float ypos, int posi)
 {
   x = xpos;
   yfaden = ypos;
   ykugel = ypos;
   platz = posi;
 }
 
 void fadenzeigen()
 {
   fill(0);
   ellipse(x, yfaden, 4, hoehe);
 }
 
 void kugelzeigen()
 {
   fill(255);
   ellipse(x, ykugel, 10, 8);
 }
 
[b]  void schieben()[/b]
 {
   if(mouseX > x - 4 && mouseX < x + 4 && mouseY > yfaden - hoehe/2 && mouseY < yfaden + hoehe/2 && mousePressed && mouseButton == LEFT) // Value is created
   {
     ykugel = mouseY;
     wert = int(map(ykugel, yfaden - hoehe/2, yfaden + hoehe/2, 255, 0));
   }
 }[b]
 void wertanzeigen() // Serial out[/b]
 {
  if(platz == 0) port.write('H');
  port.write(wert);
 }
}
     




I realy have no idea how to put an beginning byte and an ending one, so could you give me a quick reference? Because I'm sure this been discussed befor.


Thanks in advance
bluebubble

PaulS

You need to post all of your Processing sketch. From what you posted, we have no idea what type of variable regler is, except that it is an array. The type appears to be an object, in that you can call it's methods. However, the method wertanzeigen() is not the same as the function wertanzeigen() that is shown.

The method belongs to a class. The function does not.

What appears to be happening though is that Processing sends data faster than the Arduino can read it. When that happens, data that doesn't fit in the buffer is simply discarded. When the Arduino finally gets around to reading some more data, room is created in the buffer for a few more bytes. After a while, the bytes in the buffer are no longer part of the same packet.

There are any number of ways to deal with the problem. The simplest is to simply have the Arduino sent a message to the Processing application saying that it is ready for more data, and only send data when the Arduino hollers "Ready!".

The draw() function is like the loop() function, in that it is called in an endless loop.
The art of getting good answers lies in asking good questions.

bluebubble

I now updated both codes and tried to implement such a ''ready'' comment which in my case is 'H'. but it is still not working. Any ideas?


bluebubble

PaulS

Quote
Any ideas?

You now have the Processing application pumping out more data, not less.

I don't really understand what you are trying to do here. The Processing application was outputting a single int on each pass through draw(). It now outputs 'H', the int, and an 'L'.

The Arduino application now reads the serial data, and, if there is at least one byte to read, reads all 5 of them. How is that supposed to work? Even if it did, why are you reading 4 bytes after the 'H'? You are writing one int, and expecting to read 4 bytes. I don't get it.
The art of getting good answers lies in asking good questions.

bluebubble

The 'L' was a misstake, but i'm trying to tell the arduino to only read the 4 bytes after it reads the 'H'. after it has read the 'H' it should start reading to faders value.

I'm kinda unsure, but am i doing everything wrong?

PaulS

Quote
i'm trying to tell the arduino to only read the 4 bytes after it reads the 'H'.

First, you have this backwards. Processing should only send data again when the Arduino says it is ready. It should say it is ready be sending something to the serial port, which Processing needs to read.

Second, the Processing application is sending an int to the serial port. It isn't clear how it is doing so. The Processing documentation is worse than the Arduino documentation. It might be sending two bytes, in which case reading 4 bytes is silly. It might be sending the data as a string of characters. In this case, playing a note based on a character is a silly thing to do.

In no case is Processing sending 4 values, so why you are reading 4 values is a mystery. Especially without checking that there is any data to read.
The art of getting good answers lies in asking good questions.

Go Up