Hello everyone. I have this code for an art project where it will take volume from a microphone (namely the Sparkfun Breakout Board for Electret Microphone SparkFun Electret Microphone Breakout - BOB-12758 - SparkFun Electronics), record it as volumes in the array recording[] and output it as LEDs flashing in random patterns every 200ms based on the volume. Here is my sketch: (sorry, it's not very well organised or efficient. It's also full of debug serial messages...)
int recording[50];
int micLevel;
int nomore;
int ending;
int LEDL;
int LEDS;
int avail[9];
int randy;
int pin;
int out;
int fix;
/* This program SHOULD wait until a certian mic level is reached, record the levels of speech for
* a maximum of 10 seconds, and then flash random LEDs at the same levels of the recording...
* But it doesn't at the moment... Why? Anyway:
* Writer: terrabyte_aura
* Project: Voice Your Opinion.
*/
void setup() {
pinMode(2, OUTPUT);
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
randomSeed(analogRead(0));
}
void loop() {
for(int x = 0; micLevel <= 360; x++) { //waits for mic to be at a good level (<=360)
micLevel = analogRead(1);
Serial.print("Mic @ ");
Serial.print(micLevel);
Serial.println(" Not loud enough... (if last view of message, ignore)");
delay(200);
}
for(int y = 0; y <= 50; y++) {
micLevel = analogRead(1);
recording[y] = micLevel; //records the level of the microphone in the array
Serial.print(" MicLevel: ");
Serial.println(micLevel);
if(micLevel <= 360) {
nomore = nomore + 1;
Serial.print("nomore = ");
Serial.print(nomore);
if(nomore == 10) {
fix = y;
y = 51; //if a low level is recorded for 2 seconds, it will terminate the recording
}
}
else {
nomore = 0;
Serial.print(" nomore = 0"); //resets the counter for low level...
}
delay(200);
Serial.print(" Fix @ ");
Serial.print(fix);
if(fix > 0) {
ending = fix;
Serial.println(" Recording End Reached");
fix = 0;
}
}
Serial.println("Playback..."); //the code doesn't excecute from the next line till the end. WHY???
for(int z = 0; z =! ending; z++) {
Serial.print(recording[z]);
LEDL = recording[z];
Serial.print(" Position:");
Serial.println(z);
if(LEDL <= 424) { //this takes the level at frame z and turns it into a number of LEDs
LEDS = 1;
}
if(LEDL > 424 && LEDL <= 486) {
LEDS = 2;
}
if(LEDL > 486 && LEDL <= 550) {
LEDS = 3;
}
if(LEDL > 550 && LEDL <= 614) {
LEDS = 4;
}
if(LEDL > 614 && LEDL <= 678) {
LEDS = 5;
}
if(LEDL > 678 && LEDL <= 742) {
LEDS = 6;
}
if(LEDL > 742 && LEDL <= 806) {
LEDS = 7;
}
if(LEDL > 806 && LEDL <= 870) {
LEDS = 8;
}
if(LEDL > 870) {
LEDS = 9;
}
Serial.print(LEDS);
Serial.println(" is the number of LEDS needed");
LEDS = 10 - LEDS;
for(int q; q =! LEDS; q++) {
randy = random(0, 10);
Serial.print(randy); //this chooses which LEDs will NOT be turned on...
Serial.println(" is the random number");
for(int zout; out =! 1; zout--) {
if(avail[randy] == 0) { //this checks to see if the number's already been chosen
randy = random(0, 10);
Serial.print("previous had already been chosen... new number: ");
Serial.println(randy);
}
else {
avail[randy] = 0;
Serial.print(randy);
Serial.println(" deactivated");
out = 1;
}
}
}
out = 0;
for(int p; p < 9; p++) {
pin = p + 2;
if(avail[p] == 0) { //this writes to the LEDs
digitalWrite(pin, LOW);
Serial.print("LOW- ");
Serial.println(pin);
avail[p] = 1;
}
else {
digitalWrite(pin, HIGH);
Serial.print("HIGH- ");
Serial.println(pin);
}
}
delay(200); //this delays the whole script
nomore = 0;
Serial.println("nomore reset");
}
}
However, it doesn't perform any of the code after the "Playback..." serial message. Here's a copy of the serial monitor:
Mic @ 332 Not loud enough... (if last view of message, ignore)
Mic @ 351 Not loud enough... (if last view of message, ignore)
Mic @ 326 Not loud enough... (if last view of message, ignore)
Mic @ 355 Not loud enough... (if last view of message, ignore)
Mic @ 330 Not loud enough... (if last view of message, ignore)
Mic @ 347 Not loud enough... (if last view of message, ignore)
Mic @ 320 Not loud enough... (if last view of message, ignore)
Mic @ 392 Not loud enough... (if last view of message, ignore)
MicLevel: 321
nomore = 1 Fix @ 0 MicLevel: 350
nomore = 2 Fix @ 0 MicLevel: 351
nomore = 3 Fix @ 0 MicLevel: 325
nomore = 4 Fix @ 0 MicLevel: 362
nomore = 0 Fix @ 0 MicLevel: 234
nomore = 1 Fix @ 0 MicLevel: 324
nomore = 2 Fix @ 0 MicLevel: 343
nomore = 3 Fix @ 0 MicLevel: 340
nomore = 4 Fix @ 0 MicLevel: 338
nomore = 5 Fix @ 0 MicLevel: 317
nomore = 6 Fix @ 0 MicLevel: 355
nomore = 7 Fix @ 0 MicLevel: 349
nomore = 8 Fix @ 0 MicLevel: 326
nomore = 9 Fix @ 0 MicLevel: 347
nomore = 10 Fix @ 14 Recording End Reached
Playback...
Mic @ 321 Not loud enough... (if last view of message, ignore)
...
...
...
I have a feeling I've made a silly noobish mistake...
Anyway, I hope that I've given you enough information to solve my problem and many thanks in advance!