Go Down

Topic:  Serial reading to write in Array (Read 1 time) previous topic - next topic

Capde

I'm reading out the serial port the following


255
255
255
255
0
0
0
0
0
0
0
0
255
255
255
255
0
0
0
0
0
0
0
0
255
.
.
.
etc


Well, what I'm attempting to do is create an array of zeros (in fact a variable value that is less than 255), ie 4 is my header like to know where where to begin reading.

But I can not do  
Code: [Select]

import processing.serial.*;

Serial myPort;  

void setup() {

 myPort = new Serial(this, Serial.list()[0], 2400);
 size(400,200);
}

void draw() {
 int ch = 0;
 int[] sensors = new int[8];
 while (myPort.available() > 0) {
   int inByte = myPort.read();
   if (inByte==255){
     ch=ch+1;
   }
   else
     ch=0;
 }
 while(ch==4){
   for(int i = 0; i >= sensors.length; i = i + 1 ){
     sensors[i] = myPort.read();
   }
 }
 
color c1 = color(#00FF00);
color c2 = color(#FF9900);
color c3 = color (#FF0000);
color c4 = color (#FFFFFF);


if (sensors[0] >= 200){
 fill(c3);
}
else if (sensors[0] >= 100){
 fill(c2);
}
else if (sensors[0] >= 6){
 fill(c1);
}
else{
 fill (c4);
}  

rect(0, 0, 100, 100);

if (sensors[1] >= 200){
 fill(c3);
}
else if (sensors[1] >= 100){
 fill(c2);
}
else if (sensors[1] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(100, 100, 100, 100);
if (sensors[2] >= 200){
 fill(c3);
}
else if (sensors[2] >= 100){
 fill(c2);
}
else if (sensors[2] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(300, 100, 100, 100);
if (sensors[3] >= 200){
 fill(c3);
}
else if (sensors[3] >= 100){
 fill(c2);
}
else if (sensors[3] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(200, 0, 100, 100);
if (sensors[4] >= 200){
 fill(c3);
}
else if (sensors[4] >= 100){
 fill(c2);
}
else if (sensors[4] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(300, 0, 100, 100);
if (sensors[5] >= 200){
 fill(c3);
}
else if (sensors[5] >= 100){
 fill(c2);
}
else if (sensors[5] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(200, 100, 100, 100);
if (sensors[6] >= 200){
 fill(c3);
}
else if (sensors[6] >= 100){
 fill(c2);
}
else if (sensors[6] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(0, 100, 100, 100);
if (sensors[7] >= 200){
 fill(c3);
}
else if (sensors[7] >= 100){
 fill(c2);
}
else if (sensors[7] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(100, 0, 100, 100);


}




Any advice ??



Sorry about my English

digimike

Please do us all a favor and annotate (comment on) each section of your code so we have a better idea of what the code should be doing.

Capde

Sorry, OK !



Code: [Select]
import processing.serial.*;

Serial myPort;

void setup() {

 myPort = new Serial(this, Serial.list()[0], 2400);
 size(400,200);

}

void draw() {

 int ch = 0;      
                                               
 int[] sensors = new int[8];

 while (myPort.available() > 0) {

   int inByte = myPort.read();

   if (inByte==255){                                //Detec  header
     ch=ch+1;
   }
   else
     ch=0;
 }
 while(ch==4){                                  

  //When the head count is 4, started writing the series puero values in the array
   
for(int i = 0; i >= sensors.length; i = i + 1 ){

     sensors[i] = myPort.read();
   }
 }



//This part of codifgo, functions as a semaphore, depending on the value i of the position of the array show a color or other


color c1 = color(#00FF00);
color c2 = color(#FF9900);
color c3 = color (#FF0000);
color c4 = color (#FFFFFF);




if (sensors[0] >= 200){
 fill(c3);
}
else if (sensors[0] >= 100){
 fill(c2);
}
else if (sensors[0] >= 6){
 fill(c1);
}
else{
 fill (c4);
}

rect(0, 0, 100, 100);

if (sensors[1] >= 200){
 fill(c3);
}
else if (sensors[1] >= 100){
 fill(c2);
}
else if (sensors[1] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(100, 100, 100, 100);
if (sensors[2] >= 200){
 fill(c3);
}
else if (sensors[2] >= 100){
 fill(c2);
}
else if (sensors[2] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(300, 100, 100, 100);
if (sensors[3] >= 200){
 fill(c3);
}
else if (sensors[3] >= 100){
 fill(c2);
}
else if (sensors[3] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(200, 0, 100, 100);
if (sensors[4] >= 200){
 fill(c3);
}
else if (sensors[4] >= 100){
 fill(c2);
}
else if (sensors[4] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(300, 0, 100, 100);
if (sensors[5] >= 200){
 fill(c3);
}
else if (sensors[5] >= 100){
 fill(c2);
}
else if (sensors[5] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(200, 100, 100, 100);
if (sensors[6] >= 200){
 fill(c3);
}
else if (sensors[6] >= 100){
 fill(c2);
}
else if (sensors[6] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(0, 100, 100, 100);
if (sensors[7] >= 200){
 fill(c3);
}
else if (sensors[7] >= 100){
 fill(c2);
}
else if (sensors[7] >= 6){
 fill(c1);
}
else{
 fill (c4);
}
rect(100, 0, 100, 100);


}


Groove

#3
Jan 18, 2010, 11:47 am Last Edit: Jan 18, 2010, 11:48 am by GrooveFlotilla Reason: 1
Code: [Select]

// I don't understand this: - what causes the while loop to break?
 while(ch==4){

  //When the head count is 4, started writing the series puero values in the array

for(int i = 0; i >= sensors.length; i = i + 1 ){

     sensors[i] = myPort.read();
   }
Per Arduino ad Astra

Capde

Sorry it is a mistake, it would be an IF

Go Up