How ti invert grayscale (0-255) in this code ?

edited July 2016 in Arduino

Hello everyone ! I have a LIDAR lite v2 and i used it to measure distances , i mount him on 2 servo motors wich rotates in Y and X and lidar scanning distance in that time. Now i have this program which communicate with arduino and creating point cloud from degrees of X servo degrees of Y servo and distances in cm .

Now the question is why a get small picture whatever i put in size() i get the same small picture and with this program DARKER the pixel is closer the object is. AND NOW the main question is how to invert grayscale , cause i want darker pixel farther.

Sorry for so many words and thank u.

This is the code in processing.

import processing.serial.*;

float pcolor = 0; // point color value
float xVal = 0;  // x value
float yVal = 0;  // y value

Serial myPort;

void setup() {

  size(400,200);

  printArray(Serial.list()); 

  myPort = new Serial(this, Serial.list()[0], 57600);

  myPort.bufferUntil('\n');   
  background(0);
}

void draw() {
  stroke(pcolor);    
  point(xVal,yVal);
}

void serialEvent(Serial myPort) {

  String inString = myPort.readStringUntil('\n');  

  if (inString !=null) {
    inString = trim(inString);    

    float[] colors = float(split(inString, ","));    

    if (colors.length >=3) {       
      xVal = colors[0];          
      yVal = colors[1];
      pcolor = map(colors[2], 0, 400, 0,255);
    }
  }
}

Answers

  • Answer ✓

    in line 38 you map input values 0-400 to output values 0-255

    to invert that just swap the last two numbers so it maps 0 to 255 and 400 to 0

    pcolor = map(colors[2], 0, 400, 255, 0);
    
  • Just that and it is inverted ? :D so just replace 255-0 and done. Thank u very much ! I will try now ;) And one more question .. why my picture looks little and depending on deggres not on size() function in processing , cause i try with bigger size() and it is still small reading. Thanks again a lot ! :)

  • degrees of X servo degrees of Y servo

    point(xVal,yVal);

    looks like you need to scale / map the values here too. what range of values come back? if the return values are fractional then you might get many values mapped onto one pixel.

  • I know that piece of code is for that and whatever size i put the picture is always the same size , cause of that... And i dont know how to scale that now to size of whole window ... It is range of 0 to 180 degrees max for both servo , distance is from 0 to for example 500 cm... .Hmm I dont know am i understood u :) .. but i am new and dont know to solve that problem with size.

    But i dont think it mapping many values on one pixel and dont know how to chek that ? it receives date from arduino like this :

    30,130,400 40,140,345...

    For example... First are x degrees second y and third are distance ..

  • that looks ok. if it had come back

    20.1,130.0,400
    20.2,130.0,500

    then you'd have problems.

    you could map(xVal, 0, 180, 0, width); but then you can't use point() - you need to draw a rect() for each input pixel. soemthing like

    // global variables
    float w = width / 180.0;
    float h = height / 180.0;
    
    // in draw()
    rect(map(xVal, 0, 180, 0, width), map(yVal, 0, 180, 0, height), w, h);
    
  • Thank u on help, yes that is true but i dont think tthat will come back. Oh i will try that code and hope to work ;) Yea now i just replace point with that rect ? ok i will try . Thank u again u helped me so much ;)

  • unfortunately that code u give me is working but the problem is same still small picture one one part of the window :( ... Hmm I dont know what to do .. maybe it is good enough like that.. Thank u very much ;)

Sign In or Register to comment.