Friday 27 May 2016

sound - Can you play Chords on Arduino with a single piezo speaker?


I need to be able to play two tones at once and i have the arduino uni with a small 8ohm piezo speaker. Is it possible to do so?



Sample Code


#define  c     3830    // 261 Hz 

int tone1 = 10; // digital pin10
int tone2 = 9 // digital pin9
void setup() {
pinMode(tone1, OUTPUT); // pin as output
pinMode(tone2, OUTPUT); // pin as output
}


void loop() {
playTone(1000,500,700)
}

void playTone(long duration, int freq, int freq2) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(tone1,HIGH);

digitalWrite(tone2,HIGH);
delayMicroseconds(period / 2);
digitalWrite(tone1,LOW);
digitalWrite(tone2,LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}


No comments:

Post a Comment

arduino - Can I use TI&#39;s cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...