Hi all! I'm working on a project where I'm supposed to send an array of notes from Processing to Arduino, then play them on a buzzer. I have the Processing side figured out, so that I can send the array over (of frequencies, so numbers ranging from ~250 to ~1000). First I send over the number of notes there are in the array, then the array itself. But I'm quite stuck on what to do from there.
I don't know how to set it so that the incoming ints from the serial port are added to a new array, where the array is the size of whatever that initial number is. Once I can figure out how to do that, I can probably figure out how to play it, but if anyone has some tips on that, I'd be much appreciated. I'm running Processing 2.xx beta, and Arduino 1.5.1, on a Uno R3. Thanks!
Oh I'm so sorry, I thought I had pasted it in. I'm really tired...
The code I have so far just establishes a connection, and I don't really know how to continue from there. My apologies, I'm very new to programming an Arduino. This is cobbled together from a few places. Thanks!
const int buzzerPin = 9;
boolean firstContact = false;
int inByte = 0;
int serialCount;
void setup()
{
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
establishContact();
}
void loop() {
int songLength = 'notesN';
//char notes[] = ;
if (Serial.available() > 0) {
inByte = Serial.read();
// servo1.write(inByte);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.write('A'); // send a capital A
delay(300);
}
}
void serialEvent() {
// read a byte from the serial port:
int inByte = Serial.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
Serial.flush(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
Serial.write('A'); // ask for more
}
}
else {
Serial.write('A');
serialCount = 0;
}
}
An "int" on an Arduino is usually two bytes wide.
'notesN' is six bytes wide, and probably not what you intended, but what your intent was is unclear to me.