Embedded Programming

This week we were assigned to use different types of software to program and see how efficient each of the programming languages is. For this week I thought about creating a basic code for my PCB to play a song. To accomplish this task, use my PCB from Week 4

On the group page you will find information to be able to program in C++, PHYTON and Microphiton. There is also information about some microcontrollers that are in stock, you can consult it here

List of architectures in FabLab Puebla

Architecture AVR (8-bit) ARM Cortex-M0+ RISC-V (32-bit) Xtensa (LX6/LX7) Tensilica L106 (32-bit) ARM Cortex-M4F
Pros Low power consumption, Simple to program, Good community support Energy efficient, Wide range of tools, High performance for 32-bit Open source architecture, Scalable, Flexible Highly customizable, Good for DSP applications, Integrated Wi-Fi and Bluetooth Low cost, Integrated Wi-Fi, Good SDK support High performance, Floating point unit, Energy efficient
contras Limited processing power, Fewer advanced peripherals More complex to program than AVR, Higher cost Less mature ecosystem, Fewer ready-to-use libraries Proprietary, Requires licensing for commercial use Single core, Less powerful than newer ESP32 models More expensive than simpler cores, Complex for beginners

Arduino

To start practicing and understanding the language more I was looking at several song options which I was playing with and it was very helpful to me since I understood them from the examples I had, although it was a bit difficult for me. Here was where I found some examples for arduino

To start programming my code, I first needed to know what the natural scale and its semitones were, the times of the musical notes and the frequencies with which I am going to work (the same notes in different octaves will cause the frequency to change. )

First I declare the pin which to use and create variables with each of the notes and their respective frequency depending on what octave my song is in

I will also program the timing of the notes so I can make the song sound good. I will define the constants in which I will create functions for the white notes, quarter notes, eighth notes and sixteenth notes so as not to avoid writing the time values of each note in the code

I will use the SPEED_FACTOR functions to control the speed at which notes are played. and I will give the value of 0.5 which means that all notes will be played at 50% of their original duration, speeding up playback.
I will also use playNote for the function that will be responsible for playing a note or producing silence for a period of specified time.
I will use the tone function to generate the sound on a specific pin (SPEAKER_PIN).
I will also use delay to wait for the duration of the note set by SPEED_FACTOR.s
The value in the rhythm array is a number that represents a fraction of an EIGHTH_NOTE. Multiplying rhythm[i] by EIGHTH_NOTE gives the correct duration in milliseconds for the desired note.

Now that all the constants and pins are defined correctly, I will add the melody and rhythm to the code.

I will create a for loop that will loop through each note in the melody array and its corresponding duration in the rhythm array.
For each iteration of the loop, the playNote function is called with two arguments which are: the frequency of the note (melody[i]) and the duration in milliseconds (rhythm[i] * EIGHTH_NOTE).

My code


My song

Rick Astley - Never Gonna Give You Up

Useful links to help u (;

Get in touch

Follow