As of this post Marvel has since announced there will be a new Fantastic Four movie in phase four of current MCU. This would make it the fourth official live-action FF movie. I say official, because there was an unreleased FF movie produced by Constantin Film in 94′. While that makes the total five movies, that’s still four movies that introduce us to the team. It also makes it the third reboot. Since “Third times the charm”. What a minute…
That makes:
A phase 4 film
4th official release
4th intro to FF-verse
3rd reboot
Four coincidences? This film may blessed for success.
Much like the creative days prior, I just daydreamed ideas. How could I use the parts and methods from yesterday’s circuit into Mokujin. Can definitely use encoders in the build, but the 7-segment might not be the display I want to roll with if I do implement one. A small OLED came with the kit. Graphically, it will provide a cleaner read, the dimensions might not scale comfortably to the final design.
The mission worked off the circuit from day 18 and was the start of getting the flight systems up and running. This mission focused on using the encoder from the last mission, the secret code that was found on day 17, what was learned with the 7-segment display from 16, and using the passive buzzer from day 12. The instructions were a bit limited as they implied that communication security had been compromised. The circuit needed a minor tweak in the form of adding the buzzer, but it was decrypting the code from day 17 that was the important part of getting today’s code to work. When programmed properly, the circuit should produce three distinct sounds when the encoder is set to the right values and unlock the flight controls. Interrupt Service Routine (ISR) was used again in this code, so I must read up on what that means again because I still don’t understand the concept (and because I’m writing this roughly a month after I finished the course and post 18.). According to Wikipedia, the general description of an interrupt handler is:
“An interrupt handler, also known as an interrupt service routine (ISR), is a callback subroutine in an operating system or device driver whose execution is triggered by the reception of an interrupt. Interrupt handlers have a multitude of functions, which vary based on the reason the interrupt was generated and the speed at which the Interrupt Handler completes its task.”
I did this one live on stream. Mission 18 covered encoders, interrupts and the switch function(briefly) on the included rotary encoder. The A.I. mentioned two types of encoders, absolute and relative. But while looking for a clearer explanation for the post, I came to my attention that the A.I. probably meant “incremental” encoders instead of “relative”. You see, rotary encoders (according to this article by Digi-Key) are a solution for measuring the speed, direction of motion, or position of a rotating shaft. There are many different kinds, but for the sake of this post (and the fact that the kit only came with an incremental encoder) will just focus on absolute versus incremental encoders.
Absolute Encoders – More precises with lower latency, but more expensive
Incremental encoders – Cheaper but needs extra circuitry to track or count pulses that are generated as the shaft rotates. The output is usually two squares waves that are 90°
The encoder included with the kit is incremental and has the added feature of acting like a button when pressed in, but this feature wasn’t used for this project. The encoder has five markings on it.
CLK – Clock, an output measuring the state of rotation of the encoder.
DT – A secondary output that is used to measure the opposing state of rotation.
SW – Switch, this pin would be used if I wanted to incorporate the button feature of the relative encoder
(+) – +5 volt pin
GND – The (-) volt pin
An “interrupt” is a piece of code that takes an unanticipated event and has the software respond to that event.
ISR – Interrupt Service Routine, a block of code tied to a set of hardware/software interrupts, or software exceptions, and are used for device drivers or transitions between protected modes of operation, in this case turning the encoder.
I liked this one because at least with this I get a general picture of what’s going on under the hood with a handful of devices that have encoder elements. Mixers (for turntables and audio equipment), dials on editing bays, dashboards/control panels, dimmer switches, ect, I never really paid attention to how many times a dial shows up. The Mokujin Project can benefit from these concepts.
The code for this project was designed to count the rotations of of the encoder and display them on the 7-segment display. Every 25 rotations clockwise the display would flash briefly before going back to numbers. Once it hits “100” it will flash “d0nE”. The coding on this one needed to be tweaked ever so slightly as the provided code was registering rotations inversely. Clockwise was counter-clockwise and vice-versa. Nothing comes to mind project-wise when it comes to implementing this circuit/code. Not true, I could tweak it to act as more of a selector than a counter, using the encoder to switch between “modes” and using the 7-segment to display the currently selected mode. Coding for mode selection and the subsequent subsystems might be the hardest part.
This mission focused on cryptology. Cryptology is the study of codes, or the art of writing and solving them. Because communications have been compromised, the code for this mission was encrypted. I started the video for this one late 3/13/2023. I’m picking back up the following afternoon. I struggle to write the post for this one because most of the concepts and terms introduced in this mission, I’m familiar with. I want to write on what I don’t know. But I guess there’s a chance someone might read these trying to learn something (sorry about that), so I should at the very least just jot down the terms and definitions.
Encryption – The process of converting info or data into a code, especially to prevent unauthorized access.
Decryption – A process that transforms encrypted information into its original format.
Substitution Cypher – Encrypt the plain text by swapping each letter or symbol in the plain text by a different symbol as directed by the “KEY”
Caesar Cypher – The fuck boi of cyphers. Invited by Julius Caser, the Caesar cypher shifts the place of the letters a set number of places from there original position. So where, A=1 B=2 C=3, I could shift them over seven spots to the right, A=H B=I C=J. [REDACTED]
Public/Private Key Encryption – A method of encrypting or decrypting data with two different keys, one public and one private. The public key can be sheared with anyone and used to encrypt data, while the private key is kept secret and used to decrypt data. (Summarized by MS Bing)
Because of the nature of this mission I can’t really get into an explanation of the code beyond noting that the results of running it setup for a mission later in the course. As of writing this paragraph (3/19/2023), I can not go back to that mission. Attempts to do so just return me back to the mission select list. I’m not sure is that’s designed to add to the theme or a weird glitch. I don’t remember them saying anything like the message would self-destruct.
If you’re new here, WELCOME! Thanks for stopping by! If you’ve been following along, shut up. I don’t want to hear it. You knew what this was. Mission 16 made use of the 7-segment display. This is my first time working with this type of display, despite having similar units. The back of the display has four abbreviations:
CLK – Clock
DIO – Digital Output
GND – Ground ( – Power Supply)
VCC – Voltage Common Collector ( + Power Supply)
Never did know what VCC stood for. Neat. I knew “CLK” stands for “Clock”, but I didn’t know that it’s used to clock the binary data from the DIO pin and compiles it into a byte. (1 cycle = 1 byte) The digital pin receives data. Having window shopped at consumer electronics sites, I picked up on the display being called “7-Segement” due to the seven segments that make up the eight(I know, real shocker). But didn’t figure that I would have to code an array that looks like kinda SQL. Each segment is given a designation. Going clockwise and counting the middle segment last the segments are A, B, C, D, E, F, G.
So BC = 1, ABDEG = 2, and ABCDG = 3.
The coding for this one needs a library from the inventr.io downloads. Thought I had already installed it but didn’t. It wasn’t hard to find though since I download every library the first chance I got. The first four lines of code set the library, define the pins, and create an object that I’ll use to call the display. The two lines of code that follow are constant, single-row arrays that take [ 0xff ] & [ 0x00 ]. According to the comment, 0xff is a hexadecimal* number whose binary representation is all ones and 0x00 is all zeros. The next constant is an array that will print out “Done”. but because the is a 7-seg, it’ll come out looking like “d0nE”. So if I wanted to print “1994” on the display the code would look like this:
I’m going to have to go back and read the drafts in this series as I have lost track of what day I should currently be on. As of the time of writing (3/13/2023) I am currently starting day 16, “A fancy new display”. Day 13’s post was written on a Saturday after getting through that project’s video. I was supposed to go through 14 & 15 that same day but couldn’t manage the energy or focus. Sunday saw that I finally got to missions done, with day 14 adding an RGB LED to the circuit and adding more complexity to the code. And day 15 was a creative day, but other than going over the review of the previous mission’s concepts, I couldn’t even populate an idea based on what had been done. Day 14’s code added four custom functions that alter the color of the RGB LED and alter the tone of the buzzer based on key inputs. Like with day 13’s video, I replayed a few sections till I could focus enough to grasp the concepts but will rewatch the coding section again as a constant refresher. I’m seriously considering supplementing my coding with Codecademy refresher course, but we’ll see about that. Wouldn’t be any point in trying if I’m struggling to stay on schedule with this little endeavor. Seriously, I’m not a fan of how I want to know these things, I can see the benefit of knowing these things, but the most I’ll get out of a nearly 30 min long video is that the [ ~ ] is called a “tilde”. How the hell is that useful past knowing it’s the symbol used to denote digital pins with the pulse width modulation feature? Still trying to understand how [ result ] is equivalent to 0.
Sometime between days 13 and 15, I got an email from inventr.io sent me an e-mail with a bonus project. Titled, “HERO Says”, I had to use the circuit I made for day 13 with some extra LEDs to code a Simon says game for the HERO board. Because I was at work, I couldn’t use Fritzing to test the circuit because I would still need the board in front of me. And I didn’t use Tinkercad because I did not feel like trying to remap the code(or doing the several clicks necessary to pull the code I made for this situation a few days earlier. That’s a bit telling about my ability to work, isn’t it?), I opted to wait until I got home and to put it together but started watching the video anyway to try and get familiar with the code and the explanation for it. This vid ran about 14mins and was run by Alex as it was outside the “Lost in Space” theme. The constructed circuit had the analog pins power the LEDs used and the digital was set to the keypad and buzzer. Because this project would have me build a game, the code had a lot more variables that were declared before setup. In fact, the actual [ void setup ] section of code was all of nine lines of code, with the loop function at four. I’m trying to take it slow going through all the code. I’ll see the line, read the comment, then read the line again. Reading has always been a problem for me as my eye just want to dart all over the place. It’s like part of me thinks seeing it is enough while another part is trying to smash my face into the text.
From what I was able to notice when I could focus, the function [ squark ] had two variables that I think I learned about before, [ switch ] and [ case ]. Geek for Geek’s definition of switch statement reads, “Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression.”. Provided that was for C++, but that’s close enough as Arduino’s IDE is based on C and C++. But that still checks out, right? Another part of the code I thought was clever was inside the function [ playSequence ]. To prevent the sequence from taking too long to playback after a new note is added, the line [ wait = 500 – (count * 15); ]. I also realized while typing this that something is off with my math.
You see, by the end of this project, there should be thirty posts, ideally about each mission individually. But this is turning more into a challenge to get this done within a month. If I’m being honest with myself, that means that some of these posts will be stacked with missions that shouldn’t be there. This is the day 14 post, but I’m cramming in the bonus mission to make for not having anything to show for day 15 (creative day) and trying to start day 16. Which might not be right either because I think I should be doing 18 tomorrow. Just double-checked, and I’m behind. I should be doing day 17. 15 was on Saturday and 16 was for Sunday. Soooooo…
I’m doing days 13, 14, and 15 today (3/11/2023). I skipped Thursdays because I only had enough energy to type up day 12’s post after work and the stream. I loaded the mission up but was wiped out. Friday, I just straight-up forgot. So the p l a n is to do them all today. Let’s see how this plays out.
Day 13’s mission started at 16:52 and focused on coding and logic. So I got to use the same circuit. I was tasked with programming the keypad to check and accept a password. [ WHILE ] loops were a topic of focus, as they will execute repeatedly unless a condition is met. [ WHILE ] loops will also not proceed to the next section until the condition is met. The one in this code was designed to wait for four characters to be entered. I’m aware of how important numbers but coding them often comes out looking like this worded math problem. Like someone tried to draw the problem of how many apples Johnny would have if he upcharge them at a 15% markup, before Q3. Not gonna hold you I watched the video but had to keep running back sections because my attention was wandering hard. I’m still not sure I could replicate this code, which is why I should be trying to type it out as I go, but the videos aren’t formatted for that. On top of that I realized that on either Thursday or Friday, inventr.io sent me an email with a bonus mission that I have to try. I have to say hay to a friend later, but I think I have time to get that or day 14 in before then.
Typing this one at 13:36 on the day I should be working on day 13. I built the circuit and went through the video on day 12, just didn’t type as I went through the mission. Day 12’s mission was to add a passive buzzer to the keypad circuit from day 11 and code it to play certain tones when a key is pressed. The circuit wasn’t complicated as it was just the edition of two wires and the buzzer. The code was the hardest part of this mission. The code was a lot more complicated introducing Nested loops as one of the concepts of this mission. Nested loops are loops that are embedded into the ‘for’ loop of a code. The one used in this code was to engage the keypad array to make a particular sound. I understood how the code works as a whole but having trouble retaining how the ‘int tones’ array can take numerical values and turn them into sound, and how ‘j’ and ‘i’ work as a means of short-handing ROW/COL in the loop for cleaner could.
On this mission, I was a bit tired for unrelated reasons, so my approach to it was to build the circuit, run the code, then watch the video, replaying sections for clarity. Once I went through the video, I tried to complete the mission, but it wouldn’t let me. Turns out there was a side quest left to complete. The mission was pitched as a secret transmission that needed to be uploaded to the Arduino. The code didn’t make use of the keypad, just the buzzer. After the code was loaded, the buzzer started playing a chippy version of Rick Astley’s “Never Gonna Give You Up”. While jamming out I read over the code and it was MASSIVE. There are over ninety defined variables and given how the code was designed, the ‘void loop’ wasn’t needed. So, it was left empty. This means that all the other functional aspects of the code were typed exclusively in the ‘void setup’ section.
Here’s a link to the code’s source. But if you don’t want to bother with that, here you go:
[/*
Connect a piezo buzzer or speaker to pin 10 or select a new pin.
More info available at https://github.com/robsoncouto/arduino-songs
Code by Robson Couto, 2019
*/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
#define REST 0
//Change this to make the song slower or faster
int tempo = 114;
//change this to whichever oin you want to use
int buzzer = 10;
//notes of the melody follwed by the duration.
//a 4 means a quaternote, 8 an eighteenth, 16 sixteenth, so on
//!!negative numbers are used to represent dotted notes,
//so -4 means a dotted quater note, that is a quater plus an eighteenth!!
int melody[] = {
// Never Gonna Give You Up - Rick Astley
// Score available at https://musescore.com/chlorondria_5/never-gonna-give-you-up_alto-sax
// Arranged by Chlorondria
NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,4, //1
NOTE_E5,-4, NOTE_FS5,-4, NOTE_A5,16, NOTE_G5,16, NOTE_FS5,8,
NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,2,
NOTE_A4,16, NOTE_A4,16, NOTE_B4,16, NOTE_D5,8, NOTE_D5,16,
NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,4, //repeat from 1
NOTE_E5,-4, NOTE_FS5,-4, NOTE_A5,16, NOTE_G5,16, NOTE_FS5,8,
NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,2,
NOTE_A4,16, NOTE_A4,16, NOTE_B4,16, NOTE_D5,8, NOTE_D5,16,
REST,4, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_D5,8, NOTE_E5,8, NOTE_CS5,-8,
NOTE_B4,16, NOTE_A4,2, REST,4,
REST,8, NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,4, NOTE_A4,8, //7
NOTE_A5,8, REST,8, NOTE_A5,8, NOTE_E5,-4, REST,4,
NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_D5,8, NOTE_E5,8, REST,8,
REST,8, NOTE_CS5,8, NOTE_B4,8, NOTE_A4,-4, REST,4,
REST,8, NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_A4,4,
NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,4, REST,4,
NOTE_D5,2, NOTE_E5,8, NOTE_FS5,8, NOTE_D5,8, //13
NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,4, NOTE_A4,4,
REST,2, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8,
REST,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_E5,-8, NOTE_E5,-8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,-8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, //18
NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,8, NOTE_A4,8, NOTE_A4,8,
NOTE_E5,4, NOTE_D5,2, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_A5,4, NOTE_CS5,8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,4, NOTE_A4,8, //23
NOTE_E5,4, NOTE_D5,2, REST,4,
REST,8, NOTE_B4,8, NOTE_D5,8, NOTE_B4,8, NOTE_D5,8, NOTE_E5,4, REST,8,
REST,8, NOTE_CS5,8, NOTE_B4,8, NOTE_A4,-4, REST,4,
REST,8, NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_A4,4,
REST,8, NOTE_A5,8, NOTE_A5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,8, NOTE_D5,8,
REST,8, NOTE_A4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, //29
REST,8, NOTE_CS5,8, NOTE_B4,8, NOTE_A4,-4, REST,4,
NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_A4,4, REST,8,
REST,8, NOTE_E5,8, NOTE_E5,8, NOTE_FS5,4, NOTE_E5,-4,
NOTE_D5,2, NOTE_D5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,4,
NOTE_E5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,8, NOTE_A4,8, NOTE_A4,4,
REST,-4, NOTE_A4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, //35
REST,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_E5,-8, NOTE_E5,-8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,4, NOTE_A4,8,
NOTE_E5,4, NOTE_D5,2, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, //40
NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_A5,4, NOTE_CS5,8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,4, NOTE_A4,8,
NOTE_E5,4, NOTE_D5,2, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, //45
NOTE_A5,4, NOTE_CS5,8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,4, NOTE_A4,8,
NOTE_E5,4, NOTE_D5,2, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, //45
NOTE_A5,4, NOTE_CS5,8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16,
NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,4, NOTE_A4,8,
NOTE_E5,4, NOTE_D5,2, REST,4
};
// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int notes = sizeof(melody) / sizeof(melody[0]) / 2;
// this calculates the duration of a whole note in ms
int wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;
void setup() {
// iterate over the notes of the melody.
// Remember, the array is twice the number of notes (notes + durations)
for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
// calculates the duration of each note
divider = melody[thisNote + 1];
if (divider > 0) {
// regular note, just proceed
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone(buzzer, melody[thisNote], noteDuration * 0.9);
// Wait for the specief duration before playing the next note.
delay(noteDuration);
// stop the waveform generation before the next note.
noTone(buzzer);
}
}
void loop() {
// no need to repeat the melody.
} ]