Processing (image display) via Arduino ultrasonic sensor

edited June 15 in Arduino

Hi, I am just starting to explore posibilities to make something work in processing via arduino (sensors). On school exhibition I want to make a picture display that shows one image at the time on the wall (with a projector), depending how close or far person is located from distance sensor. This is a sketch of what I am thinking about:example To see if it really works, I explored some tutorials that explains how to connect Arduino input with Processing. Currently, I have these codes in both programs: for Arduino:

const int anPin1 = 0;
long distance1;

void setup() {
  Serial.begin(9600);  // sets the serial port to 9600
}

void read_sensors(){
  /*
  Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
  Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
  */
  distance1 = analogRead(anPin1)/2;
}

void print_all(){
  /*
  Serial.print("S1");
  Serial.print("inches");
  */
  Serial.print(" ");
  Serial.print(distance1);
  Serial.println();
}

void loop() {
  read_sensors();
  print_all();
  delay(50); 
}

And for Processing:

import processing.serial.*;  
Serial myPort;  
String data="" ;
PFont  myFont;  
int distance;

void setup(){
  size(1366,900); // size of processing window
  //background(0);// setting background color to black

  myPort = new Serial(this, "/dev/cu.usbmodemfd111", 9600);
  myPort.bufferUntil('\n');
}

void draw(){

  background(0);
  textAlign(CENTER);
  fill(255);
  text(data,820,400);
  textSize(100);
  fill(#4B5DCE);
  text("              Distance :        cm",450,400);
  noFill();
  stroke(#4B5DCE);

}

void serialEvent(Serial myPort){

  data=myPort.readStringUntil('\n');

}

And this distance reading works perfect!

Unfortunately didn’t found any specific example for what i am looking for. So the question is, how can I get, for example, 3 pictures working this way:

if(distance>60){
image(photo1, 0, 0); //display first photograpgy
}
else if (40<distance<60) {
image(photo2, 0, 0);
}
else if (distance<40) {
image(photo3, 0, 0);
}

Do I have to write something like that in draw ()? And how can I get income distance value as number from which depends displayed image? And should I upload any specific library for this? Do i have to write something also again in Arduino code?

Would be great if someone could suggest any examples or codes for this.

Hoping for advise, komats!!

Answers

  • Looks very good

    Place this in draw of processing code

    No libraries needed

    Load images in setup ()

    Check with println the values distance is having, they must match your ifs

  • edited June 14

    So now I have in processing this code:

    import processing.serial.*;  
    PImage photo1, photo2, photo3;
    Serial myPort;  
    String data="" ;
    PFont  myFont;  
    int distance;
    
    
    void setup(){
      size(1366,900); // size of processing window
    
      photo1 = loadImage("photo1.jpg");
      photo2 = loadImage("photo2.jpg");
      photo3 = loadImage("photo3.jpg");
      //background(0);// setting background color to black
    
      myPort = new Serial(this, "/dev/cu.usbmodemfd121", 9600);
      myPort.bufferUntil('\n');
    }
    
    void draw(){
      if(distance>50){
      image(photo1, 0, 0); //display first photograpgy
      }
      else if (40<distance && distance<60) {
      image(photo2, 0, 0);
      }
      else if (distance<40) {
      image(photo3, 0, 0);
    }
    
    }
    
    void serialEvent(Serial myPort){
    
      data=myPort.readStringUntil('\n');
    
    }
    

    It plays only photo3. How can i refer distance to income values? I guess that the income is written in last lines data=myPort.readStringUntil('\n'); ?!

  • Yes try

    distance=int(data);

  • Try println distance

  • edited June 14

    Your ifs are a mess

    Basically they must have the right ranges in the right order

    they must also match the range data /distance are in - use println to find that out

    new version

     if(distance <= 40){
          image(photo1, 0, 0); //display first photography
      }
      else if (distance > 40 && distance < 60) {
          image(photo2, 0, 0); //display 2nd photography
      }
      else if (distance >= 60) {
          image(photo3, 0, 0); //display 3rdphotography
      }
    

    use ctrl-t in processing to get better indents automatically

    [EDITED]

  • edited June 15

    In consol the range of the sensor is from 5 up to 60 (where is the ceiling). After updating the code the picture still is the same one. And even if the data shows more than 20, it plays photo1.

    import processing.serial.*;  
    PImage photo1, photo2, photo3;
    Serial myPort;  
    String data="" ;
    PFont  myFont;  
    int distance;
    
    
    void setup() {
      size(900, 900); // size of processing window
    
      photo1 = loadImage("photo1.jpg");
      photo2 = loadImage("photo2.jpg");
      photo3 = loadImage("photo3.jpg");
      //background(0);// setting background color to black
    
      myPort = new Serial(this, "/dev/cu.usbmodemfd121", 9600);
      myPort.bufferUntil('\n');
    }
    
    void draw() {
      println(data);
      distance=int(data);
      if (distance <= 20) {
        image(photo1, 0, 0); //display first photography
      } else if (distance > 20 && distance < 40) {
        image(photo2, 0, 0); //display 2nd photography
      } else if (distance >= 50) {
        image(photo3, 0, 0); //display 3rdphotography
      }
    }
    
    void serialEvent(Serial myPort) {
    
      data=myPort.readStringUntil('\n');
    }
    
  • You forgot to set distance from data

    In line 28 you want 40

    say background (0); at start of draw() to clear canvas

  • And use println(distance);

    !!!!

  • When I use println(distance); it shows zeros. I wrote distance=int(data) in draw, but now changed it in setup.

  • Belongs after line 35!!!

  • edited June 15

    Nothing changes. Still zeros and one picture. But previous println(distance) showed the numbers at least.. Could there be wrong datatypes for something?

    import processing.serial.*;  
    PImage photo1, photo2, photo3;
    Serial myPort;  
    String data="" ;
    int distance;
    
    
    void setup() {
      size(900, 900); // size of processing window
      photo1 = loadImage("photo1.jpg");
      photo2 = loadImage("photo2.jpg");
      photo3 = loadImage("photo3.jpg");
    
      myPort = new Serial(this, "/dev/cu.usbmodemfd111", 9600);
      myPort.bufferUntil('\n');
    }
    
    void draw() {
      background(0);
      println(distance);
      if (distance <= 20) {
        image(photo1, 0, 0); //display first photography
      } else if (distance > 20 && distance < 40) {
        image(photo2, 0, 0); //display 2nd photography
      } else if (distance >= 40) {
        image(photo3, 0, 0); //display 3rdphotography
      }
    }
    
    void serialEvent(Serial myPort) {
    
      data=myPort.readStringUntil('\n');
      distance=int(data);
    }
    
  • Answer ✓

    What does println data give you

    What gives println distance? numbers or zeros?

    try

    distance=int(trim(data));

  • There is a new forum

    Please ask there

  • Omg! Don't have a clue how, but distance=int(trim(data)); completely fixed everything! huge thanks! I think I would not get answers in the new forum.

  • Answer ✓

    Great!

    ;-)

  • edited June 16 Answer ✓

    Lessons learned:

    • println is your friend. When you don’t know the value of a variable you can’t evaluate it with if

    • read your code slowly and carefully: when you forget to assign a value to a variable, it won’t work. When your if-clause is wrong it won’t work.

    • Use ctrl-t for automatic indents in processing

    • It’s good that you always showed the entire code when posting so we were always on the same page.

    Chrisir ;-)

Sign In or Register to comment.