# String not converted Properly from int

**URL:** https://discourse.processing.org/t/string-not-converted-properly-from-int/12295
**Category:** Electronics (Arduino, etc.)
**Created:** [June 26, 2019, 5:54am UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295 "2019-06-26T05:54:27Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![The\_nish](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the_nish/32/5634_2.png) [@The\_nish](https://discourse.processing.org/u/The_nish)
#### Post date: [June 26, 2019, 5:54am UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/1 "2019-06-26T05:54:27Z")

</div>

post is deleted…

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [June 26, 2019, 10:22am UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/2 "2019-06-26T10:22:18Z")

</div>

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

```auto
/**
 * 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-0131](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f283f1bdb790321e63bc3c0336ed7c2119eca242.jpeg)

![SNAG-0132](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6e2da85edbe64abf9512c8f05779f3681137e1d0.jpeg)

---

<div class="post-metadata">

### Author: ![The\_nish](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the_nish/32/5634_2.png) [@The\_nish](https://discourse.processing.org/u/The_nish)
#### Post date: [June 26, 2019, 11:13am UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/3 "2019-06-26T11:13:28Z")

</div>

post is deleted…

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [June 26, 2019, 12:07pm UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/4 "2019-06-26T12:07:52Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![The\_nish](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the_nish/32/5634_2.png) [@The\_nish](https://discourse.processing.org/u/The_nish)
#### Post date: [June 26, 2019, 12:54pm UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/5 "2019-06-26T12:54:24Z")

</div>

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 .

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [June 26, 2019, 7:03pm UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/6 "2019-06-26T19:03:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 26, 2019, 7:51pm UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/7 "2019-06-26T19:51:37Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/70a846150cc31400c99705a88a09379bf2281ba8.png)

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

🙂

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 26, 2019, 8:07pm UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/8 "2019-06-26T20:07:24Z")

</div>

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

```auto
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);
      }
    }  
  }

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [June 27, 2019, 1:22am UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/9 "2019-06-27T01:22:17Z")

</div>

-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…

---

<div class="post-metadata">

### Author: ![The\_nish](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the_nish/32/5634_2.png) [@The\_nish](https://discourse.processing.org/u/The_nish)
#### Post date: [June 27, 2019, 6:28am UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/10 "2019-06-27T06:28:13Z")

</div>

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.

 ![upload](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f859ae85eeec3f55b8f304c72abbe79e64a92947.png)  
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

---

<div class="post-metadata">

### Author: ![The\_nish](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the_nish/32/5634_2.png) [@The\_nish](https://discourse.processing.org/u/The_nish)
#### Post date: [June 27, 2019, 7:04am UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/11 "2019-06-27T07:04:04Z")

</div>

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 .

---

<div class="post-metadata">

### Author: ![The\_nish](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the_nish/32/5634_2.png) [@The\_nish](https://discourse.processing.org/u/The_nish)
#### Post date: [June 27, 2019, 7:26am UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/12 "2019-06-27T07:26:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 27, 2019, 12:00pm UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/13 "2019-06-27T12:00:59Z")

</div>

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

```auto
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.

🙂

---

<div class="post-metadata">

### Author: ![The\_nish](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the_nish/32/5634_2.png) [@The\_nish](https://discourse.processing.org/u/The_nish)
#### Post date: [June 27, 2019, 12:40pm UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/14 "2019-06-27T12:40:36Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 27, 2019, 5:16pm UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/15 "2019-06-27T17:16:50Z")

</div>

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/index.html)  
[https://processing.org/reference/libraries/serial/Serial\_write\_.html](https://processing.org/reference/libraries/serial/Serial_write_.html)

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

🙂

---

<div class="post-metadata">

### Author: ![The\_nish](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the_nish/32/5634_2.png) [@The\_nish](https://discourse.processing.org/u/The_nish)
#### Post date: [June 28, 2019, 5:25am UTC](https://discourse.processing.org/t/string-not-converted-properly-from-int/12295/16 "2019-06-28T05:25:54Z")

</div>

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

 ![upload](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f859ae85eeec3f55b8f304c72abbe79e64a92947.png)

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 .
