We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Controling loops with Arduino Serial
Page Index Toggle Pages: 1
Controling loops with Arduino Serial (Read 1987 times)
Controling loops with Arduino Serial
Nov 27th, 2009, 6:12pm
 
Hello everyone! I'm new here, and I need some help. I would be glad deeply for this. I guess it's a question of purely logic. So, let's go.

I'm using some LDRs and a Arduino, just the HIGH and LOW and printing some values in the serial port. This is the Arduino code:

Code:
const int buttonPin = 3;    
const int reggaePin = 4;


void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(reggaePin, INPUT);

}

void loop() {

if (digitalRead(buttonPin) == HIGH) {
Serial.print(10, BYTE);
}
else if (digitalRead(buttonPin) == LOW) {
Serial.print(1, BYTE);
}

delay(50);

if (digitalRead(reggaePin) == HIGH) {
Serial.print(20, BYTE);
}
else if (digitalRead(reggaePin) == LOW) {
Serial.print(2, BYTE);
}


delay(50);


}


This part is working really well. I can even read this on processing, like the image bellow:

img.photobucket.com/albums/v336/parasiteking/Pessoal/ProcessingSerial-1.jpg (I can't post imgs, but this is the link =P)

All the stuff are working, when read 20 or 10 play the minim music. The problem is that I need to read it once, not two or more times. The idea is: send 20 (it can be one or more), do the action that I want (play the song), and then when send 20 again (once again: it be one or more) do the other action (pause the song).

When processing reads 20 more than once per second, the song is played wrong (with some pauses) or even didn't play (if the number of reading "20" equals a pair number, for example).

The idea is the Arduino send 20, play the song, and when send 20 again stop de song. Just this! But it reads 20 more than one time and I need to have a accurate reading.

I used the switch case and some boolean variables. Is this the more efficient way? If you want to suggest others, feel free!

It's simple to explain, but I'm trying this out for several days and nothing. Here's the Processing code:

Code:
import ddf.minim.*;
import processing.serial.*;

Serial portaArduino;
Minim minim;
AudioPlayer player;
AudioPlayer player2;

int val;
boolean p1 = true;
boolean p2 = true;

void setup() {
size (100, 100, P2D);
minim = new Minim(this);


player = minim.loadFile("Game.mp3", 1024);
player2 = minim.loadFile("Macaroni.mp3", 1024);

// - - - - - - - Abaixo versão para Mac p/ leitura de porta - - - - - - -
// portaArduino = new Serial(this, Serial.list()[0], 9600);
// - - - - - - - Versão para WIN p/ leitura de porta - - - - - - -
portaArduino = new Serial(this, "COM9", 9600);

}

void draw() {
if (portaArduino.available() > 9) {
val = portaArduino.read();
println(val);
}


switch(val) {

case 10:
if (p1){
player.play();
p1 = false;
}
else {
player.pause();
p1 = true;

}
break;


case 20:
if (p2){
player2.play();
p2 = false;
}
else {
player2.pause();
p2 = true;
}

break;
}
}

void stop()
{
player.close();
player2.close();
minim.stop();
super.stop();
}
Re: Controling loops with Arduino Serial
Reply #1 - Nov 28th, 2009, 4:12am
 
Your explanation and your arduino code do not match.

You say it should play the song when it receives 20 and stop the song when it receives 20 again. Yet, the arduino code says it sends 20 when the LDR is high (lit) and 2 when it says the LDR is low (unlit).

Also, when the LDR is lit, it will send 20 every 100ms. I don't think you want to respond to this in the way you describe.

I would check if I receive 20, check if my player is playing. If it is not, play it. If it is, ignore the 20 you just received.
When you receive 2, stop the player (maybe check if it is playing, but thats not required, i think).

If this is not how you want the program to react, pleasy describe on a high level what it is you expect from the program.
Re: Controling loops with Arduino Serial
Reply #2 - Nov 28th, 2009, 6:39am
 
Haha! All Right, I guess it was kinda confusing. Let me explain again with more details:

:: Step 1
Arduino is sending all the time 1 and 2, saying that there's light on the LDR.

:: Step 2
When light is interrupted Arduino sends 20, or 10.

:: Step 3
Processing, that was receiving all the amount of data (1 and 2) recognizes, for example, 20 and enters in the switch case, play the music, and turns false the boolean.

:: Step 4
The song is being played, and processing is receiveing from the serial the 1 and 2 once again because the light is no longer interrupted.

:: Step 5
When the light is interrupted again, arduino sends again 20 and once the var is "false" because of the boolean, it pauses.

This is the idea, step by step.
The problem:

Like the image that I sent, my problem is that I want to processing read the first 20 and the subsequents 20 being ignored on the next second.

Processing is reading 20 more than once per sec, so it enters in the switch case more than once, sometimes the song is not played, because it enters true, become false and returns to true (pair number of times) or when the number of serial's 20 is sended 3, 5, 7 times (odd number of times), it plays-pause very fast, making the music weird.

My question is: Is there a way to make processing read JUST the first 20 and ignore the others on the next second, and when this second passes makes it ready to start reading again the nexts 20 that will be inputted on the serial port?

Remind that all the mecanism is like I explained above: sending 1 and 2 all the time (saying that there's light on the LDR) and when send 20 (light on the LDR is turned off) plays the music and keep playing until 20 is sended again by the serial port, when it stops.

And then all this system above being reiniciated again and again. Playing and pausing the music everytime the light on LDR is interrupted or re-interrupted.
Re: Controling loops with Arduino Serial
Reply #3 - Nov 28th, 2009, 8:06am
 
Ah I see.

Well, it's easy! (As it usually is with processing).

Code:

//Top of program
long prevTime1 = 0;
long prevTime2 = 0;

//Draw:
case 10:
if(millis() - prevTime1 > 1000)
{
 ..//rest of code
  prevTime1 = millis();
}

...the same with case 20, but with prevTime2 (or whatever name you wish to use)

Re: Controling loops with Arduino Serial
Reply #4 - Nov 29th, 2009, 7:22am
 
OMG! I didn't know that processing had a function like this one.
Really, really thank you, JR! You've saved my college project!

It's working simply perfect. Thanks again.
Re: Controling loops with Arduino Serial
Reply #5 - Nov 29th, 2009, 8:37am
 
tell us a bit more about your project when its done. Maybe post it in the exhibition section. Sounds interesting
Re: Controling loops with Arduino Serial
Reply #6 - Dec 3rd, 2009, 7:01pm
 
Oh Gwarsh, sure!
I don't know if you've ever heard about Stephen Hobley, the one who did the Laser Harp with Arduino too?

We are making something much more like this.
But we're thinking about something to being used in discos, of someplace like this. It's like the laser harp, but not designed to make songs, when you hit the lasers it starts a loop song, and the others starts another loops and so on.

And with this you can make songs, but not just with a switcher, or mixer that you use buttons, etc. You choose the ones which you want to mix and play then just stopping the lights on the LDRs!

There will be a sonar (an ultrasound) attached with the LDRs and lasers that will be responsible to make changings in music during the play. Like making it loud, for example, or even making the song faster or slowly.

We will present the project next thursday or friday (december 10, 11), I'll make a video and post here some photos too.

Thanks for the support and the interest!!
Re: Controling loops with Arduino Serial
Reply #7 - Dec 3rd, 2009, 9:59pm
 
Sounds great. Waiting for the video. GOod luck with your presentation!
Page Index Toggle Pages: 1