Sending serial data: help for a newbie:
in
Programming Questions
•
8 months ago
The code is supposed to reference an image, return a greyscale value for a pixel (in this case 5 pixels in and then return the greyscale for the 15th pixel, then 25th, etc)
I havent yet bothered to build the "go to next line" or a "next pixel please" part into my arduino. I am just trying to get it to provide a series of numbers at present, then i will build on it.
the image (grad.jpg) is currently just a gradation from black to white, not that that matters i suppose
I think the first one (number), gets through (the arduino makes a suitable movement) but then it stops.
i do not get the value for the shade printed on the black box at the base of the processing window, as requested in line 34.
can anyone please tell me how to i make lines 30, through to 35 loop until the end of the image is reached?
I presume I cant just stick them in a loop, as they are already in a serialEvent void?
I havent yet bothered to build the "go to next line" or a "next pixel please" part into my arduino. I am just trying to get it to provide a series of numbers at present, then i will build on it.
the image (grad.jpg) is currently just a gradation from black to white, not that that matters i suppose
I think the first one (number), gets through (the arduino makes a suitable movement) but then it stops.
i do not get the value for the shade printed on the black box at the base of the processing window, as requested in line 34.
can anyone please tell me how to i make lines 30, through to 35 loop until the end of the image is reached?
I presume I cant just stick them in a loop, as they are already in a serialEvent void?
- import processing.serial.* ;
- Serial myPort;
- boolean firstContact = false;
- PImage img1;
- int cellSize = 10; // size of cells into which the image is split, larger size = less cells
- int squareSize = 300; // size of the image square (double horizontally to allow for processed image)
- int x = 0;
- int y = 0;
- void setup() {
- println(Serial.list());
- String portName = Serial.list()[0];
- myPort = new Serial(this, portName, 9600);
- myPort.bufferUntil('A');// waiting on response from arduino
- size(squareSize, squareSize); // frame is 500 x 500
- img1 = loadImage("grad.jpg"); // image, 300 x 300
- imageMode(CENTER);
- }
- void draw() {
- image(img1, 150, 150); // draws the image
- }
- void serialEvent(Serial myPort) {
- int inByte = myPort.read();
- if (firstContact == false) {
- if (inByte == 'A') {
- myPort.clear(); // clear the serial port buffer
- firstContact = true; // you've had first contact from the microcontroller
- myPort.write('A'); // ask for more
- }
- else {
- for (int x = 0; x < squareSize; x = x + cellSize ) {
- for (int y = 0; y < squareSize; y = y + cellSize) {
- color c = get(x + cellSize/2, y + cellSize/2);
- println(c);
- myPort.write(c);
- }
- }
- }
- }
- }
1