Hello there,
I'm trying to build a guitar hero-esque game using the arduino and processing, basically the arduino outputs viibration or lights to the buttons that have to be pressed, and if they're pressed correctly, it sends a '1' to the serial and if its wrong, it sends a '0' .
After a lot of struggle I managed to get processing to read the string , convert it to a float and check for whether its 1 or 0, this controls whether the volume of the music is high or low, signifying success and failure . but when i run it i get a very long string of just 1's for a very long time , and then the same for 0s.
In the arduino serial monitor, these values update quite accurately. how do i get the same accuracy with processing?
Arduino code:
#include "eott.h"
boolean Button1flag= LOW; // these are flags... I don't know why these are here
boolean Button2flag= LOW;
boolean Button3flag= LOW;
int Buttonstate1 ; // these are the button states, read from the arduino
int Buttonstate2 ;
int Buttonstate3 ;
int A=0;
int B=0;
int C=0;
int i=0; // i will drive the array row change
/* this is the array that would correspond to the song, a poor substitute for a MIDI file but simple. every row is a 25 ms block of the song.
This is a test , ideally the array would be stored outside the code so I can call different 'songs'*/
long Delay= 137.6121539054329; //this is the delay, and the duration for which each loop will run
long t=0; // millis wil keep track of the time
void setup() {
// First define all the inputs and outputs
pinMode(11,INPUT); // Pins 2,3,4 are the inputs where the buttons will be connected
pinMode(12,INPUT);
pinMode(13,INPUT);
pinMode(2,OUTPUT); // Pins 11,12,13 are the outputs where the vibration motors will be connected (and mounted to the vibrators)
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(8,OUTPUT);
Serial.begin(115200); // start the serial code at 9600 bpa
}
void loop() {
if (i<=167)
{
i++;
}
else {i=0;}//{while(1);}
A= array[i][0];
B= array[i][1];
C= array[i][2];
digitalWrite(2,A);
digitalWrite(3,B);
digitalWrite(4,C);
digitalWrite(8,LOW);
// put your main code here, to run repeatedly:
while (millis()<=t+Delay) // run the loop while the current value of millis is less than the sum of the previous time and the delay value
{
Buttonstate1 = digitalRead(11); // these are the button states, read from the arduino
Buttonstate2 = digitalRead(12);
Buttonstate3 = digitalRead(13);
if (Buttonstate1==A) //first condition: is button1 equal to the corresponding array value?
{if (Buttonstate2==B) // second condition
{if (Buttonstate3==C) // basically if all three conditions are not met, the output is false
{
Serial.println("1"); // send a signal to processing to increase the volume of the song to signify success
digitalWrite(8,HIGH);
}
}
}
else {
Serial.println("0"); // this will tell processing to reduce the volume of the song, signifying failure.
digitalWrite(8, LOW);
}
}
t=millis();
/*
Serial.print(A);{0,0,0},
Serial.print("\t");
Serial.print(B);
Serial.print("\t");
Serial.print(C);
Serial.print("\t");
*/
}
Processing code:
/**
* This sketch demonstrates how to play a file with Minim using an AudioPlayer.
* It's also a good example of how to draw the waveform of the audio. Full documentation
* for AudioPlayer can be found at http://code.compartmental.net/minim/audioplayer_class_audioplayer.html
* <p>
* For more information about Minim and additional features,
* visit http://code.compartmental.net/minim/
*/
import processing.serial.*;
import ddf.minim.*;
Serial myPort;
String val;
float a;
Minim minim;
AudioPlayer player;
void setup()
{
myPort = new Serial(this, "COM3", 115200);
size(512, 200, P3D);
// we pass this to Minim so that it can load files from the data directory
minim = new Minim(this);
// loadFile will look in all the same places as loadImage does.
// this means you can find files that are in the data folder and the
// sketch folder. you can also pass an absolute path, or a URL.
player = minim.loadFile("Highway to Hell.wav");
}
void draw()
{
if ( myPort.available() > 0)
{ // If data is available,
val = (myPort.readStringUntil('\n'));
if (val != null) {
val =trim(val);
a = float(val);
println(a);
}
}
if(a==1)
{
player.setGain(0);
}
else if(a==0)
{
player.setGain(-30);
}
//println(val);
if (mousePressed == true)
{ //if we clicked in the window
myPort.write('1'); //send a 1
println("1");
} else
{ //otherwise
myPort.write('0'); //send a 0
}
background(0);
stroke(255);
// draw the waveforms
// the values returned by left.get() and right.get() will be between -1 and 1,
// so we need to scale them up to see the waveform
// note that if the file is MONO, left.get() and right.get() will return the same value
for(int i = 0; i < player.bufferSize() - 1; i++)
{
float x1 = map( i, 0, player.bufferSize(), 0, width );
float x2 = map( i+1, 0, player.bufferSize(), 0, width );
line( x1, 50 + player.left.get(i)*50, x2, 50 + player.left.get(i+1)*50 );
line( x1, 150 + player.right.get(i)*50, x2, 150 + player.right.get(i+1)*50 );
}
// draw a line to show where in the song playback is currently located
float posx = map(player.position(), 0, player.length(), 0, width);
stroke(0,200,0);
line(posx, 0, posx, height);
if ( player.isPlaying() )
{
text("Press any key to pause playback.", 10, 20 );
}
else
{
text("Press any key to start playback.", 10, 20 );
}
if (keyPressed)
{
{if(key == 'b')
if ( player.isPlaying() )
{
player.pause();
}
// if the player is at the end of the file,
// we have to rewind it before telling it to play again
else if ( player.position() == player.length() )
{
player.rewind();
player.play();
}
else
{
player.play();
}
}}}
I'm sorry about how ugly the processing code is, I've been trying a lot of different combinations of snippets of code to try and make it work
Thank you in advance for any help you can offer!