Go Down

Topic: serial communication between arduino and processing (Read 794 times) previous topic - next topic

javedh8

i want to control two videos using two capactive sensors but don't get how to relate videos with capacitive sensor's  on serial communication,
  i want like if i touch "sensor one" then "movie one" will play if touch "sensor two " then first movie stop and "movie two " will play and vice versa. i also want to know the function of serial.print();
Here is my code for arduino....



Code: [Select]
#include "mpr121.h"
#include <Wire.h>

int irqpin = 2;  // Digital 2

boolean touchStates[2]; //to keep track of the previous touch states


void setup(){
  pinMode(irqpin, INPUT);
  digitalWrite(irqpin, HIGH); //enable pullup resistor



  Serial.begin(9600);
  Wire.begin();

  mpr121_setup();
}

void loop(){

  readTouchInputs();


}


void readTouchInputs(){
  if(!checkInterrupt()){

   
    Wire.requestFrom(0x5A,2);

    byte LSB = Wire.read();
    byte MSB = Wire.read();

    uint16_t touched = ((MSB << 8) | LSB);


    for (int i=0; i < 12; i++){ 
      if(touched & (1<<i)){

        if(touchStates[i] == 0){



          if(i==0)
          {

           Serial.print((char)1);
           
          }
         
          if(i==1)
          {

            Serial.print((char)2);
          }
         
         
          else if(touchStates[i] == 1){
            //pin i is still being touched
          } 

          touchStates[i] = 1;     
        }
      }
        else{
          if(touchStates[i] == 1){
            if(i==0)
            {

             Serial.print((char)0);
             
            }
           
             
            if(i==1)
            {

              Serial.print((char)0);
            }
           
           
            touchStates[i] = 0;
          }

        }

      }
     
   
  }

}



here is my code for processing


Code: [Select]
import processing.serial.*;
import processing.opengl.*;
import codeanticode.gsvideo.*;
import codeanticode.glgraphics.*;
Serial port;



GSMovie myMovie, myMovie2, nowPlaying;
GLTexture tex;
PImage img;
int val = 0;

void setup() {
  size(960, 540, GLConstants.GLGRAPHICS);
  background(0);

  tex = new GLTexture(this);
  myMovie = new GSMovie(this, "looper.mov" );
  myMovie2 = new GSMovie(this, "childplay.mov" );
 
nowPlaying = myMovie;
  nowPlaying.loop();
 
  println(Serial.list());
port = new Serial(this, Serial.list()[1], 9600);
 
}


void draw() {
background(255);
image(nowPlaying,0,0,width,height);
if (0 < port.available()) {
val = port.read();
}

if (val == 1) {

  myMovie2.pause();
    myMovie2.jump(0);
    nowPlaying = myMovie;
   
   
    nowPlaying.loop();
}
   
   

if (val == 2) {

  myMovie.pause();
    myMovie.jump(0);
    nowPlaying = myMovie2;
        nowPlaying.loop();

}

void movieEvent(GSMovie _mov){
  _mov.read();
}


thanxs in anticipation...................

PaulS

Code: [Select]
void loop(){
  readTouchInputs();
}

I guess I'll never understand why a function does nothing but call another function.

Code: [Select]
            if(i==0)
            {

             Serial.print((char)0);
             
            }
           
             
            if(i==1)
            {

              Serial.print((char)0);
            }

A reasonable amount of white space is one thing. This amount of white space is something else.

Putting each { on a new line and using Tools + Auto Format and deleting all the useless white space would make your code a lot easier to read. But, basically, it looks like your Arduino sketch is sending '0', '1', or '2' to the serial port when a key is pressed or released.

Then. your Processing sketch is looking for 0, 1, or 2. 0 != '0'. 1 != '1'. 2 != '2'.

Of course, all that you said was what you wanted. You said nothing about what the code actually does, or what problems you are having.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy