DEV Community

loading...

Learning to Code for an Adruino

Kyle Trahan
Full Stack Software Engineer
・3 min read

After buying my Arduino starter kit I start diving into it and realized that the coding part of it was actually really simple. From my last blog post I talked about the musical keyboard I made. Now I want to go into a little more detail about the actual code behind the scenes.

So most arduino projects consist of two different functions. You have your setup(), which is run once when the program starts and you have your loop() that continues to run based off the conditions you have set within. Other than that its pretty simple straight forward coding. You can declare some variables before getting started to store different sets of data.

The main variable I needed for this project was just an array of integers. Creating that looks like so:

int notes[] = { 262, 294, 330, 349 }

Pretty simple really, if you haven't worked with a typed language. This is basically just saying I have an array called notes made up of integers.

Next up we have the setup() function, which was really simple for this project. It looks like:

 void setup() {

  Serial.begin(9600);

} 
Enter fullscreen mode Exit fullscreen mode

Serial is a port object, we are telling it to start, and to work at a speed of 9600 bits per second (baud). This basically just opens up a serial port, and sets the data rate to 9600 bps. What this really means is that now whenever I press buttons on the keyboard I will receive the output data so that I can see how much power is coming through each button. Then I can use that information to set what frequency should play based on the amount of power coming through. It just allows for some fine tuning. The other part of this functionality is kept within the loop().

Next up we have the loop() function. Also pretty simple for this project it just has a few if else statements to determine what note should be played for a range of frequencies. In here we are using Serial.print in order to print out those values we need in order to set our ranges to determine which frequency should be played.

void loop() {

  int keyVal = analogRead(A0);
  Serial.print(keyVal);

   if ( keyVal == 1023 ) {
    tone( 8, notes[0] );
   } else if ( keyVal >= 990 && keyVal <= 1010 ) {
    tone( 8, notes[1] );
   } else if ( keyVal >= 505 && keyVal <= 515 ) {
    tone( 8, notes[2] );
   } else if ( keyVal >= 5 && keyVal <= 10 ) {
    tone( 8, notes[3] );
   } else {
    noTone(8);
   }

}
Enter fullscreen mode Exit fullscreen mode

In here we are setting our keyVal to be equal to the input that is being read by the A0 pin on our Arduino board. This will basically take a reading of how much power is passing through whenever you hit the button. Each button is wired up with a different level resistor therefore they should all be transferring different levels of power. Then its as simple as saying if its between these two power levels I want you to tone(8), aka play this note through our piezo which is connected to pin 8 on the Arduino board. Which we then grab a different frequency from the notes[] we made earlier. Its important to remember to set everything else to just play noTone() otherwise you'll have a frequency playing all the time!

Thats for taking the time to read this and stay tuned for future projects. Have a great day!

Discussion (0)