Arduino + LDR Sensor + Processing

edited May 2016 in Arduino

At the time i dont have the arduino or the light sensors, i will only work on that tomorrow =/. But at processing i'm trying some stuff. The idea is to have a video always in loop, but every time i take the light of a sensor (either if i put my finger on the sensor or a cloth) it show's another video, but when that sensor got light again it goes to the loop video.

This is what i got so far, still missing the keyPressed as a substitute of the sensors. The problem is the videos are all runing ate the same the time, and i've no idea how to do the keyPressing to call the other videos. Its the first time i'm using this library, and i really need help on this.

import processing.video.*;

String[] movieList = {"movie/x.mov", "movie/y.mov", "movie/w.mov", "movie/z.mov"};
Movie[] filmes = new Movie[movieList.length];

void setup() {
  size(1280, 720);
  //fullScreen();
  background (0);
  frameRate(60);

  for (int i = 0; i < movieList.length; i++)
  {
    filmes[i] = new Movie(this, movieList[i]);
    filmes[i].loop();
  }
}

void draw() 
{
  for (int i = 0; i < movieList.length; i++)
  {
    image(filmes[i], 0, 0);
  }
}

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

Answers

  • edited May 2016

    I'm already trying to connect arduino with processing, but i'm stuck now. I will work with 5 LDR Sensor and i need to every time i take the light in one of the sensors, that sensor will show a Video. How do i do that? This is all the code i got so far.

    I got a void keyPressed, cause at the time i was doing some tests

    Arduino

    
    //PIN AO QUAL O SENSOR ESTA LIGADO
    int LDR = 0;
    //GUARDA OS VALORES DADOS PELO SENSOR
    int LDRValue = 0;
    //VALOR APROXIMADO A LUZ EXPOSTA O SENSOR
    int Light_sensitivity = 500;
    
    void setup () {
      Serial.begin(9600);
      //LED PARA TESTARMOS, SE ACENDER QUANDO TAPARMOS LUZ DO SENSOR ESTA  A FUNCIONAR
      pinMode(13, OUTPUT);
    }
    
    void loop(){
      //LE OS VALORS DO SENSOR
      LDRValue = analogRead(LDR);
      //MOSTRA OS VALORES DADOS PELO SENSOR
      Serial.println(LDRValue);
      //VELOCIDADE MOSTRADA PELO SENSOR NA CONSOLA
      delay(50);
    
      if (LDRValue < Light_sensitivity) {
        digitalWrite(Light_sensitivity, HIGH);
      }
      else {
        digitalWrite(Light_sensitivity, LOW);
      }
    }
    
    

    Processing

    import processing.serial.*;
    import processing.video.*;
    
    //ARDUINO
    
    Serial arduinoPort;
    
    int arduinoInputVideo1, arduinoInputVideo2, arduinoInputVideo3, arduinoInputVideo4;
    
    //VIDEOS
    
    Movie Video1;//#1
    Movie Video2;//#2 
    Movie Video3;//#3
    Movie Video4;//#4
    
    //DATA
    
    
    
    boolean Vid1 = true;
    boolean Vid2 = false;
    boolean Vid3 = false;
    boolean Vid4 = false;
    
    
    void setup() { 
      //SETUP
      
      size(1280, 720);
      //fullScreen();
      background(0);
      frameRate(60);
    
      arduinoPort = new Serial (this, Serial.list()[2], 9600);
      arduinoPort.clear();
    
    
      Video1 = new Movie(this, "movie/x.mov");
      Video2 = new Movie(this, "movie/y.mov");
      Video3 = new Movie(this, "movie/w.mov");
      Video4 = new Movie(this, "movie/z.mov");
    }
    
    
    
    void draw() { 
    
      //VIDEO1
      if (Vid1) {
        Video1.loop(); //VIDEO1 FICA EM LOOP
        Video2.stop(); //VIDEO2 PARA
        Video3.stop(); //VIDEO3 PARA
        Video4.stop(); //VIDEO4 PARA
        image(Video1, 0, 0, width, height);
      }
    
      //VIDEO2
      if (Vid2) {
        Video1.stop(); //VIDEO1 PARA
        Video3.stop(); //VIDEO3 PARA
        Video4.stop(); //VIDEO4 PARA
        Video2.play(); //VIDEO2 COMEÇA A DAR
        image(Video2, 0, 0, width, height);
      }
    
      //VIDEO3
      if (Vid3) {
        Video1.stop(); //VIDEO1 PARA
        Video2.stop(); //VIDEO2 PARA
        Video4.stop(); //VIDEO4 PARA
        Video3.play(); //VIDEO3 COMEÇA A DAR
        image(Video3, 0, 0, width, height);
      }
    
      //VIDEO 4
      if (Vid4) {
        Video1.stop(); //VIDEO1 PARA
        Video2.stop(); //VIDEO2 PARA
        Video3.stop(); //VIDEO3 PARA
        Video4.play(); //VIDEO4 COMEÇA A DAR
        image(Video4, 0, 0, width, height);
      }
    }
    
    //FUNÇÃO "movieEvent" É CHAMADA SEMPRE QUE EXISTE UMA UMA FRAME NOVA PARA SER LIDA/VISTA
    //SEM ISTO OS VIDEOS TB NÃO DÃO, OUVE-SE O SOM, MAS NÃO SE VÊ A IMAGEM
    void movieEvent(Movie m) { 
      m.read();
    }
    
    void keyPressed() {
    
      //#1 POE O VIDEO1 A CORRER, PÕE TODOS OS RESTANTES PARADOS
      if (key == '1') {
        Vid1 = true;
        Vid2 = false;
        Vid3 = false;
        Vid4 = false;
      }
    
      //#2 POE O VIDEO2 A CORRER, PÕE TODOS OS RESTANTES PARADOS
      if (key == '2') {
        Vid1 = false;
        Vid2 = true;
        Vid3 = false;
        Vid4 = false;
      }
    
      //#3 POE O VIDEO3 A CORRER, PÕE TODOS OS RESTANTES PARADOS
      if (key == '3') {
        Vid1 = false;
        Vid2 = false;
        Vid3 = true;
        Vid4 = false;
      }
    
      //#4 POE O VIDEO4 A CORRER, PÕE TODOS OS RESTANTES PARADOS
      if (key == '4') {
        Vid1 = false;
        Vid2 = false;
        Vid3 = false;
        Vid4 = true;
      }
    }
    
    
  • Answer ✓

    Arrays are our best friends. Shorten our code and allows us to use loops w/ numerical indices: O:-)
    https://forum.Processing.org/two/discussion/8082/from-several-variables-to-arrays

  • So i've been changing my code in arduino. But i'm getting a problem i cant see resolve because e can't find my problem. When i run the code i always got the same numbers "1100". But what i want to see, is every time i put my finger on one of the LDR sensor is, " LDRValue1 A FUNCIONAR 1 " (i put the 1 here as an example).

    
    #define LDRpin  7  //PIN ONDE O SENSOR LDR E A RESITENCIA ESTAO LIGADOS
    #define LDRpin1 8  //PIN ONDE O SENSOR LDR E A RESITENCIA ESTAO LIGADOS
    #define LDRpin2 12  //PIN ONDE O SENSOR LDR E A RESITENCIA ESTAO LIGADOS
    #define LDRpin3 13 //PIN ONDE O SENSOR LDR E A RESITENCIA ESTAO LIGADOS
    
    //VALORES PIN
    int LDRValue  = 0;
    int LDRValue1 = 0;
    int LDRValue2 = 0;
    int LDRValue3 = 0;
    
    void setup () {
      Serial.begin(9600);
    
      pinMode(LDRpin,  INPUT);
      pinMode(LDRpin1, INPUT);
      pinMode(LDRpin2, INPUT);
      pinMode(LDRpin3, INPUT);
    }
    
    void loop() {
      //LE OS VALORES DO SENSOR
      LDRValue  = analogRead(LDRpin);
      LDRValue1 = analogRead(LDRpin1);
      LDRValue2 = analogRead(LDRpin2);
      LDRValue3 = analogRead(LDRpin3);
    
      //MOSTRA OS VALORES DADOS PELO SENSOR 0
      Serial.print(LDRValue);
      Serial.print(" ");
    
      //MOSTRA OS VALORES DADOS PELO SENSOR 1
      Serial.print(LDRValue1);
      Serial.print(" ");
    
      //MOSTRA OS VALORES DADOS PELO SENSOR 2
      Serial.print(LDRValue2);
      Serial.print(" ");
    
      //MOSTRA OS VALORES DADOS PELO SENSOR 3
      Serial.print(LDRValue3);
      Serial.print(" ");
    
      //TESTE PARA SABER SE TODOS OS SENSORES ESTAO A FUNCIONAR
      //LDRValue
      if ((LDRValue == 1) && (LDRValue1 == 0) && (LDRValue2 == 0) && (LDRValue3 == 0)) {
        Serial.print(" LDRValue0 A FUNCIONAR 0 ");
      }
    
      //LDRValue1
      if ((LDRValue == 0) && (LDRValue1 == 1) && (LDRValue2 == 0) && (LDRValue3 == 0)) {
        Serial.print(" LDRValue1 A FUNCIONAR 1 ");
      }
    
      //LDRValue2
      if ((LDRValue == 0) && (LDRValue1 == 0) && (LDRValue2 == 1) && (LDRValue3 == 0)) {
        Serial.print(" LDRValue2 A FUNCIONAR 2 ");
      }
    
      //LDRValue3
      if ((LDRValue == 0) && (LDRValue1 == 0) && (LDRValue2 == 0) && (LDRValue3 == 1)) {
        Serial.print(" LDRValue3 A FUNCIONAR 3 ");
      }
      //TEMPO DE RESPOSTA
    delay(1000);
    }
    
    
Sign In or Register to comment.