Aruino tilt switch/ buttons as INPUT, notes in Processing as OUTPUT

edited June 2014 in Arduino

Hello Everyone! I try to create a simple keyboard with tilt switches (instead of buttons) as INPUT. The OUTPUT are notes played with Processing. I use the Arduino. and Soundcipher. libraries. It works only with the first digital input. But if I try the next switch there is no signal. It looks like the software runs only until the first "if()".

I would really appreciate your help. Thanks a lot!

import processing.serial.*;
import cc.arduino.*;
import arb.soundcipher.*;


Arduino arduino; 

void setup() {
  size(470, 280);

  // Prints out the available serial ports
  println(Arduino.list());

 arduino = new Arduino(this,"COM25", 57600);

  // Set the Arduino digital pins as inputs

    arduino.pinMode(3, Arduino.INPUT);
    arduino.pinMode(4, Arduino.INPUT);

    arduino.digitalRead(3);           
    arduino.digitalRead(4);

}


void draw (){



  SoundCipher sc = new SoundCipher(this);

  if(arduino.digitalRead(3) == Arduino.HIGH) {   // play the first note if the state of the digital pin 3 is HIGH
     sc.playNote(50, 50, 3.0);
     delay(3000);
  }

       if(arduino.digitalRead(4) == Arduino.HIGH) {  
     sc.playNote(60, 50, 3.0);
     delay(3000);
       }

}

Answers

  • edited June 2014
    • When posting code, highlight it and hit CTRL+K in order to format it!
    • Is it necessary to instantiate a new SoundCipher object @ 60 FPS within draw()?
    • Don't use delay() within draw(). Control draw()'s callback rate via frameRate() instead:
      http://processing.org/reference/frameRate_.html
  • edited June 2014

    Unfortunatelly, it doesn't help. I know, that it is unusual to use an SoundCipher object in draw() but when it is in void setup() it doesn't work (no tone). I think the problem is not that much about the sound but more about recognizing the Aruino digital INPUTs.

    Has anyone an idea?

  • gotoloop is right, you need

    before setup()

    SoundCipher sc;

    in setup()

    sc = new SoundCipher(this);

    the last line has no leading SoundCipher please.

  • frameRate(4); in setup()

  • thanks a lot, but it also didn't solve my problem.

  • I am trying to do something similar but by sending data via the serial from the arduino IDE, here is the post : http://forum.processing.org/two/discussion/6041/need-help-to-play-a-file-whenever-a-button-is-pressed-via-the-arduino#Item_1

    When I press the button for the first time the audio file plays well ( 1st if statement) but when I press the button again it does not play the file. So its as if once the if statement has been executed the program just stops and does nothing.

Sign In or Register to comment.