Ah good point, well I am trying to program a piezo buzzer through an interface in processing... this code does not include everything but illustrates the problem I am having with delay() and how I am using it.
The delay() function does not accept float values and therefore is always rounding to 1 or 0, forcing my piezo buzzer to only play one note regardless of the inputted frequency and duration.
**This code works in Arduino with the delayMicroseconds() function
import cc.arduino.*;
import processing.serial.*;
Arduino arduino;
int speakerOut = 11;
int melody[] = { 1912, 2028, 2550, 1912, 2028, 3038, 0, 1912, 3830, 2550, 2272, 1912 };
int beats[] = { 16, 16, 16, 8, 8, 16, 32, 16, 16, 16, 8, 8 };
int MAX_COUNT = 6; // Melody length, for looping.
// Set overall tempo
int tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100;
// Initialize core variables
int tone = 0;
int beat = 0;
int duration = 0;
// Do we want debugging on serial out? 1 for yes, 0 for no
int DEBUG = 1;
void setup () {
size(1200,600);
smooth();
frameRate(30);
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(speakerOut, Arduino.OUTPUT);
}
// PLAY TONE ==============================================
// Pulse the speaker to play a tone for a particular duration
void playTone() {
long elapsed_time = 0;
if (tone > 0) { // if this isn't a Rest beat, while the tone has
// played less long than 'duration', pulse speaker HIGH and LOW
while (elapsed_time < duration) {
arduino.digitalWrite(speakerOut,Arduino.HIGH);
delay(tone/2000);
//delayMicroseconds(tone / 2);
// DOWN
arduino.digitalWrite(speakerOut, Arduino.LOW);
delay(tone/2000);
//delayMicroseconds(tone / 2);
// Keep track of how long we pulsed
elapsed_time += (tone);
}
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delay(duration/1000);
}
}
}
void draw() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT; i++) {
tone = melody[i];
beat = beats[i];
duration = beat * tempo; // Set up timing
playTone();
// A pause between notes...
delay(pause/1000);
if (boolean(DEBUG)) { // If debugging, report loop, tone, beat, and duration
print(i);
print(":");
print(beat);
print(" ");
print(tone);
print(" ");
println(duration);
}
}
}