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 288 times)
Controling loops with Arduino Serial
Nov 27th, 2009, 6:13pm
 
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();
}
Page Index Toggle Pages: 1