How to avoid multiple keypresses from Fruit Piano sensors (Arduino USB HID)

edited October 2015 in Arduino

I'm using Capacitive Sensors to measure input from fruit to trigger events in Processing (like a sound, or in this case a video). I don't want to delay the Arduino program, so it keeps scanning for input. The Arduino sens keypresses (as a USB HID device) to the pc, so I can detect keypresses in Processing. The problem is that often the Arduino is scanning so fast that it sends multiple Keypresses, so the video keeps triggering.

I want to solve this in Processing, so that I don't have to use a delay in the Arduino code. My question is, how do I integrate this type of code:

int sX, sY, sT;

void setup(){
  size(200,200);
}

void draw(){
  background(0);
  if( mousePressed ){
    sX = mouseX;
    sY = mouseY;
    sT = millis() + 2000;
  }
  if( millis() > sT && millis() < sT + 2000 ){
    fill(255,0,0);
    rect(sX-5, sY-5, 10, 10);
  }
}

into this:

  import processing.video.*;


int maxmyMovies = 5; 
int myMoviesIndex = 0; 
Movie[] myMovies = new Movie[maxmyMovies];

void setup() {

  size(1280, 720);
  for (int i = 0; i < myMovies.length; i ++ ) {
    myMovies[i] = new Movie(this, i + ".mp4");
    myMovies[i].loop();
  }
}

void movieEvent(Movie myMovies) {

  myMovies.read();
}

void draw() {
  image(myMovies[myMoviesIndex], 0, 0, 1280, 720); // Displaying one image
}

void keyPressed() {

  if (key=='a') {
    myMovies[myMoviesIndex].pause();
    myMovies[myMoviesIndex].jump(0.0);
    myMoviesIndex = 1;
    myMovies[myMoviesIndex].play();
  } 
  if (key=='b') {
    myMovies[myMoviesIndex].pause();
    myMovies[myMoviesIndex].jump(0.0);
    myMoviesIndex = 2;
    myMovies[myMoviesIndex].play();
  } 
  if (key=='c') {
    myMovies[myMoviesIndex].pause();
    myMovies[myMoviesIndex].jump(0.0);
    myMoviesIndex = 3;
    myMovies[myMoviesIndex].play();
  } 
  if (key=='d') {
    myMovies[myMoviesIndex].pause();
    myMovies[myMoviesIndex].jump(0.0);
    myMoviesIndex = 4;
    myMovies[myMoviesIndex].play();
  }
}

I have to scan for almost the entire alphabet, so the code is getting very messy. Is there also a way of simplifying this? Thanks in advance for your help.

Answers

  • Answer ✓

    Same way you use movieEvent() to read() a new PImage video frame, you should apply to serialEvent():
    https://Processing.org/reference/libraries/serial/serialEvent_.html

    And establish a specific byte value as a trigger for serialEvent() via bufferUntil():
    https://Processing.org/reference/libraries/serial/Serial_bufferUntil_.html

    There are many forum threads about it too:
    http://forum.Processing.org/two/discussions/tagged?Tag=bufferuntil()

  • Answer ✓
    void keyPressed() {
      final int k = keyCode, idx = k - 'A';
    
      if (idx >= 0 & idx < myMovies.length) {
        myMovies[myMoviesIndex].stop();
        myMovies[myMoviesIndex = idx].loop();
      }
    }
    
  • Answer ✓

    For the alphabet-question: You can use the value of the "key"-character to calculate the movie-index. Somthing like this:

    void keyPressed() {
      // check if key is in range
      if (key >= 'a' && key <= 'z') {
        myMovies[movieIndex].pause();
        myMovies[movieIndex].jump(0.0);
        movieIndex = key - 'a';
        myMovies[movieIndex].play();
      }
    }
    

    One thing that looks odd: you use "myMovies" as a name for your movie-array and as a variable inside of movieEvent(). Though it works, i would try to use different names to avoid irritation.

    For the timing: When you start a movie, you can store the time with the use of millis() and on key-events you can check if enough time has passed since the last movie started.

    long startTime = -3000;
    void setup() {}
    void draw() {}
    
    void keyPressed() {
      if (millis() - startTime > 3000) {
        println("time to start a new movie");    
        startTime = millis();
      }else{
        println("be patient...");
      }
    }
    
  • Thanks! I think I can manage with this info! And I learned something new, which is always good.

Sign In or Register to comment.