how to send float from processing to arduino?

edited December 2013 in Arduino

hello! I'm new to programming, so I'm having problems with understanding how serial works. My goal is to control a robot from Processing via serial to Arduino. Problem appears when I try to send FLOAT to arduino. This is the part of my code that I need help with. How do I send value X ? And I know that this could probably be shorter and better written, but that's not the point here.

thanks for help! :)

float speed;
float x = 200;
color fillE= 255;

    void slider_Servo()
    {
      background(0);
      fill(fillE);
      x = constrain(x, 20, 380);
      smooth();
      ellipse(x,50,25,25);
      if(key == CODED)
      {

        if(keyCode == RIGHT && keyPressed == true)
        {
            x=x+speed;

            fillE= color(0,0,255);
            println(x);
            myPort.write(x);


        }
        else
        {
            x=x;
            fillE=255;
            println(x);
            myPort.write(x);

        }
        if(keyCode == LEFT && keyPressed == true)
        {
          x = x-speed;
          fillE=color(0,0,255);
          println(x);
          myPort.write(x);
        }
        else
        {
          x=x;
          fillE=255;
          println(x);
          myPort.write(x);
        }
        if(keyCode == DOWN)
        {
          if(x > 200)
          {
            x = x-4;
            println(x);
            myPort.write(x);
          }
          if(x < 200)
          {
            x = x+4;
            println(x);
            myPort.write(x);
          }
        }
      }

    }

Answers

  • oh, and speed value is controled by another slider. just to make things clear.

  • You need to have a serial connection opened to your arduino and you need to send the value with with write http://arduino.cc/en/Serial/write. And i think that you can't send float to arduino. Here is a processing sketch for rgb led control it might help.

    import java.awt.Robot; //java library that lets us take screenshots
    import java.awt.AWTException;
    import java.awt.event.InputEvent;
    import java.awt.image.BufferedImage;
    import java.awt.Rectangle;
    import java.awt.Dimension;
    import processing.serial.*; //library for serial communication
    
    Serial arduino; //creates object "arduino" of serial class
    Robot robby; //creates object "robby" of robot class
    
    void setup() {
      println(Serial.list());
      // Usually works with [0], change accordingly
      arduino = new Serial(this, Serial.list()[0], 9600);
    
      //window size (doesn't matter)
      size(100, 100);
    
      //standard Robot class error check
      try {
        robby = new Robot();
      } catch (AWTException e) {
        println("Robot class not supported by your system!");
        exit();
      }
    }
    
    void draw() {
      //ARGB variable with 32 int bytes where
      int pixel; 
      //sets of 8 bytes are: Alpha, Red, Green, Blue
      float r = 0;
      float g = 0;
      float b = 0;
    
      int x = displayWidth; //possibly displayWidth
      int y =  displayHeight; //possible displayHeight instead
    
      //get screenshot into object "screenshot" of class BufferedImage
      BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(x,y)));
      //returns the native monitor resolution
    
      //Skiping every alternate pixel makes the program 4 times faster
      int skipValue = 1;
      int i = 0;
      int j = 0;
    
      for(i = 0 ; i < x; i = i + skipValue){
        for(j = 0; j < y; j = j + skipValue){
          pixel = screenshot.getRGB(i, j); //the ARGB integer has the colors of pixel (i,j)
          r = r + (int)(255 & (pixel>>16)); //add up reds
          g = g + (int)(255 & (pixel>>8)); //add up greens
          b = b + (int)(255 & (pixel)); //add up blues
        }
      }
    
      //get the modified resolution for faster performance
      int aX = x/skipValue;
      int aY = y/skipValue;
      r = r / (aX * aY); //average red
      g = g / (aX * aY); //average green
      b = b / (aX * aY); //average blue
    
      //println(r+","+g+","+b);
    
      //filter values to increase saturation
      float maxColorInt;
      float minColorInt;
    
      maxColorInt = max(r, g, b);
      if(maxColorInt == r){
        // red
        if(maxColorInt < (225)){
          r = maxColorInt + 30;  
        }
      } 
      else if (maxColorInt == g){
        //green
        if(maxColorInt < (225)){
          g = maxColorInt + 30;  
        }
      } else {
         //blue
         if(maxColorInt < (225)){
          b = maxColorInt + 30;  
        }  
      }
    
      //minimise smallest
      minColorInt = min(r, g, b);
      if(minColorInt == r){
        // red
        if(minColorInt > 20){
          r = minColorInt - 20;  
        }
      }
      else if (minColorInt == g){
        //green
        if(minColorInt > 20){
          g = minColorInt - 20;  
        }
      }
      else {
         //blue
         if(minColorInt > 20){
          b = minColorInt - 20;  
        }
      }
    
      //Send final value to Arduino
      arduino.write(0xff); //write marker (0xff) for synchronization
      arduino.write((byte)(r)); //write red value
      arduino.write((byte)(g)); //write green value
      arduino.write((byte)(b)); //write blue value
      delay(10); //delay for safety
    
      background(r, g, b); //make window background average color
    }
    
  • So how could I send value of slider to arduino? is there even a way?

  • edited December 2013

    You can always send the value from Processing as a string and then on the Arduino end ready the bytes and convert the string to a float using

    float f = strtof (data);

    another variant would be

    myFloatVariable = (float)strtod(myStringReadFromSerial);

Sign In or Register to comment.