Review: PiFace Digital – now with Musical Relays!

You didn’t think I’d end my PiFace Digital review there did you? No. Of course not.

So here’s a bonus – musical relays. Everyone loves them. They wear out your relays, but they also give you some music without any added hardware. Feeling a bit patriotic, today it’s Advance Australia Fair.

The code (yes, it’s awful – I bodged this up in less than an hour, magic numbers everywhere …) is as follows:

#include <stdio.h>
#include <time.h>
#include <libpiface-1.0/pfio.h>

int noteperiod[] = {3822, 3405, 3034, 2863, 2551, 2272, 2024, 1911, 1702, 1517, 1431};
//                     C     D     E     F     G     A     B     c     d     e     f
//                     0     1     2     3     4     5     6     7     8     9    10
int songnote[] = {4, 7, 4, 2, 4, 7, 7, 7, 9, 8, 7, 6, 7, 8, -1, 4, 7, 4, 2, 0, 4, 4, 5, 9, 8, 7, 6, 5, 4, -1, 4, 5, 6, 7, 4, 4, 2, 2, 4, 5, 7, 10, 9, 8, -1, 4, 5, 6, 7, 5, 4, 7, 7, 8, 9, 7, 8, 6, 7, 9, 10, 9, 8, 7, 6, 5, 4, 7, 9, 7, 8, 6, 7, -1};
int songdura[] = {2, 2, 2, 2, 2, 3, 1, 2, 2, 2, 2, 2, 2, 4,  2, 2, 2, 2, 2, 2, 3, 1, 2, 2, 2, 2, 2, 2, 2,  4, 2, 3, 1, 2, 2, 3, 1, 2, 2, 2, 2,  2, 2, 2,  4, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 3, 1, 6, 2,  2, 2, 2, 2, 2, 2, 2, 2, 3, 1, 3, 1, 6, -1};

int i = 0;
int n = 0;
int o = 0;

int main (void) {
    pfio_init();
    while (!(songnote[i]==-1 && songdura[i]==-1)) {
    if(songnote[i]!=-1) {
      n = songdura[i]*125000/noteperiod[songnote[i]];
      while (n>0) {
        pfio_digital_write(0,o);
        o=~o;
        usleep(noteperiod[songnote[i]]);
        n--;
      }
    } else {
      usleep(songdura[i]*125000);
        }

    usleep(50000);
    i++;
    }
    pfio_digital_write(0,0);
    pfio_deinit();
    return (0);
}

About lui_gough

I'm a bit of a nut for electronics, computing, photography, radio, satellite and other technical hobbies. Click for more about me!
This entry was posted in Computing, Electronics, Raspberry Pi and tagged , , , . Bookmark the permalink.

2 Responses to Review: PiFace Digital – now with Musical Relays!

  1. bengrisdale says:

    It doesn’t work it says invalid syntax

    • lui_gough says:

      You need to provide more details? Where did the error occur? Are you sure you copied the code correctly, and the browser didn’t mangle it for you? What compiler options did you specify? Did you first install the PiFace libraries for C as described in the documentation? Are your library links updated by running sudo ldconfig?

      The code should work, unless they changed anything about the libraries recently.

      – Gough

Error: Comment is Missing!