Author Topic: Arduino and Processing Launchpad type thing  (Read 2270 times)

0 Members and 1 Guest are viewing this topic.

Offline Rihards VeipsTopic starter

  • Contributor
  • Posts: 22
  • Country: lv
Arduino and Processing Launchpad type thing
« on: April 28, 2014, 07:12:33 pm »
I am trying to make something similar to a Novation Launchpad with buttons connected to my arduino.
The arduino sends a serial message and Processing receives it and if it is "1" it plays a sound[Using MINIM] but I have a problem that the sound plays only one time and then doesn't react to the serial messages.
If you could help me please do :D

(My computer broke the Processing code file... when I write it again I will update this topic, but if anyone could write me one that would be awesome :D)

[[Arduino code]]

==============================================================

const int buttonPin = 2;   
int buttonState = 0;     

void setup() {
  pinMode(buttonPin, INPUT);     
}

void loop(){

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {     
    Serial.print("1");
  }
  else {
     delay(10);
  }
}
- Le Nub
 

Offline Hideki

  • Frequent Contributor
  • **
  • Posts: 256
  • Country: no
Re: Arduino and Processing Launchpad type thing
« Reply #1 on: April 28, 2014, 10:38:23 pm »
Right now, the code will try to send "1" as fast as it possibly can, as long as you're holding the button down. That's bad :)

What you will want to do is to only send the "1" when the button state changes from up to down, then stop sending.

One easy way to do this is to keep track of the previous state.
Declare a new variable and add prevState = buttonState; right before you do digitalRead.
Then change the next line to: if (buttonState == HIGH && prevState == LOW) {

To do button debouncing, it's better to put a delay right after you send the "1" and just delete the "else" part.

Note that it's common to let the input be HIGH through a pullup resistor then connect the input to ground when you push the button.
Either way works, but since the AVR has built-in pullups you can avoid using additional resistors.
 

Offline Rihards VeipsTopic starter

  • Contributor
  • Posts: 22
  • Country: lv
Re: Arduino and Processing Launchpad type thing
« Reply #2 on: April 29, 2014, 12:21:14 pm »
I have a 100ms delay after the Serial.print()    forgot to add thata to the post, but the problem is with Processing - it only plays a sound one time


Right now, the code will try to send "1" as fast as it possibly can, as long as you're holding the button down. That's bad :)

What you will want to do is to only send the "1" when the button state changes from up to down, then stop sending.

One easy way to do this is to keep track of the previous state.
Declare a new variable and add prevState = buttonState; right before you do digitalRead.
Then change the next line to: if (buttonState == HIGH && prevState == LOW) {

To do button debouncing, it's better to put a delay right after you send the "1" and just delete the "else" part.

Note that it's common to let the input be HIGH through a pullup resistor then connect the input to ground when you push the button.
Either way works, but since the AVR has built-in pullups you can avoid using additional resistors.
- Le Nub
 

Offline Hideki

  • Frequent Contributor
  • **
  • Posts: 256
  • Country: no
Re: Arduino and Processing Launchpad type thing
« Reply #3 on: April 29, 2014, 06:34:53 pm »
I don't know much about Processing, but you won't get much help from anyone if you don't show any code :)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf