Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use Device::SerialPort;
- if ($#ARGV + 1 != 2) {
- print "Usage: $0 port filename\n";
- print "Example: $0 /dev/ttyASM0 money.txt\n";
- exit 1;
- }
- my $file = $ARGV[0];
- my $dev = $ARGV[1];
- if (!-e $file || !-e $dev) {
- print "File or brain not found.";
- exit 1;
- }
- require "notes.pl";
- my $arduino = DeviceSerialPort->new($dev);
- $arduino->baudrate(9600);
- $arduino->databits(8);
- $arduino->parity("none");
- $arduino->stopbits(1);
- open NOTES, "$file";
- while (<NOTES>) {
- chomp; # no newline
- s/#.*//; # no comments
- s/^\s+//; # no leading white
- s/\s+$//; # no trailing white
- next unless length; # anything left?
- if ($_ =~ m/^TEMPO/) {
- my $tempo = split(/\s+/, $_, -1);
- } else {
- my @tone = split(/\s+/, $_);
- }
- my $note = $notes{$tone[0]};
- my $duration = $tone[1]*$tempo;
- print "Tempo is $tempo.";
- print "Playing $tone[0] (\@$note Hz) for $tone[1] units ($duration ms).";
- while ($note > 255) {
- $arduino->write(chr(255));
- $note -= 255;
- }
- $arduino->write($note);
- $arduino->write(";");
- sleep($duration);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement