Error in the heatmap code

Hello!..i have been a developing a code where I'm reading the analog values from the sensor and reading the analog values in the heat map matrix ( 4* 4) in processing IDE. I have read the values successfully from the Arduino and filled the analog values into the heat map but then I cannot read proper values into the matrix(processing IDE)..im getting 0 in the reading portal and the values are generated in between the 0's. how to overcome the error and print the analog values and fill the heat map matrix.??.

Here's my Arduino code:

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

Arduino result:

458
457
463

here's my processing IDE code:

import processing.serial.*;
int box_size = 50;
Serial mySerial; 
int val;
String myString = null;
int nl = 10;
float myVal;
int a[][]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};


void setup()
{
size(200,200);
colorMode(HSB,240,100,100);
background(100);
String portName = "COM29";
mySerial = new Serial(this, portName, 9600);
printArray(Serial.list());
}

void draw()
{
  fill(50);
  stroke(100);
  strokeWeight(2);
  
 
 
   while(mySerial.available()>0){
    myString = mySerial.readStringUntil(nl);
    if(myString!=null){
      background(0);
      myVal= float (myString);
      myVal = myVal;
      println(myVal);


      
    for(int x=0; x<4; x++)
     {
       for(int y=0; y<4; y++)
       {
         
         //a[x][y]=readval();
         println(a[x][y]);
         fill(a[x][y],100,100);
         rect( x * box_size, y * box_size, box_size, box_size);
         
       }
     }

  }
  delay(0);
}
}
//int readval()
//{ 
//  int value =0;
//  if ( mySerial.available() > 0) 
//  { 
//  value =  mySerial.read();
//  }
//  return value;
//}

Processing IDE result :

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
493
0
0
0
0
0
0

how to read the values continuously without any 0's??.
I need this help badly.. Thank u.

I guess that the unwanted 16 zeros are being printed here since array A is zero filled. What does function fill() do, incidentally?

the fill function will fill the desired value in the (4*4) heatmap matrix and it reflects the desired color according to HSB (Hue, Saturation, Brightness) (mentioned in the above codes of line) according to the analog value.

if I remove those zeros I'm not getting proper output. If I leave blank there is no output visible.

Is this printing the correct value? In other words, does myVal contain the result which you want to put into all elements of the 4x4 array A ?
At the moment, array A has only zeros in it.

yes u r right...It will print the exact values from Arduino analog read and put into the elements (4 *4 )..I checked this line statement in separate code.

Then, if if have understood correctly what you are attempting, you add the line marked "<<<<<" below into your code.

for(int x=0; x<4; x++)
     {
       for(int y=0; y<4; y++)
       {
         
         //a[x][y]=readval();

         a[x][y] = myVal ;  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

         println(a[x][y]);
         fill(a[x][y],100,100);
         rect( x * box_size, y * box_size, box_size, box_size);
         
       }
     }

it says it cannot be converted from float into int.

Try this line instead.

a[x][y] = myString.toInt( )  ;  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

If that fails, show the exact error message and the exact previous error message you got where you said "it cannot be converted from float into int."


This is the error.

This is the next error

OK. I see. Processing seems then to need an explicit conversion. Try this:

a[x][y] = int( myVal ) ; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

I guess we came almost..it worked but then same as 0 it is printing the same values again and again

I'm attaching the screenshot

notice he values they are getting repeated for 16 times..as mentioned in the array as 0 (16 times)

looking like this...values are repeating as per array declaration...

Well, how do you want the data, which comes from the Arduino, distributed in the array A ?
At the moment, each item of data is copied into all 16 array elements.

Let us suppose you get the following data from the Arduino:

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

How should that data appear in the array ?
Does 191 go into the first cell, 192 go into the next (horizontally adjacent) cell etc. Or what ?

i want my array value to go horizontal..

horizontal .. in matrix it should be running in an loop continuous that's why for loop is given..

I'm sorry but your description is not enough to help.
You appear to be gettimg a large stream of analog values which come from an arduino. It looks like it could be delivering hundreds of values per second. You are reading these values in your processing code and appear to be putting these into an array. At the moment, you copy every data item into all 16 positions in the array.
You have mentioned that values are repeated but that is because, for each data item, you print it 16 times.
You have to give a more concrete description of what you are attempting to do.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.