Arduino melody note values

Hello everybody. For my project I am using the public domain code found here on arduino.cc. To program an attiny85 to play music. My general question is how to interpret the int beats[] values. I have a feeling that 1 means a quarter note and 2 is a half note then 4 is a whole note. Then how can I Make an 8th note or sixteenth note? I tried using decimals but it reads them as a quarter note as well. Any help will be appreciated.

//twinkle twinkle little star
int speakerPin = 9;

int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names == note) {
_ playTone(tones*, duration);_
_
}_
_
}_
_
}_
void setup() {
_
pinMode(speakerPin, OUTPUT);_
_
}_
void loop() {
_
for (int i = 0; i < length; i++) {_
_ if (notes == ' ') {_
delay(beats _ tempo); // rest_

* } else {*
playNote(notes, beats * tempo);
* }*
* // pause between notes*
* delay(tempo / 2);*
* }*
}

It would help considerably if you read and followed the advice in the stickies of this forum on how to post code. As it is, half or more of your code is in italics because of an array index using i triggering italics mode. Using code tags prevents the code being mangled and also presents it in a scrolling window in a fixed font.

Pay some attention to YOUR code. Where are the values in the beats array used? I imagine that you can see how to change what 1 means (quarter note) to something else (eighth note) by simply changing the scale factor applied to the numbers in the array.

If not, this is not the project for you.

Thank you PaulS for the advice although I do wish it sounded a little less condescending. I am new to programming and I even purchased the arduino book. Asking for help online is not easy. But nevertheless I will look into your advice. Thank you.

Bob and Paul are two regulars here and have vast experience. Paul wasn't trying to be condescending, it's just that after 44 thousand posts, he gets a little miffed when people post before they read the first two information posts at the top of the forum.

If you look at the message icon list, the sharp sign (#) is used to post code. While your code is in the Arduino IDE, use the Ctrl-T feature to give it a standard C formatting look before you copy-and-paste into your message.

As to why the decimal values don't work, it's probably because decimal values are truncated (not rounded) when you try to assign float (i.e., decimal) values into an int data type.

Also, a more portable way of controlling loops when working with arrays is shown below. Instead of hard-coding the size of the array as you did with int length = 15, try the following:

#define ArrayLength(x) (sizeof(x)/sizeof(x[0]))
// ...a bunch of your code...
// Now, where you used this before:
//  for (int i = 0; i < length; i++) {, try:

for (int i = 0; i < ArrayLength(notes); i++) {

The advantage of this construct is that, if you change the number of elements in the notes[] array, you don't have to remember to change length, the ArrayLength() preprocessor directive does it automatically for you. There are still some other issues with the code, but keep trying.

Out of curiosity, which book are you using to learn C?

The book is titled "getting started with arduino" by massimo banzi. 2nd edition.

metalhead8711:
The book is titled "getting started with arduino" by massimo banzi. 2nd edition.

While that book is OK for the Arduino-specific stuff, it leaves a lot to be desired as a "hot to program in C/C++" book.