Pages: [1]   Go Down
Author Topic:  Serial reading to write in Array  (Read 915 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

MD, USA
Offline Offline
God Member
*****
Karma: 2
Posts: 663
A jack of all trades and a master of none!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, OK !



Code:
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);


}

Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

// 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();
    }
« Last Edit: January 18, 2010, 05:48:40 am by GrooveFlotilla » Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry it is a mistake, it would be an IF
Logged

Pages: [1]   Go Up
Jump to: