Convert image in to blinking LED on processing

@noel my mission …
i connect a led in to my arduino board.i need to load a grayscale image and convert it into byte array.because i need binary values of each pixel of my whole image.then i need to blink my led which has connected to arduino ,for its all binary values.

eg:1 st pixel value is 120 ,then send its binary value is 01111000.then led blink serialy 0 1 1 1 1 0 0 0 (low high high high high low low low)

after that as an example 2 nd
pixel value 34 ,then send its binary value is 00100010.then led blink again 0 0 1 0 0 0 1 0 (low low high low low low high low) .

i want to blink all the pixel values through my led sequentialy.

please guide me to do this.what is the way to do this.i know you all good in programing ,i need some help from you

For your project to succeed you have to have o good electronic set up as well.
Forget about using a LDR, because it has a speed response of about 50 ms. You would have to slow down your reading speed. And even for a very small image of 100 by 100 mono pixels (and forgetting about handshaking, stop, and parity bits), it would take more then three hours transfer time.
Then, to avoid outside light interference you need a strong source, a laser diode, or use an optical cable.
But for just a test, you may use a mono led or IR led as transmitter and a photo diode as receiver, one centimeter separated, and do the test in a dark ambient.
As for the “binary stream”, you don’t actually have to worry about this. The Serial lib will take care of this for you. Yes, a byte in Arduino has a value of 0 to 255, the values you have in the text file. (Unlike Processing where a byte has the value of -128 to 127). But you can also use a char.
In the test set up you connect a 330 ohm resistor from the digital port 2 to the cathode of the emitter led (shorter leg), and the anode you connect to ground. Then you load following code:

#include <SoftwareSerial.h>

#define rx 3  
#define tx 2  

SoftwareSerial mySerial =  SoftwareSerial(rx, tx);

void setup()  {
     mySerial.begin(9600);
}

void loop() { 
    mySerial.println("0");
    delay(1000);
    mySerial.println("150");
    delay(1000);
    mySerial.println("255");  
    delay(1000);
}

Then on the other Arduino you connect the cathode of the receiver with a 1k ohm resistor to the ground, and also a wire from the same cathode to digital pin 2. The anode of this photo diode you connect with a 220 ohm resistor to the 5V. Then you load the following code.

#include <SoftwareSerial.h>
#define rx 2
#define tx 3

SoftwareSerial mySerial =  SoftwareSerial(rx, tx, false);

void setup()  {
   mySerial.begin(9600);
   Serial.begin(9600);
   Serial.println("Receiving...");
}

void loop() {
  if (mySerial.available())  {
      Serial.print((char)mySerial.read());
  }
}

The receiver arduino you leave connected to the PC, where you open the serial monitor of the Arduino ide, (be sure to choose same baudrate), and also select the right com port.
I hope you already learned how to get the data from the text file on the SD card with the lib I linked above.
Have fun in the dark!

2 Likes

Are you using a single LED?

A minimal example in processing to display bits (LSB to MSB) in a byte and change background:

//https://discourse.processing.org/t/convert-image-in-to-blinking-led-on-processing/15022/22

// I had to work with integers in JAVA but only used lower byte (first 8 bits):
//https://docs.oracle.com/javase/specs/jls/se10/html/jls-5.html#jls-5.6.2
//https://docs.oracle.com/javase/specs/jls/se10/html/jls-15.html#jls-15.22.1

int led = 0;
int mask = 1;
int data = 0x55; // 0x55 hex is 85 decimal is 01010101 binary
int result;
int i;

void setup()
  {
  size(200, 200);
  
  for(int i=0; i<8; i++)
    {
    result = mask & data;
    if (result > 0)
     led = 1;  
    else
     led = 0; 
    //println(mask, result, led); 
    print(led);
    mask = mask << 1; 
    }  
  
  i = 0;
  mask = 1;
  println();
  }

void draw()
  {
  if (frameCount%60 == 0)
    {
    result = mask & data;
    if (result > 0)
     {
     led = 1; 
     background(0);
     }
    else
     {
     led = 0; 
     background(255, 0, 0); 
     } 
    print(led);
    mask = mask << 1; 
    i++; 
    }
   
  if (i>=8) 
    {
    i = 0;    
    mask = 1;
    println();
    }
  }

https://processing.org/reference/leftshift.html

:slight_smile:

2 Likes

hi @noel
can i use your code for image transmission which i explained??
“ … 1 st pixel value is 120 ,then send its binary value is 01111000.then led blink serialy 0 1 1 1 1 0 0 0 (low high high high high low low low)

after that as an example 2 nd
pixel value 34 ,then send its binary value is 00100010.then led blink again 0 0 1 0 0 0 1 0 (low low high low low low high low) .

i want to blink all the pixel values through my led sequentialy. “

hi @glv
can i send my all pixel value s byte value into blinking led ,which i explained latest post ,using your code??

“… 1 st pixel value is 120 ,then send its binary value is 01111000.then led blink serialy 0 1 1 1 1 0 0 0 (low high high high high low low low)

after that as an example 2 nd
pixel value 34 ,then send its binary value is 00100010.then led blink again 0 0 1 0 0 0 1 0 (low low high low low low high low) .

i want to blink all the pixel values through my led sequentialy. “

Yes. Exactly that. In the code I wrote above three values in a loop are send as a binary stream to the other Arduino throughout a light bundle. You can now modify the loop and access the values from the SD card, and send them in a sequence. When all data are send, you complete printing for instance: println(“end”); On the other Arduino you can capture that in an "if " block and remount the image and, (you didn’t specify what you are wanting to do but I suppose) send it to a Led matrix.

1 Like

hi @noel noel can i send pixel values sequence using processing? can pixel values convert into binary stream using processing?

Yes. Please read this link carefully.

Also please tell me if you did the experiment for which I explained the circuit and gave you the code, and if you saw the values appearing over and over in the serial monitor of the Arduino ide.

Hello,

I stated what my code does:

Do you understand what it is doing?

You can slow things down and use an LED (transmitter) and LDR (receiver) if the hardware is set up correctly and it is coded correctly. I used my Processing display in place of an LED.

This is what I did:

  1. Created an image (16*16 pixels) that contained colors with brightness from 0 to 255.
    It is easier to work with a known image for testing and debugging.
    Saved as a BMP file so I could easily see data in file with a hex editor.
    https://processing.org/reference/createImage_.html
  2. Loaded image that I created:
    https://processing.org/reference/loadImage_.html
  3. Read the pixel data from the image I created and saved it to a data file.
    https://processing.org/reference/saveBytes_.html
  4. Read the data file into an array.
    https://processing.org/reference/loadBytes_.html
  5. Read the data one byte at a time from array and displayed the bits one bit at a time to console from LSB to MSB and to sketch window (changed the background).
    I integrated the example I posted to do this.
  6. Use Arduino to read the screen with an LDR (placed near sketch display) and turn an LED ON and OFF.
    Analog read was used initially:
    https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/

Other references:
https://processing.org/reference/PImage.html
I made us of the Processing examples that are available to you; seek and you shall find.

I am sharing as much code with you as you have shared with us:

// Blank

What progress have you made on this project?

:slight_smile:

There is more than one way to do this and I found this interesting enough that I chose to explore it.
I learned a lot from this exercise.

I may experiment later to see how fast I can Tx and Rx data!

Green would have been a better choice of color:

The common CdS (cadmium sulfide) LDRs have peak spectral response at about 550 nm which is the green region of the visible spectrum.

:slight_smile:

I use Notepad++ as my editor and the Hex-editor plugin:
https://notepad-plus-plus.org/
This is the data file created:

3 Likes

@noel
Thanks for helping me again dear my friend…
Are you sure about your code s are transmitting pixel values in to blinking one LED by convert into binary.In your code there are no any image encoding loop or something there.your code that mention there is very simple .please can you explain what is that simple code there.
:thinking:

Hello,

You can be the clever guy!

We cannot do your academic project for you.
We can guide you.

You have multiple posts on this subject.
This post is 24 days old:


Another post:

What progress have you made?

:slight_smile:

2 Likes

Yes I am sure. But you could have responded to @glv because he made a very nice approach where you can see the actual blinking. My code is too fast.It’s somehow difficult to simulate a led blinking with serial byte transfer. Because if you consider a byte like IIIIIIII, you would have a eight bits LED ON time, while a 00000000 value a eight bits time OFF
And with a 01010101 byte it would blink four times . So just to take an average you could have 2 blinks per byte.
The minimum baud rate of the arduino is 300bps But a baud rate is different from the bitrade as it determines how often a channel can change it’s state.
Bit rate = baud rate x the number of bit per baud.
Just to have a rough idea I wrote a code to see until what frameRate I still could see the blinking. For me it’s about 50 That means a 25 times per second blinking of the led. Meaning that my code above does the transfer correctly, but you will not be able to see the blinking. The baud rate is still way to high!
Below the code, but has some interference. Maybe someone can come up with something more accurate.

boolean change;
int teller;

void setup () {
  size(100, 100);
  fill(0);
  frameRate(40);
  ellipseMode(CENTER);
} 

void draw () {
  ellipse(width/2, height/2, 30, 30);
  if (change) {
    fill(0);
  } else {
    fill(0, 255, 0);
  }
  change =! change;
  if (teller == 10) {
    println(frameRate); 
    teller = 0;
  }
  teller++;
}
2 Likes

@noel
I tried with the code. As it seems, the receiving part is having some issues as it is not getting out of Serial.read function because it doesn’t have an end to read. Am ai correct? Please reply mee

The first thing you have to verify , is to see if the text “Receiving…”. appears in the serial monitor of the ide. If not, verify in the dialog box on the bottom if the baud rate is set to 9600 Every time you reset the receiver arduino, this message must appear. This has nothing to do with the sending arduino, because it uses another own serial object. When it does write the message, and everything is installed as I described, with a photo diode (not a LDR), the code will receive characters in the loop forming exactly te values that are send by the other Arduino.
0
150
255 over and over
The best way is to solder the wires because older plugs sometimes have a bad contact. Below as you see I made an image for the setup. And the type of leds to use. Do you see the led from the sender blink for a short time every second?. This part has to work first for you to continue. You can also test aproximating the leds, and it must be in a dark ambient to avoid noise. Even the monitor can give interference. When you get this working I can give you details about how to have it work for a larger distance.

Edit: While it is just a test, you can even connect the leds together with black isolating tape to avoid interference
1

2

3 Likes

@noel
hi…tell me why the recieving side arduino ide print “ recieving …” only.i used ldr.If i use a photo transister , the ide prints 0,150,255 ???need all the resisters u mention to the set up if i have to print 0,150,255 or anything I input
Reply my dear friend!!! @noel

If it prints “Receiving…”, that means that the Serial object is working, the baud rate is set up correctly, and the arduino/pc driver works. A serial object is from a class that does the individual settings of the bytes transfer for you, behind the scenes.If you want to see how that works you have to build @glv 's example.
The code has two different serial objects. One called Serial, that is the common one, for communication between pc and arduino. The other is from another lib. The imported Software Serial lib , and called mySerial, for communication between the two arduinos. If you see the sender led blinking, it is working there.

For @glv 's example you can use an LDR,because the bit transfer is slower. At higher speed you need a photo diode.

Of course you need. And with the (aproximating ) values. You also need to test the components with the help of a multimeter. If you connect a led reversed for instance, it can be damaged. You need to learn how to test them.

2 Likes

@noel
hi.thank you very much.
Let me know my friend!! If i use your sending and recieving codes without any changes and with your hardware set up you mention ,can i send any image or number of words directly ?? and recieve from the other side as original data…???my dear friend @noel

Yes. But first you have to buy the components (more then one each) and do the setup. When you have it working, the data you obtained from the image with the code somewhere above, and saved on the SD card on first arduino, can be send to the second arduino connected to the computer where you can display the image on the screen.

2 Likes

@noel
thanks .ooh…before i send the data (image or text) ,should i save on the SD card according to your code ?

Yes, that is how it all started not?