Arduino and Processing

edited February 2016 in Arduino

I have the arduino and ultrasonic HC-SR04. I want to help please us. The arduino want to measure the distance and what gives the serial. Communicates with the processing and when the distance is greater than 150cm displays an image, if less playing video. My problem is that I run the processing but the video plays either away or close. Please help me. I beginner!!

Arduino is from a library NewPing // --------------------------------------------------------------------------- // Example NewPing library sketch that does a ping about 20 times per second. // ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  Serial.println("cm");
}

Processing

// Video
import processing.video.*;
Movie movie;

// Image
PImage img;  // Declare variable "a" of type PImage

// Serial
import processing.serial.*;
Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port


void setup() {
  size(640, 360);
  background(0);

  printArray(Serial.list());
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 115200);

  // Load and play the video in a loop
  movie = new Movie(this, "transit.mov");
  movie.loop();
  img = loadImage("moonwalk.jpg");  // Load the image into the program
}

void movieEvent(Movie m) {
  m.read();
}

void draw() {
  if (movie.available() == true) {
    movie.read(); 
  }
  image(movie, 0, 0, width, height);

  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }  


  if (val>130) {
    image(img, 0, 0);
  } else {
    image(movie, 0, 0, width, height);
  }
  println(val);
}

Answers

  • Can you help me??? Please.

  • edited February 2016

    Let me try. Try to delete all of this and tell what happens:

      if (movie.available() == true) {
      movie.read(); 
      }
      image(movie, 0, 0, width, height);
    
  • I did it and it doesn't work. Also, I see only video. When I put front my hamd ultrasonic at a distance 150cm and it doesn't show me the picture.

  • arduino serial.println send a ascii text,,processing myport.read read a byte, try arduino Serial.write or processing myport.readString

  • println(val); what is the value of val? is it as expected?

  • edited February 2016

    Camperos I change but it doesn't work. Ater my project helped a friend of mine but he does not know very well. The code Arduino he made is this. #include <NewPing.h>

    #define PING_PIN  12  // Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
    #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    
    NewPing sonar(PING_PIN, PING_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    
    void setup() {
      Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
    }
    
    void loop() {
      delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
      unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
      if (uS / US_ROUNDTRIP_CM > 150) {
        // on spam data (=0) do nothing
        }
     else
     {
      Serial.print("Ping:");
      Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
      Serial.println("cm");
    
      Serial.write(uS / US_ROUNDTRIP_CM); // to Processing
      }
      delay(50);
    }
    

    But I think that it doesn't work 100%. Τhe former which was posted on the library NewPing in Arduino. Can you help me plz?

  • edited March 2016 Answer ✓

    Why do you send descriptive texts out like "Ping:" or "cm"?
    Why would Processing's receiving end need that for?

    Also you send out uS / US_ROUNDTRIP_CM 2 times.
    1st as String and 2nd as int?
    If you intend to read 1 val only from Processing, just send out 1 val!


    // forum.Processing.org/two/discussion/15102/arduino-and-processing
    // 2016-Feb-26
    
    #define PING_PIN 12
    #define MAX_DISTANCE 400
    #define MAX_CM 150
    #define BAUDS 115200
    #define DELAY 50
    
    NewPing sonar(PING_PIN, PING_PIN, MAX_DISTANCE);
    
    void setup() {
      Serial.begin(BAUDS);
    }
    
    void loop() {
      const unsigned int uS = sonar.ping(), trip = uS / US_ROUNDTRIP_CM;
      if (trip <= MAX_CM)  Serial.println(trip);
      delay(DELAY);
    }
    

    // forum.Processing.org/two/discussion/15102/arduino-and-processing
    // 2016-Feb-26
    
    import processing.serial.Serial;
    int val;
    
    void setup() {
      noLoop();
      final String portName = Serial.list()[1];
      new Serial(this, portName, 115200).bufferUntil(ENTER);
    }
    
    void draw() {
      println(val);
    }
    
    void serialEvent(final Serial s) {
      val = int(s.readString().trim());
      redraw = true;
    }
    

  • Thank you very much my friends and thank you my friend GoToLoop. It works my project. :) :D

Sign In or Register to comment.