how to control videos from 3 ultrasonic sensors from arduino

ileile
edited August 2015 in Arduino

HI ! I'm working with 4 ultrasonic sensor, that plays 2 videos and 2 sounds, the problem is that at some point the computer restart the videos, and sounds. I can`t figure out where is the problem, i already chek the sensor and they work find, so, mabye is a problem of comunication betwen arduino and processing? mabye arduino reset?

anyIdeas? help! the openinng of this art instaattion is this friday!

    #include <NewPing.h>
    #define SONAR_NUM     4 // Number or sensors.
    #define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
    #define PING_INTERVAL 633 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
    unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
    byte cm[SONAR_NUM];         // Where the ping distances are stored.
    uint8_t currentSensor = 0;          // Keeps track of which sensor is active.
    NewPing sonar[SONAR_NUM] = {     // Sensor object array.
      NewPing(11, 12, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
      NewPing(10, 9, MAX_DISTANCE),
      NewPing(8, 7, MAX_DISTANCE),
      NewPing(14, 15, MAX_DISTANCE)

    };
    void setup() {
      Serial.begin(9600);
      pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
      for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
        pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
    }
    void loop() {
      for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
        if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
          pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
          if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
          sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
          currentSensor = i;                          // Sensor being accessed.
          cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sensor.
          sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
        }
      }
      // The rest of your code would go here.
    }
    void echoCheck() { // If ping received, set the sensor distance to array.
      if (sonar[currentSensor].check_timer())
        cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
    }
     void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
        Serial.write(cm, SONAR_NUM);
    };
Tagged:

Answers

  • I think first you have to train with the processing examples that are examples of Arduino...Arduino-->file-->example-->communication--> graph (or another)

  • use

    if(1st sensor on}

    else if (2nd sensor on)

    else if (3rd sensor on)

    show your code

    ;-)

  • import processing.video.*; //Importo la librería para tratamiento de video import processing.serial.*; //Importo la librería para tratar datos puerto serie Movie myMovie; Serial arduino; int valor; void setup() { background(0); arduino = new Serial(this, Serial.list()[9], 9600); // Abro el puerto serie size(720, 480); // El tamaño de la pantalla myMovie = new Movie(this, "semilla.mov"); //Reproducimos la pelicula myMovie.stop(); } void draw() { image(myMovie, 0, 0); while (arduino.available() > 0) // Consulto si hay datos en el puerto serie {
    valor = arduino.read(); //Leo y almaceno el valor del puerto serie text(char(valor), width/2, height/2); //Conversión para leer los datos de Arduino println(valor); // Muestro por la consola el valor if (valor<8){ // Distancia igual /inferior para que se reproduzca el video myMovie.play(); } else { myMovie.stop(); //Caso contrario, paro el video para que luego reinicie } } } void movieEvent(Movie m) { m.read(); }

  • It's good idea chrisir! so I guest the right question will be, How do I declare the 3 sensor from arduino in processing?

  • look at the examples please

  • ileile
    edited July 2015

    Well i get close troghout a code , but now i have the problem that i cant get two sensors activate a video, and one just a sound, looks like the check is too fast and oscilates all the time and start over, any ideas?

        import ddf.minim.spi.*;
        import ddf.minim.signals.*;
        import ddf.minim.*;
        import ddf.minim.analysis.*;
        import ddf.minim.ugens.*;
        import ddf.minim.effects.*;
        import de.voidplus.soundcloud.*;
        import processing.video.*;  //Importo la librería para tratamiento de video
        import processing.serial.*; //Importo la librería para tratar datos puerto serie
        Movie[] myMovie;
        Minim minim;
        AudioPlayer player;
        Serial arduino;
        byte[] valor = new byte[]{127,127,127};
        Movie currentMovie;
        void setup() {
          background(0);
          arduino = new Serial(this, Serial.list()[0], 9600); // Abro el puerto serie
          size(720, 480); // El tamaño de la pantalla
         myMovie = new Movie[] { 
            new Movie(this, "video1.mov"), 
            new Movie(this,"video2.mov") 
          };
           minim = new Minim(this);
          player = minim.loadFile("palo.wav");
          player.mute();
    
    
          //Reproducimos la pelicula
          myMovie[0].stop();
          myMovie[1].stop();
        }
        void draw() {
          background(0);
          if (arduino.available() > 0)  // Consulto si hay datos en el puerto serie
          {  
            arduino.readBytes(valor);      
            if (valor[0]<3 && valor[1]<3){ // Distancia igual /inferior para que se reproduzca el video   
              image(myMovie[1], 0, 0);
              myMovie[0].stop();
              currentMovie = myMovie[1];
              String texto = "Deberia Ver Video 2 Sensor 1: " + valor[0] + " Sensor 2: " + valor[1]  + " Sensor 3: " + valor[2];
              text(texto, width/2, height/2); 
            }
            else if (valor[0]<3) {
              image(myMovie[0], 0, 0);
              currentMovie = myMovie[0];
              myMovie[1].stop();
              String texto = "Deberia Ver Video 1 Sensor 1: " + valor[0] + " Sensor 2: " + valor[1]  + " Sensor 3: " + valor[2];
              text(texto, width/2, height/2);
            } else {
              myMovie[0].stop();
              myMovie[1].stop();
              currentMovie = null;
              String texto = "no deberia ver ningun video Sensor 1: " + valor[0] + " Sensor 2: " + valor[1]  + " Sensor 3: " + valor[2];
              text(texto, width/2, height/2);
          }
            if (valor[2]<3) {
              player.unmute(); 
            }
            else {
              player.mute();
            }  
          }
          if (currentMovie != null) {
            image(currentMovie, 0, 0);
            currentMovie.play();
          }
        }
    
  • You'll get a better response if you format your code. Here's how:

    http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

  • thanks!

Sign In or Register to comment.