[Processing] Working with sensors

edited January 2017 in Raspberry PI

Hey everyone.

Lately, I've been trying to create a mini game with Processing. My goal is to make a sketch which has several questions and answers, and I want the player to make his choice for each. The thing is, I found this code below, which is the game with an ArrayList for the questions, but it can be played with keyPressed, and as far as I'm concerned, I want to make it through Capacitive sensors, which would be placed on certain objets in a room. These sensors would receive the information, and say if it's correct or not. At this point, I'm totally clueless. I've already done this through Pure Data (with sensors as well), my code works great but I don't know how to do it with Processing and Sensors now. I can put the code if needed.

ArrayList <Question> questions = new ArrayList();
int indexQuestions = 0;

// the status of the program 
final int play=0;       // possible status 
final int gameOver=1;   // possible status 
final int feedback=2;   // possible status 
int status = play;      // current status


void setup() {
  size(800, 600);
  // define all questions
  String[] a1 = loadStrings("test.txt");
  // println (a1);
  for (int i = 0; i<a1.length; i+=4) {
    Question currentquestion = new Question( a1[i], a1[i+1], a1[i+2], a1[i+3], 2, 30, 30 );
    questions.add(currentquestion);
  }
}

void draw() {
  background(0);
  switch (status) {
  case play:
    questions.get(indexQuestions).display(); 
    break;
  case feedback:
    // correct? 
    if (questions.get(indexQuestions).check(key)) {
      text ("Gut", 100, 200);
    }
    else {
      text ("Nein", 100, 200);
    } // else
    text ("Bitte eine Taste", 100, 300); 
    break;
  case  gameOver:
    text ( "All questions done.", 200, 200 );
    text ( "Press r to restart.", 200, 400 );
    break;
  }  // switch
} // func 

// -------------------------------------------

void keyPressed() {
  switch (status) {
  case play:
    if (key>='1' && key <= '3') 
    { 
      status=feedback;
    }
    break;
  case feedback:
    // any key 
    // next status
    status = play; 
    // next question 
    indexQuestions++;
    // last question? 
    if (indexQuestions>questions.size()-1)
    {
      status = gameOver; // game over
    } // if
    break;
  case gameOver:
    if (key == 'r') {
      // restart 
      indexQuestions=0;
      status=play;
    } // if
    break;
  default:
    // error 
    break;
  } // switch
  //
} // func 

// ==================================================================

class Question {
  String quest;
  String answer1, answer2, answer3;
  int correctAnswerNumber; 
  int posX, posY;

  // constr
  Question (String que, 
  String answer1temp, String answer2temp, String answer3temp, 
  int correctAnswerNumberTemp, 
  int x, int y) {
    quest = que;
    answer1=answer1temp;
    answer2=answer2temp;
    answer3=answer3temp;
    correctAnswerNumber=correctAnswerNumberTemp;
    posX=x;
    posY=y;
  }

  void display() {
    text(quest, posX, posY);
    text("  1  "+answer1, posX, posY+22);
    text("  2  "+answer2, posX, posY+44);
    text("  3  "+answer3, posX, posY+66);
    text("Please enter a number", posX, posY+94);
  } // method 

  boolean check( char keyToTest ) {
    if (keyToTest=='1' && correctAnswerNumber==1) return true;
    if (keyToTest=='2' && correctAnswerNumber==2) return true; 
    if (keyToTest=='3' && correctAnswerNumber==3) return true; 
    return false;
  } // method 
  //
} // class ================================================================

Thank you.

Tagged:

Answers

  • @Allycia -- It sounds like a very interesting project.

    What are you using for the capacitive sensors? An Arduino? A Raspberry Pi? An OSC controller? What did you use with PD / Pure Data?

    There are several libraries to send messages to / from special interface devices / hardware -- but they depend on the type of hardware. Once you have picked a library, you can look at the basic examples and demos for that library.

    Look at:

    1. Processing Libraries under the Contributed Libraries "Hardware" section
    2. [Processing Hardware I/O Library
  • Hey, sorry for the delay and also thank you for your answer.

    The capacitive sensor is that thing : http://hpics.li/d25c5b3. It's a Raspberry Pi card, on which we have to connect these capacitive sensors.

    With Pure Data, we used the same equipment. I'm totally lost with Processing, even though it's quite the same thing.

    Some people told me to use Serial library, but even with this library, I don't understand how to make this work, the codes we have to write seem to be difficult, I don't even know where to begin.

  • edited January 2017

    @Allycia --

    How did you use the Capacitif capacitive sensors with Raspberry Pi and Pure Data? Did you have the Raspberry Pi send messages, like MIDI or OSC, over a wireless or network cable, and then have a desktop machine running Pure Data listen for them?

    Explain more about the setup you have already used and know how to implement with the R.Pi -- you can probably use the same setup with Processing, and this will make the problem easier (your Raspberry Pi will be the same, and only Processing will change).

Sign In or Register to comment.