Thanks for suggesion regarding connecting both sides of seaker to PWM outputs!
It works nice. We tried to play with the same tunes for Star Wars. It sounds very different now, but still very special, so we decided to share it with you again: https://vimeo.com/127748913
Not bad techno music for my taste :)
Also, this time I feel ready to share the code for this demo (still not perfect though), maybe it will help somebody.
BUZZER1 = B6;
BUZZER2 = B7;
BUZZER3 = B4;
BUZZER4 = B5;
function freq(buz, f) {
if (f===0) digitalWrite(buz,0);
else analogWrite(buz, 0.5, { freq: f } );
}
var pitches = {
'a':220.00,
'b':246.94,
'c':261.63,
'd':293.66,
'e':329.63,
'f':349.23,
'g':392.00,
'A':440.00,
'B':493.88,
'C':523.25,
'D':587.33,
'E':659.26,
'F':698.46,
'G':783.99,
'H':830.60
};
function Buzzer(pin, tune, led, wave) {
this.pin = pin;
this.tune = tune;
this.led = led;
this.pos = 0;
this.wave = wave;
}
Buzzer.prototype.reset = function() {
this.pos = 0;
};
Buzzer.prototype.step = function() {
var ch = this.tune[this.pos];
if (ch !== undefined) {
this.pos++;
print(this.pin+","+ch);
}
if (ch in pitches) {
if(this.wave!==undefined) {
wave(this.pin, this.wave, pitches[ch]);
} else {
freq(this.pin, pitches[ch]);
}
digitalWrite(this.led, 1);
}
else {
if(this.wave!==undefined) {
wave(this.pin, this.wave, 0); //off
} else {
freq(this.pin, 0); //off
}
digitalWrite(this.led, 0);
}
};
// . . . . . . . . . . . . . . . "
var tune1 = "AA AA AA ff CAA ff CAAAA EE EE EE HH ECC ff CAAAA";
var tune2 = "a a a a a a a a a a a a f f g g a a a a f f g g a a a a a a a a e e e e e e e e e e e e g g f f c c c c f f g g a a a a aaaaaaaa";
var tempo = 150;
var tempo1 = 2*tempo;
var tempo2 = tempo;
var tempo3 = tempo;
var tempo4 = 2*tempo;
var buzzer1 = new Buzzer(BUZZER1, tune1, LED1, w1);
var buzzer2 = new Buzzer(BUZZER2, tune2, LED2, w2);
var buzzer3 = new Buzzer(BUZZER3, tune1, B15, w1);
var buzzer4 = new Buzzer(BUZZER4, tune2, B14, w2);
function resetTune() {
buzzer1.reset();
buzzer2.reset();
buzzer3.reset();
buzzer4.reset();
}
function initAudioAndLED() {
setInterval( function() { buzzer1.step();}, tempo1);
setInterval( function() { buzzer2.step();}, tempo2);
setInterval( function() { buzzer3.step();}, tempo3);
setInterval( function() { buzzer4.step();}, tempo4);
var resetInterval = Math.max(tune1.length*tempo1, tune2.length*tempo2);
setInterval(resetTune, resetInterval);
}
function onInit() {
initAudioAndLED();
}
onInit();
// save();
The result was achieved pretty quickly, making me confident we can play even better tunes in future.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
@Gordon,
Thanks for suggesion regarding connecting both sides of seaker to PWM outputs!
It works nice. We tried to play with the same tunes for Star Wars. It sounds very different now, but still very special, so we decided to share it with you again:
https://vimeo.com/127748913
Not bad techno music for my taste :)
Also, this time I feel ready to share the code for this demo (still not perfect though), maybe it will help somebody.
The result was achieved pretty quickly, making me confident we can play even better tunes in future.
Thanks,
Victor