PDA

View Full Version : Arduino Stall warning audio...i need help



MLRUSSELL
06-01-2013, 06:45 PM
i am new to programming and i am teaching myself. i am trying to get a speaker to play a specific tone/note when Link2fs in out reads Stall Warning "HIGH". The code that i have written so far sound as if its reading links2fs info but not playing note/tone; i hear a low noise tic almost like when u use a battery on a speaker.
here is my code:

int speakerPin = 9;
int j1;
int length = 2; // the number of notes
char notes[] = "C C "; // a space represents a rest
int beats[] = { 3, 3 };
int tempo = 300;


String StallW, StallWOld;


void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
if (Serial.available() > 0) {
j1 = Serial.read();
if(j1 == 'L') {
delay(11);
StallW = "";
StallW += (char)Serial.read();
if(StallW != StallWOld){
if(StallW == "1")
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
if(StallW == "0")
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
StallWOld = StallW;
}
}
}
}


void playNote(char note, int duration) {
char names[] = { 'C', 'C' };
int tones[] = { 956, 956 };
// play tone corresponding to the note name
for (int i = 0; i < 2; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}


void setup() {
Serial.begin(115200);
{
pinMode(speakerPin, OUTPUT);

Serial.flush();
}
}


void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2); }
}



here is where i got the idea to use note/tone to play when stall warning HIGH.(UPLOAD THIS CODE TO TEST THE SOUND WITH SPEAKER/PIEZO ATTACH TO PIN 9 AND GROUND)
int speakerPin = 9;


int length = 2; // the number of notes
char notes[] = "C C "; // a space represents a rest
int beats[] = { 3, 3 };
int tempo = 300;


void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}


void playNote(char note, int duration) {
char names[] = { 'C', 'C' };
int tones[] = { 956, 956 };
// play tone corresponding to the note name
for (int i = 0; i < 2; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}


void setup() {
pinMode(speakerPin, OUTPUT);
}


void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2); }
}

Jim NZ
06-03-2013, 04:50 AM
I'm terrible at fiddling with some-one else's code but after quick look ,, one thing that jumped out at me was the serial com.s stuff was in the wrong place.
It should be in the "void loop" as that's the 'main' loop that everything happens from.
So have a shuffle around with that and see if you can't make it happen.
I could be wrong though as I haven't solidly studied the code.

Another thing to think about is with Arduino doing all the "oscillating" for the speaker ,,, is ,, while it's doing that, it's not doing other stuff. That's fine though as long as the cards not going to be busy doing other stuff.

Cheers ,,, Jim

MLRUSSELL
06-03-2013, 09:13 PM
i have tried it before in void loop, and it didnt work. i will give it a next try. maybe it was the way i rearranged it before. thanks for your suggestion i will keep you posted and let u know if it works....
i am updating my falcon 7x flight sim. BIG THANKS to the softwares on ur webpage it made most things possible to simulate and now i dont have to buy expensive sim hardware. i can now make everything completely from scratch... :D794079417942

thats what my sim looks like now going update MCP, glareshield, overhead, radiostack, and add an annuciator panel with ur software.

cheers and thanks alot ....Mario

MLRUSSELL
06-04-2013, 01:19 AM
hey jim
good news i got it to work.....bbbbuuuuuuuuttttt!!!!! the audio does not play for the duration of the stall in fsx. its plays when aircraft stall but only up to the calculated point in the code then it stops..... im at the point where i am trying to figure out how to keep audio signal for duration of the stall.

int speakerPin = 9;
int j1;
int length = 1; // the number of notes
char notes[] = "C"; // a space represents a rest
int beats[] = { 9 };
int tempo = 300;
int i;




String StallW, StallWOld;


void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}


void playNote(char note, int duration) {
char names[] = { 'C' };
int tones[] = { 956 };
// play tone corresponding to the note name
for (int i = 0; i < 1; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}


void setup() {
Serial.begin(115200);
{
pinMode(speakerPin, OUTPUT);


}
}
void loop() {
if (Serial.available() > 0) {
j1 = Serial.read();
if(j1 == 'L') {
delay(11);
StallW = "";
StallW += (char)Serial.read();
if(StallW != StallWOld){
if(StallW == "1"){
playNote(notes[i], beats[i] * tempo);
delay(11);

if(StallW == "0")
digitalWrite(speakerPin, LOW);
delay(11);
}
StallWOld = StallW;
}
}
}
}


i know so far that "beats[i] * tempo" determines the duration after audio signal is triggered. i have tried multiple ways to change duration none seem to work yet. i will keep on trying until i figure it out. next step i will try is to remove "int duration" out of the code.

MLRUSSELL
06-11-2013, 12:43 AM
all problems are solved i have rewritten the program, much more simple than above codes and it works excellent... :D