Ciao ragazzi, avrei bisogno di generare più onde quadre e inviarle contemporaneamente a diverse uscite digitali di arduino.
Sono riuscito a generare 2 onde e a farle uscire a turno prima da un'uscita e poi dall'altra
[code]/* Melody
* (cleft) 2005 D. Cuartielles for K3
*
* This example uses a piezo speaker to play melodies. It sends
* a square wave of the appropriate frequency to the piezo, generating
* the corresponding tone.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = period / 2 = 1 / (2 * toneFrequency)
*
* where the different tones are described as in the table:
*
* note frequency period timeHigh
* c 261 Hz 3830 1915
* d 294 Hz 3400 1700
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* g 392 Hz 2550 1275
* a 440 Hz 2272 1136
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
*
* http://www.arduino.cc/en/Tutorial/Melody
*/
int speakerPin = 10;
int speakerPin2 = 9;
int length = 1; // the number of notes
int length2 = 1; // the number of notes
char notes[] = "d"; // a space represents a rest
char notes2[] = "c"; // a space represents a rest
int beats[] = {1};
int beats2[] = {1};
int tempo = 10000;
int tempo2 = 10000;
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 playTone2(int tone2, int duration2) {
for (long i = 0; i < duration2 * 1000L; i += tone2 * 2) {
digitalWrite(speakerPin2, HIGH);
delayMicroseconds(tone2);
digitalWrite(speakerPin2, LOW);
delayMicroseconds(tone2);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 2; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void playNote2(char note2, int duration2) {
char names2[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones2[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 2; i++) {
if (names2[i] == note2) {
playTone2(tones2[i], duration2);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(speakerPin2, 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);
}if (notes2[i] == ' ') {
delay(beats2[i] * tempo2); // rest
} else {
playNote2(notes2[i], beats2[i] * tempo2);
}
}
}
o sono riuscito a far uscire 2 onde dalla stessa uscita
/* Melody
* (cleft) 2005 D. Cuartielles for K3
*
* This example uses a piezo speaker to play melodies. It sends
* a square wave of the appropriate frequency to the piezo, generating
* the corresponding tone.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = period / 2 = 1 / (2 * toneFrequency)
*
* where the different tones are described as in the table:
*
* note frequency period timeHigh
* c 261 Hz 3830 1915
* d 294 Hz 3400 1700
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* g 392 Hz 2550 1275
* a 440 Hz 2272 1136
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
*
* http://www.arduino.cc/en/Tutorial/Melody
*/
int speakerPin = 10;
int speakerPin2 = 9;
int length = 1; // the number of notes
int length2 = 1; // the number of notes
char notes[] = "c"; // a space represents a rest
char notes2[] = "g"; // a space represents a rest
int beats[] = {1};
int beats2[] = {1};
int tempo = 10000;
int tempo2 = 10000;
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);
digitalWrite(speakerPin2, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin2, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 2; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void playNote2(char note2, int duration2) {
char names2[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones2[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 2; i++) {
if (names2[i] == note2) {
playTone(tones2[i], duration2);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(speakerPin2, 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);
playNote2(notes2[i], beats2[i] * tempo2);
}
}
}
qulcuno potrebbe aiutarmi?
R.