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 & HelpSound,  Music Libraries › Synchronizing loops - Make they playing together
Page Index Toggle Pages: 1
Synchronizing loops - Make they playing together (Read 648 times)
Synchronizing loops - Make they playing together
Dec 3rd, 2009, 6:50pm
 
Hello Everyone! I'm facing some problems here while working on a college project using Arduino and Processing.

I'm using some loops, and when they were created, they were intended to being synchronized, starting together, and I don't know how to do this. Before anything, here is the code (ignore the comments, plz):

Arduino code
Code:
const int primeiroPin = 3;     // Numero do primeiro pino
const int segundoPin = 4; // Segundo Pino
const int terceiroPin = 5; // Terceiro Pino
const int quartoPin = 6; // Quarto Pino


void setup() {
// inicializa comunicacao serial:
Serial.begin(9600);
// Inicializa como input
pinMode(primeiroPin, INPUT);
pinMode(segundoPin, INPUT);
pinMode(terceiroPin, INPUT);
pinMode(quartoPin, INPUT);

}

void loop() {


// ------- PRIMEIRO PINO -------
if (digitalRead(primeiroPin) == HIGH) { // Quando a luz tiver fraca
Serial.print(10, BYTE); // Mande 10 pra serial
}
else if (digitalRead(primeiroPin) == LOW) {
Serial.print(1, BYTE); // Quando a luz tiver forte mande 1
}
delay(100); // OBS: 1000 = 1 sec


// ------- SEGUNDO PINO -------
if (digitalRead(segundoPin) == HIGH) { // Quando a luz tiver fraca
Serial.print(20, BYTE);
}
else if (digitalRead(segundoPin) == LOW) {
Serial.print(2, BYTE); // Quando a luz tiver forte
}
delay(100);


// ------- TERCEIRO PINO -------
if (digitalRead(terceiroPin) == HIGH) { // Quando a luz tiver fraca
Serial.print(30, BYTE);
}
else if (digitalRead(terceiroPin) == LOW) {
Serial.print(3, BYTE); // Quando a luz tiver forte
}
delay(100);


// ------- QUARTO PINO -------
if (digitalRead(quartoPin) == HIGH) {
Serial.print(40, BYTE);
}
else if (digitalRead(quartoPin) == LOW) {
Serial.print(4, BYTE);
}
delay(100);

}


Processing Code
Code:
//Importa Bibliotecas
import ddf.minim.*;
import processing.serial.*;

// Baseado no Simple Read do Processing


// Nao sei que porra eh esta
Serial portaArduino; // Cria a variavel baseado nas infos serial

// Inicializa Minim e cria AudioPlayers
Minim minim;
AudioPlayer pointer01;
AudioPlayer pointer02;
AudioPlayer pointer03;
AudioPlayer pointer04;

// Variavel pra armazenar dados da Serial
// Booleans para condicionais
// Tempo de execução
int val;
boolean p1 = true;
boolean p2 = true;
boolean p3 = true;
boolean p4 = true;
long prevTime1 = 0;
long prevTime2 = 0;
long prevTime3 = 0;
long prevTime4 = 0;

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

// Carregar um arquivo aqui fiote
pointer01 = minim.loadFile("pointer01_00.mp3", 1024);
pointer02 = minim.loadFile("pointer02_00.mp3", 1024);
pointer03 = minim.loadFile("pointer03_00.mp3", 1024);
pointer04 = minim.loadFile("pointer04_00.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, "COM10", 9600);

}

void draw() {
if (portaArduino.available() > 9) { // Se o dado esta disponivel,
val = portaArduino.read(); // leia esse valor e armazene em "val"
println(val);
}


switch(val) {

case 10:
if(millis() - prevTime1 > 1000)
{
if (p1){
pointer01.loop();
p1 = false;
}
else {
pointer01.pause();
pointer01.rewind();
p1 = true;

}
prevTime1 = millis();
}
break;


case 20:
if(millis() - prevTime2 > 1000)
{
if (p2){
pointer02.loop();
p2 = false;
}
else {
pointer02.pause();
pointer02.rewind();
p2 = true;
}
prevTime2 = millis();
}
break;

case 30:
if(millis() - prevTime3 > 1000)
{
if (p3){
pointer03.loop();
p3 = false;
}
else {
pointer03.pause();
pointer03.rewind();
p3 = true;
}
prevTime3 = millis();
}
break;

case 40:
if(millis() - prevTime4 > 1000)
{
if (p4){
pointer04.loop();
p4 = false;
}
else {
pointer04.pause();
pointer04.rewind();
p4 = true;
}
prevTime4 = millis();
}
break;
}
}

void stop()
{
pointer01.close();
pointer02.close();
pointer03.close();
pointer04.close();
// always close Minim audio classes when you are done with them
minim.stop();
super.stop();
}


I had a previous problem to create a 1-sec pause while the information on the serial is being read.  That's why there's a function using the millis() on the switch case.

That's how it should works:

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

:: Step 4 (Here's the problem)
If a person stops the light on another LDR, anothe loop starts, but this time is not synchronized with the first loop on step 1. Supose you hit the LDR while the first loop is playing in about 2,5sec. The loops have a duration of 4 seconds, what I want is make they play together, synchronized.

Everything should be like: "Just start the second loop when the one that is playing right now re-start the loop that is being repeated". I don't know if minim library has something like this to control the loop and the time or if this is possible, well...

All yeah, and there're FOUR loops on the switch case. All of them have 4-sec duration, and surely, all of then must be synchronized.

I'll be waiting for some help. Thanks everyone. =)
Page Index Toggle Pages: 1