String not converted Properly from int

post is deleted…

now i did not test that with a arduino…
but the code runs here without device.

i started from the loadDisplayImage example
show a shrunk image with 50*50
make a Black / White copy of it
print to console and send to serial

/**
 * Load and Display 
 * 
 * Images can be loaded and displayed to the screen at their actual size
 * or any other size. 
 */
import processing.serial.*;

PImage img;
int ledW =50, ledH=50, s, point;
color ccompare, cimg, cset;
float fcompare, fimg;
Serial myPort;

void setup() {
  size(640, 360);
  colorMode(RGB, 100);
  img = loadImage("moonwalk.jpg");  // Load the image from the data folder into the program
  image(img,0,0);
  image(img, 0, 0, ledW, ledH);
  noLoop();
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  send_pix();
}

void send_pix() {
  loadPixels();                       // canvas
  ccompare = color(50, 50, 50);    // black white limit ( 50 of 100 )
  fcompare = brightness(ccompare);
  for (int y=0; y < ledH; y++) {
    println();
    print("y "+y+"_");
    for (int x =0; x < ledW; x++) {
      point = x+y*width;
      s = 0;
      cset=color(0);
      cimg = pixels[point];
      fimg = brightness(cimg);      
      if (fimg > fcompare) { 
        s = 1;
        cset=color(100);
      }
      pixels[x+ledW+y*width]=cset;         // make a B/W copy right of mini image
      print(s);
      myPort.write(s);
      delay(2);
    }
    myPort.write('\n');                    // try a line feed after sending ledW pix 1/0 ( to be understood in arduino )
  }
  updatePixels();                          // show the B/W copy
}

SNAG-0132

post is deleted…

what is string in shape???
depending on the arduino code i recommend using lines,
so a line of pixels gives a line of string to arduino,
but sure you can overlay your own pseudo protocol
start lines with
y=xx;0101010
( like i use at the console print )
and split on that “;” … in arduino
so you sure the data go to what ?led strip?

please understand what i am trying to do
i have a 100 led strip connecting through arduino .
through processing i want to create a binary image of png extension. where 1 for black and 0 for other.
binary image is equal to orignal image and this image send by row wise to arduino.
row wise as a string beacuse in my arduino code i use function readString not readByte. ok
but problem is occured is that
binary image which pixels is in int form like i = x+y*width.
i have a value of pixel 0 to 10000.
and i want to convert each pixel value in 0 and 1 form according to png image and this 0 and 1 value should be in string form and then this string read row wise data and send to the arduino.
i am not using int value of 0 and 1 because when i send data to arduino 8 bit is send at one time but i want to send 100 bit as a row one time.
thats all i want to try.
now i hope u can understand what i am trying to do.
so please help me yaar to get out of this problem .

1 Like

I don’t have 100 LEDs but got a few I will give this a go !

1 Like

Your code needs some work.

String s = str(i); is only one character so q[] is only a one element array; it still prints and outputs but there is a lot of unnecessary code.

I have this working…

You really need to simply the code and make sure output to console and file is what you expect before doing a myPort.write();

Here is a simple ping file to work with:

Simple

int i= x+y*width; is not correct, width in this case is width of screen and not the image.

:slight_smile:

1 Like

An example that draws to screen.
I removed all the console prints and outputs to a file and leaving this as an exercise. :slight_smile:

PImage img;
String s;
int j;
char p;

void setup()
  {
  size(400, 400);
  img = loadImage("Simple.png");
  img.loadPixels();

  println(img.width, img.height);
  println(img.pixels.length);
  
  textSize(18);
  }

void draw()
  {
  background(0);
  function();
  image(img, 0, 0);
  noLoop();
  }

void function()
  {
  for (int y=0; y<img.height; y++)
    {
    for (int x=0; x<img.width; x++)
      {
      int i = x + y*img.width;
      println(i);
      if (img.pixels[i] == color(0, 0, 0))
        p = 'X';
      else 
        p = ' ';    
      text(p, img.width + 10 + x*15, y*15 + 60);
      }
    }  
  }

-a- this sounds very much like the code i did in the above image example.
did you test and understand my code?
-b- the reduction from img.width,img.height to ledW,ledH you can adjust accordingly there
-c- the code used for serial is STRING ( ASCII ), so you not send a bit for a pixel,
a real text “0” or “1” and has to be treated in arduino like that
yes it is possible to send binary, but i think that’s some years too early for you,
pls stay with readable text what can also be tested from the arduino IDE monitor…

1 Like

my above code is in running condition with processing and arduino. processing send 0,1 to arduino and led’s are blinking but in wrong order.
this kind of output i get into arduino.


my String continues print from 0 to 10000 it characters.

i want only limit it at 100 characters print at a time and form image correctly when i get ouput in notepad

ok thats great. but will u please tell me how this text value i can store as a string and how to take output as a .txt file thats will be very helpfull for me .

ok i will wait .
share your ans. as soon as possible.

This is an example of how to create a string with random numbers.

String s;
char p;

void setup()
  {
  size(475, 475); 
  fill(0);
  textSize(18);
  }

void draw()
  {
  background(255);
  s = function();
  print(s);
  println();
  println();

  for (int i=0; i<25*25; i++)
    {
    print (s.charAt(i));
    if ((i+1)%25 == 0) println();
    }
  noLoop();
  }

String function()
  {  
  String s1 = "";
  for (int y=0; y<25; y++)
    {
    for (int x=0; x<25; x++)
      {
      int i = x + y*25;
//      println(i);
      if (int(random(0, 2)) == 0)
        p = 'X';
      else 
        p = ' ';    
      s1 = s1 + p;
      text(p, 25 + 10 + x*15, y*15 + 60);
      }
    }  
  return s1;
  }

You should be able to write code to send this out the serial port and write to a text file with some effort.
I will leave the rest up to you.

Consider adding extra characters to string and check for those so you know when string starts and\or ends.

:slight_smile:

1 Like

can u update my above code.
i am trying your code but still it has same problem occur.

i am only want that
grid of string img.width and height without using \n or \r function . not in console window and not any window.
if i get this grid i can easily transfer each img.width as a row to arduino.

I will not update your code.

We have all shared working examples from which you can glean some insight on what you can do.
The rest is up to you.

https://processing.org/reference/libraries/serial/index.html
https://processing.org/reference/libraries/serial/Serial_write_.html

Try doing a serial.write(src) where src is a String.

:slight_smile:

1 Like

yes you are right . all rest is up to me.
but i am saying my above code is in running condition and string is not getting shape of image when i send my code to arduino this type of string is sending and 100 serial led are blinking. .

i only want to check my code at least one time. and if you can please set in proper manner.
i am trying all my efforts and all your efforts are very nice. but only last time please check my above code in your processing ide.
i am not wanted to print image in console i wanted which image is printing in console that should be built in my code and send row by row to arduino.
please yaar help me .