![c++ – How do you utilize two piezos on the similar time? c++ – How do you utilize two piezos on the similar time?](https://techtoguide.com/wp-content/uploads/2023/10/Why-wont-my-servo-and-ultrasonic-sensor-work-together.png)
[ad_1]
I am making an attempt to copy a brief music clip with two piezos, and I would like them to play on the similar time. Whereas the fitting piezo is taking part in, I would like the left one to nonetheless be looping. The issue is, I am unsure how.
//Left hand
const int leftPin = 10; /Left pin
const int leftTones[] = {123.4, 103.83, 138.59, 92.50}; //The looping left tones
const int leftDelays[] = {700, 1300, 900, 900}; //How lengthy these tones final
const int leftNotes = 4; //What number of left notes there are
//Proper hand
const int rightPin = 2; //Proper pin
const int rightTones[] = {739.99, 587.33, 587.33, 659.25, 698.46, //The looping proper tones
659.25, 587.33, 554.37, 587.33, 659.25, 739.99, 987.77, 493.88,
554.37, 587.33, 659.25, 587.33, 554.37, 880.00, 783.99, 739.99,
587.33, 587.33, 659.25, 698.46, 659.25, 587.33, 554.37, 587.33,
659.25, 739.99, 987.77, 987.77, 1108.73, 1174.66, 783.99, 739.99,
698.46, 1174.66, 1318.51};
const int rightDelays[] = {700, 1000, 100, 100, 300, 300, 200, //How lengthy these tones final
300, 300, 200, 700, 700, 250, 150, 200, 400, 300, 300, 400, 200,
700, 1000, 100, 100, 300, 300, 200, 400, 300, 200, 700, 700, 300,
200, 400, 300, 200, 400, 300, 200};
const int rightNotes = 40; //What number of proper notes there are
void setup(){
pinMode(leftPin, OUTPUT);
pinMode(rightPin, OUTPUT);
delay(1000);
}
void loop(){
for (int i = 0; i < leftNotes; i++){
tone(leftPin, leftTones[i]);
delay(leftDelays[i]);
noTone(leftPin);
}
for (int i = 0; i < rightNotes; i++){
tone(rightPin, rightTones[i]);
delay(rightDelays[i]);
noTone(rightPin);
}
}
With this code, the left piezo performs first, after which the fitting piezo performs. It must be on the similar time.
Edited Code:
lengthy currentMillis = 0;
lengthy leftPreviousMillis = 0; // earlier time for left hand
lengthy rightPreviousMillis = 0; // earlier time for proper hand
const int leftNotes = 4; // The quantity of left notes
const int rightNotes = 40; // The quantity of proper notes
int leftIndex = 0; // The particular index for the left piezo
int rightIndex = 0; // The particular index for the fitting piezo
const int leftPin = 10; // The left piezo
const int rightPin = 2; // The fitting piezo
// The particular left pitches (as float)
const float leftTones[] = {123.4, 103.83, 138.59, 92.50};
// How lengthy the left pitches final for
const lengthy leftDelays[] = {700, 1300, 900, 900};
// The particular proper pitches (as float)
const float rightTones[] = {739.99, 587.33, 587.33, 659.25, 698.46, 659.25, 587.33, 554.37, 587.33, 659.25, 739.99, 987.77, 493.88, 554.37, 587.33, 659.25, 587.33, 554.37, 880.00, 783.99, 739.99, 587.33, 587.33, 659.25, 698.46, 659.25, 587.33, 554.37, 587.33, 659.25, 739.99, 987.77, 987.77, 1108.73, 1174.66, 783.99, 739.99, 698.46, 1174.66, 1318.51};
// How lengthy the fitting pitches final for
const lengthy rightDelays[] = {700, 1000, 100, 100, 300, 300, 200, 300, 300, 200, 700, 700, 250, 150, 200, 400, 300, 300, 400, 200, 700, 1000, 100, 100, 300, 300, 200, 400, 300, 200, 700, 700, 300, 200, 400, 300, 200, 400, 300, 200};
void setup() {
pinMode(leftPin, OUTPUT);
pinMode(rightPin, OUTPUT);
Serial.start(9600);
delay(1000);
tone(rightPin, rightTones[rightIndex]);
tone(leftPin, leftTones[leftIndex]);
}
void loop() {
currentMillis = millis();
// Left hand notes
if (currentMillis - leftPreviousMillis >= leftDelays[leftIndex]) {
noTone(leftPin); // Flip off the left be aware
leftPreviousMillis = currentMillis;
leftIndex++;
if (leftIndex == leftNotes) // Reset left hand index
leftIndex = 0;
tone(leftPin, leftTones[leftIndex]); // Activate the brand new left be aware
}
// Proper hand notes
if (currentMillis - rightPreviousMillis >= rightDelays[rightIndex]) {
noTone(rightPin); // Flip off the fitting be aware
rightPreviousMillis = currentMillis;
rightIndex++;
if (rightIndex == rightNotes) // Reset proper hand index
rightIndex = 0;
tone(rightPin, rightTones[rightIndex]); // Activate the brand new proper be aware
}
}
I’ve finished some testing, and I believe it is an issue with the tones operate. Proper now, this code solely performs the fitting hand notes. The issue is that this half:
tone(rightPin, rightTones[rightIndex]);
tone(leftPin, leftTones[leftIndex]);
After I do that:
tone(leftPin, leftTones[leftIndex]);
tone(rightPin, rightTones[rightIndex]);
Solely the left hand notes are heard. However, it isn’t excellent. There’s little beeps and buzzes every so often. This leaves me with the conclusion that if there are two tones on the similar time, it solely performs the one it reads first. I’ve examined it with less complicated code, and it stands true.
In order that begs the query, is it even attainable to make use of two piezos on the similar time with one Arduino? I am testing it with the Arduino simulator tinkercad, so perhaps that is the issue? My code works for the left piezo, and the fitting piezo. However whenever you put them collectively, every thing messes up.
[ad_2]