Bluetooth doesn't send an input signal

Bluetooth doesn’t send an input signal.
Can you check if there are any mistakes?
I’m sorry, the cord is too long.
Thank you

import ddf.minim.*;

Minim minim;
AudioPlayer player;

import processing.serial.*;

import processing.video.*;

import SimpleOpenNI.*;

Serial serial;

PImage runBG;

Movie movie, R1, R2, BG, RT, RR;

SimpleOpenNI  kinect;

int[] userList;

float headX, headY, oldHeadX, oldHeadY;
float lHandX, lHandY, rHandX, rHandY, oldLHandX, oldLHandY, oldRHandX, oldRHandY;
float lElbowX, lElbowY, rElbowX, rElbowY, oldLElbowX, oldLElbowY, oldRElbowX, oldRElbowY;

PImage[] ready = new PImage[30];
PImage[] run = new PImage[23];
PImage[] jump = new PImage[22];
PImage[] breath = new PImage[21];

PImage[] runTurtle = new PImage[25];
PImage[] runRabbit = new PImage[20];

int runningCount;

int bgX;

float runTX, runTY, runRX, runRY;
float runRCal, runTCal;
int runTC, runRC;
float rSpeed = 1;
float tSpeed;
float bgSpeed = 10;

float imX = 685;
float imY = 190;

boolean isRunning = false;
char trainingState;
int trainingC = 0;

void setup() {
  size(1920, 1080);
  
  minim = new Minim(this);  
  player = minim.loadFile("game_bgm.mp3");


  for (int i = 0; i < ready.length; i ++) {
    ready[i] = loadImage("ready_" + i + ".png");
  }
  for (int i = 0; i < run.length; i ++) {
    run[i] = loadImage("run_" + i + ".png");
  }
  for (int i = 0; i < jump.length; i ++) {
    jump[i] = loadImage("jump_" + i + ".png");
  }
  for (int i = 0; i < breath.length; i ++) {
    breath[i] = loadImage("breath_" + i + ".png");
  }

  runBG = loadImage("runBG_small.png");

  for (int i = 0; i < runTurtle.length; i ++) {
    runTurtle[i] = loadImage("turtle_" + i + ".png");
  }
  for (int i = 0; i < runRabbit.length; i ++) {
    runRabbit[i] = loadImage("rabbit_" + i + ".png");
  }

  bgX = 0;

  runTX = 50;
  runTY = 600;
  runRX = 50;
  runRY = 450;

  runRCal = 0;
  runTCal = 0;

  runTC = 0;
  runRC = 0;

  kinect = new SimpleOpenNI(this);

  kinect.enableRGB();
  kinect.enableDepth();
  kinect.enableUser(); 

  R1 = new Movie(this, "real_1.mp4");
  R2 = new Movie(this, "real_2.mp4");
  BG = new Movie(this, "BG.mp4");  
  RT = new Movie(this, "real_turtle.mp4");
  RR = new Movie(this, "real_rabbit.mp4");
  //R1 = new Movie(this, "real_1.mov");
  //R2 = new Movie(this, "real_2.mov");
  //BG = new Movie(this, "BG.mov");  
  //RT = new Movie(this, "real_turtle.mov");
  //RR = new Movie(this, "real_rabbit.mov");

//   serial = new Serial(this, "/dev/cu.usbmodem14401", 9600); //HC06
    serial = new Serial(this, "/dev/cu.HC-06-DevB", 9600);

  noCursor();

  movie = R2;
  movie.play();
}

void draw() {
  image(movie, 0, 0);

  if (movie == R1) {
    if (movie.time() > movie.duration() - 0.1) {
      movie.stop();
      movie = BG;
      movie.play();
    }
  }

  if (movie == BG) {
    if (movie.time() > movie.duration() - 0.1) {
      trainingC = 0;
      movie.stop();
      movie = R2;
      movie.play();
      isRunning = false;
    } else {
      kinect.update();
      userList = kinect.getUsers();
      if (userList.length > 0) {
        PVector v = new PVector();
        kinect.getJointPositionSkeleton(userList[0], SimpleOpenNI.SKEL_HEAD, v);
        kinect.convertRealWorldToProjective(v, v);
        headX = v.x;
        headY = v.y;

        kinect.getJointPositionSkeleton(userList[0], SimpleOpenNI.SKEL_LEFT_HAND, v);
        kinect.convertRealWorldToProjective(v, v);
        lHandX = v.x;
        lHandY = v.y;

        kinect.getJointPositionSkeleton(userList[0], SimpleOpenNI.SKEL_RIGHT_HAND, v);
        kinect.convertRealWorldToProjective(v, v);
        rHandX = v.x;
        rHandY = v.y;

        kinect.getJointPositionSkeleton(userList[0], SimpleOpenNI.SKEL_LEFT_ELBOW, v);
        kinect.convertRealWorldToProjective(v, v);
        lElbowX = v.x;
        lElbowY = v.y;

        kinect.getJointPositionSkeleton(userList[0], SimpleOpenNI.SKEL_RIGHT_ELBOW, v);
        kinect.convertRealWorldToProjective(v, v);
        rElbowX = v.x;
        rElbowY = v.y;
      }

      if (trainingC == 0) {
        trainingState = 'b';
        if ((lHandX > lElbowX) && (rHandX > rElbowX)) {
          if ((lHandY < lElbowY) && (rHandY < rElbowY)) {
            trainingState = 'r';
          }
        } else if ((headX > oldHeadX) && (lHandX < oldLHandX) && (lElbowX < oldLElbowX)) {
          trainingState = 'n';
        } else if (((headX < oldHeadX) && (rHandX > oldRHandX) && (rElbowX > oldRElbowX))) {
          trainingState = 'n';
        } else if (headY < oldHeadY) {
          trainingState = 'j';
        }
      }
      switch (trainingState) {
      case 'r':
        image(ready[trainingC], imX, imY);
        trainingC ++;
        trainingC %= ready.length;
        break;
      case 'n':
        image(run[trainingC], imX, imY);
        trainingC ++;
        trainingC %= run.length;
        break;
      case 'j':
        image(jump[trainingC], imX, imY);
        trainingC ++;
        trainingC %= jump.length;
        break;
      default :
        image(breath[trainingC], imX, imY);
        trainingC ++;
        trainingC %= breath.length;
        break;
      }

      oldHeadX = headX;
      oldHeadY = headY;
      oldLHandX = lHandX;
      oldLHandY = lHandY;
      oldRHandX = rHandX;
      oldRHandY = rHandY;
      oldLElbowX = lElbowX;
      oldLElbowY = lElbowY;
      oldRElbowX = rElbowX;
      oldRElbowY = rElbowY;
    }
  }

  if (movie == R2) {
    if (isRunning) {
      if (serial.available() > 0) {
        runningCount = serial.read();
      }
      player.play();
      
      image(runBG, bgX, 0);

      if (runRX > bgX + 10060) {
        if (runRCal < 250) {
          runRCal += 4;
        }
      }
      image(runRabbit[runRC], runRX, runRY - runRCal);

      if (runTX > bgX + 10060) {
        if (runTCal < 250) {
          runTCal += 4;
        }
      }
      image(runTurtle[runRC], runTX, runTY - runTCal);

      if (runningCount > 5) {
        tSpeed = 2;
      } else if (runningCount > 0) {
        tSpeed = 1;
      } else {
        tSpeed = -1;
      }

      //runTX = constrain(runTX + tSpeed, 50, width / 2);
      //runRX = constrain(runRX + rSpeed, 50, width / 2 - runRabbit[0].width / 2);
      runTX = constrain(runTX + tSpeed, 50, width / 2 + 200);
      runRX = constrain(runRX + rSpeed, 50, width / 2 + 200);

      runTC ++;
      runTC %= runRabbit.length;
      runRC ++;
      runRC %= runRabbit.length;

      if (bgX > -1 * runBG.width + width) {
        bgX -= bgSpeed;
      }

      if (runRX > bgX + 11500) {
        isRunning = false;
        player.close();
        movie = RR;
        movie.play();
      } else if (runTX > bgX + 11500) {
        isRunning = false;     
        player.close();
        movie = RT;
        movie.play();
      }
    } else {
      if (movie.time() > movie.duration() - 0.1) {
        movie.stop();
        serial.clear();
        isRunning = true;
      }
    }
  }

  if ((movie == RR) || (movie == RT)) {
    if (movie.time() > movie.duration() - 0.1) {
      bgX = 0;

      runTX = 50;
      runTY = 600;
      runRX = 50;
      runRY = 450;

      runRCal = 0;
      runTCal = 0;

      runTC = 0;
      runRC = 0;

      movie.stop();
      movie = R1;
      movie.play();
    }
  }
}

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

void onNewUser(SimpleOpenNI curkinect, int userId) {
  curkinect.startTrackingSkeleton(userId);
}

I was unable to run the code that you posted. Could you please post a simple example which demonstrates the problem?

2 Likes

@svan === this code cannot run with android; it uses imports that are incompatible ( minim, simple openni …) - if you want an answer ( for bluetooth) rewrite your code in a more simple and direct way

1 Like