Image from Serial
in
Integration and Hardware
•
1 years ago
Hi, this is my first post and also my first processing experience.
I'm an enginnering student and have much experience using arduino, now im trying to get images from a serial camera trough the arduino.
To test Ive made this simple sketch, the arduino program waits for an 'a' in the serial and then responds with 1000 bytes of random data. The background change is to see if there is data coming and works ok, but no pixel change.
is the code ok?
Sorry for my bad english.
- import processing.serial.*;
- Serial myPort;
- char valr;
- char valg;
- char valb;
- int indx=0;
- PImage img;
- void setup() {
- size(500, 500,P2D);
- background(0);
- String portName = Serial.list()[1];
- myPort = new Serial(this, portName, 115200);
- img = createImage(400, 400, RGB);
- image(img, 0, 0);
- }
- void draw() {
- byte[] inBuffer = new byte[1500];
- img.loadPixels();
- int ix=0;
- /* while ( myPort.available() > 0) { // If data is available,
- myPort.readBytes(inBuffer);
- if (inBuffer != null) {
- background(inBuffer[50]);
- for (int ix=0 ; ix<1000;ix++){
- img.pixels[ix] = inBuffer[ix];
- img.updatePixels();
- }
- }
- }*/
- if (myPort.available() > 0){
- ix++;
- img.pixels[ix] = myPort.read();
- background(img.pixels[ix]);
- img.updatePixels();
- }
- image(img, 0, 0);
- }
- void keyPressed() {
- if (key == 'a') {
- myPort.write('a');
- }else{
- myPort.write('d');
- }
- }
1