Espruino Star Wars (first steps)

Posted on
  • Hi guys!

    Just started playing with my brand new Espruino Pico boards and can't stand sharing this.
    I know it's very basic, but it's just.. so fun!!

    So here are the results of me and my friend's 2 hours of life. We are just starting, so don't judge hard.
    https://vimeo.com/127084624

    Enjoy,
    Victor

  • Begin with fun, resume with fun...

  • That's awesome! I'm a huge Starwars fan. I know this is late "May the Fourth be with you." :)

  • Love it! Thanks for posting it up :)

    I think this is actually the first time I've seen a project with images in it... Did you use the ImageMagick commands in the graphics page to get the images into Espruino, or did you do it some other way?

    Also, I don't know if it helps, but it's actually possible to get two separate pitches out of one speaker. Just connect each side of it to a PWM output (and make sure you do digitalWrite(pin,0) rather than digitalRead(pin) when you want to stop the sound).

    You can also output actual waveforms if you want to, even at once. I did a forum post on it, but I'm the most un-musical person ever so never managed to come up with two distinct music tracks to play at once :)

  • My 7 yr old will want me to build one too, Great Fun thanks for sharing!

  • Hi All,

    Thanks for positive feedback!

    Gordon,

    The first image was created just as bitmap buffer, then we found your ImageMagick tutorial and used it for creating other images. It worked very nice. The only thing is that we inverted colors in some images using '-negate' option.

    Also, thanks for your suggestion about 2 pitches from 1 speaker - we're looking forward into investigating this, maybe it's possible to create a quartet from 2 speakers?)) We'll look into using waveforms as well, it seems promising.

    It looks like our appetites are growing, so will keep you posted ;)

    Thanks,
    Victor

  • @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.

    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.

    Thanks,
    Victor

  • Really nice code!

    The wave thing, is it for play (back) of a recording made using waves?

    PS: BUZZER#s are variables too... it is good practice to define them as such (epecially when evetually putting the code into a module. Not adding the var, messes with the global name space. In this sample code not releveant because it is already in the global namespace). All upper case makes it clear that it is not really a var. For the pitches (PITCHES), the property names do not need to be quoted.

  • @allObjects,

    Thanks for pointing to missing 'var' statements, I just overlooked that! I'm not using JavaScript often, so tend to forget simple things.

    Yes, wave thing is for playing WaveForms. It's not used here as I didn't get it output something really interesting yet. I know it's somewhat confusing in this example, but I was planning to make a reusable module from Buzzer, which could play both WaveForm and simple pitches, so the code is mentioning waves.

    As for the PITCHES - this is a copy-paste from espruino examples. And too me it's more clear to have quotes there. There is no need to dig into JavaScript magic in this case :)

    Thanks for the feedback, it is really useful!

  • That's great! It's really nice to have a reusable bit of code that plays each instrument :)

    Strange about the sound - it seems that when the two notes interact it's a bit quieter?

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Espruino Star Wars (first steps)

Posted by Avatar for vickingur @vickingur

Actions