#Nyan Board by @Radomir Dopieralski
[UPDATE] Video and code added
So this should be an easy one, right? Oh how I failed. I'm still not getting sounds out of it, after I played around with libraries and different Versions of arduino. It's always tones and timers that get me.
I first thought the pads next to the attiny45/85 where a pullup resistor - d'oh. Of course it didn't do anything. Now it at least blinks.
To fit on an attiny45 I had to change the code a bit.
this code works, see the comments:
const int LENGTH = 27;
const int NOTES[] PROGMEM = {
65, 67, 70, 67, 74, 74, 72, 65, 67, 70, 67, 72, 72, 70, 69, 67, 65, 67, 70,
67, 70, 72, 69, 65, 65, 72, 70
};
const int DURATIONS[] PROGMEM = {
25, 25, 25, 25, 75, 75, 15, 25, 25, 25, 25, 75,
75, 75, 25, 5, 25, 25, 25, 25, 100, 5, 75, 25,
100, 5, 100, 200
};
void play() {
float bps = (60.0 / 120) * 1000.0;
for (int i = 0; i < LENGTH; ++i) {
int note = pgm_read_word_near(NOTES + i);
int duration = pgm_read_word_near(DURATIONS + i);
int hertz = (int) (pow(2.0, ((note - 69.0) / 12.0)) * 440);
tone(4, hertz, (long) (duration * bps / 100) - 10);
delay((long) (duration * bps / 100));
toggle_eyes();
}
}
void toggle_eyes() {
PORTB ^= ( 1 << PB3 );
}
void setup() {
DDRB = ( 1 << PB3 );
DDRB = ( 1 << PB4 );
}
void loop() {
play();
}
#include "pitches.h"
#include <avr/pgmspace.h>
const int FREQ[] = {
REST, NOTE_AS4, NOTE_AS5, NOTE_B4, NOTE_B5, NOTE_CS5, NOTE_CS6, NOTE_D5,
NOTE_DS4, NOTE_DS5, NOTE_DS6, NOTE_E4, NOTE_E5, NOTE_E6, NOTE_FS4,
NOTE_FS5, NOTE_GS4, NOTE_GS5,
};
const signed char INTRO_NOTES[] PROGMEM = {
9, 12, -15, -4, 9, 12, 15, 4, 6, 10, 6, 2, -4, -15, 9, 12, -15, -4, 6, 2,
4, 6, 13, 10, 13, 4, 0
};
const signed char MELODY_NOTES[] PROGMEM = {
-15, -17, 9, 9, 0, 3, 7, 5, 3, 0, -3, -5, -7, 7, 5, 3, 5, 9, 15, 17, 9, 15, 5,
9, 3, 5, 3, -9, -15, 17, 9, 15, 5, 9, 3, 7, 9, 7, 5, 3, 5, -7, 3, 5, 9, 15, 5,
9, 5, 3, -5, -3, -5, -15, -17, 9, 9, 0, 3, 7, 5, 3, 0, -3, -5, -7, 7, 5, 3, 5,
9, 15, 17, 9, 15, 5, 9, 3, 5, 3, -9, -15, 17, 9, 15, 5, 9, 3, 7, 9, 7, 5, 3, 5,
-7, 3, 5, 9, 15, 5, 9, 5, 3, -5, -3, -5, -3, 14, 16, -3, 14, 16, 3, 5, 9, 3,
12, 9, 12, 15, -3, -3, 14, 16, 3, 14, 12, 9, 5, 3, 14, 8, 11, 14, -3, 14, 16,
-3, 14, 16, 3, 3, 5, 9, 3, 14, 16, 14, -3, 3, 1, 3, 14, 16, 11, 12, 9, 12, 15,
-3, -1, -3, 14, 16, -3, 14, 16, 3, 5, 9, 3, 12, 9, 12, 15, -3, -3, 14, 16, 3,
14, 12, 9, 5, 3, 14, 8, 11, 14, -3, 14, 16, -3, 14, 16, 3, 3, 5, 9, 3, 14, 16,
14, -3, 3, 1, 3, 14, 16, 3, 12, 9, 12, 15, -3, -5
};
#define BEEP_PIN 4
#define LED_PIN 3
#define SPEED 1500
void toggle_eyes() {
static bool blink = false;
digitalWrite(LED_PIN, blink);
blink = !blink;
}
void play(const signed char *notes, const unsigned int length) {
for (unsigned int n = 0; n < length; n++) {
signed char f = pgm_read_word_near(notes+n);
int d;
if (f < 0) {
d = SPEED / 8;
f = -f;
} else {
d = SPEED / 16;
}
tone(BEEP_PIN, FREQ[f], d);
delay(1.3 * d);
noTone(BEEP_PIN);
toggle_eyes();
}
}
void setup() {
play(INTRO_NOTES, 26);
}
void loop() {
play(MELODY_NOTES, 216);
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
I updated the code at bitbucket to use progmem too: https://bitbucket.org/thesheep/nyan/src/tip/alternate/alternate.ino
Are you sure? yes | no
How about something like:
void play(const signed char *notes, const unsigned int length) {
for (unsigned int n = 0; n < length; n++) {
long now = millis();
int f = pgm_read_word_near(notes+n);
int d;
if (f < 0) {
d = (SPEED / 8) * 1.3;
f = -f;
} else {
d = (SPEED / 16) * 1.3;
}
tone(BEEP_PIN, FREQ[f], d);
while (millis() < now + d) {}
noTone(BEEP_PIN);
toggle_eyes();
}
}
Are you sure? yes | no
nopes, same result.
Are you sure? yes | no
The original core I used was https://code.google.com/p/arduino-tiny/ at 1Mhz. You are not getting any sound at all, or just distorted sounds?
Are you sure? yes | no
I finally got sound! I've imported the arduino-tiny stuff into my sketch folder and it works on an attiny45 with arduino 1.6.5! Have you changed the song to Rick Astleys "Never gonna give you up"?
Are you sure? yes | no
Maybe ;)
Try this for the original one: https://bitbucket.org/thesheep/nyan/src/tip/alternate/alternate.ino
Are you sure? yes | no
it's recognizable, but I have some bumps in the song.
Are you sure? yes | no
Hmm, interesting, I wonder what that may be. Note how the eyes also blink when it stutters, so it's not just the tone generator -- it looks like the whole chip is pausing. Weird. Maybe the delay() function has a bug?
Are you sure? yes | no
I changed the program a bit to fit on an attiny45 - I've put the arrays to progmem. Maybe it's not reading fast enough? I'll post the code tomorrow, maybe you could check it on your attiny85.
Are you sure? yes | no
@Radomir Dopieralski - I've added the code on the bottom.
Are you sure? yes | no
That may be. How about you read the next note into a variable before doing the delay()?
Are you sure? yes | no
Wait, doesn't it get stuck on the negative notes?
Maybe try making f a signed char, not an int?
Are you sure? yes | no
!!! that's it !!!
Are you sure? yes | no