How does this Adruino Code Work?

  • Electronics
  • Thread starter opus
  • Start date
  • Tags
    Code Work
In summary, this person is trying to create a synthesized keyboard for an Arduino and has some questions about the code. The code has a for loop which does something with the pin status, but the person does not understand what it is doing.
  • #1
opus
Gold Member
717
131
Following up on my previous thread, I found a project that I can do for this Arduino. Its really just a little synthesize keyboard thing, but I'm wondering how the code works. Kind of just the logic behind it. I know that some of the stuff at the bottom deals with tones and frequencies and stuff, but I don't know what the rest does. I am sure some of it has something to do with saying what is going into each port of the arduino, but I can't seem to differentiate anything or see the logic in any of this. I've attached a picture of the code and the link to the project.

Can anyone help me understand this?
Thank you.

http://www.kumantech.com/blog/simple-arduino-sound-board_b0049.html
 

Attachments

  • Screen Shot 2019-02-24 at 6.33.01 PM.png
    Screen Shot 2019-02-24 at 6.33.01 PM.png
    26.6 KB · Views: 507
Physics news on Phys.org
  • #2
Have you read any tutorials on the Arduino programming? www.arduino.cc has plenty of them and it will be a better starting point than our comments on some random piece of code.

Feel free to ask more detailed questions when you have problems understanding something, but don't ask us to teach you from the beginning.
 
  • Like
Likes opus and jim hardy
  • #3
Yes I've done a good amount of research for it. To my understanding, it is a form of C/C++ language, but is different than usual because it has a setup function that basically says "This port is being used, these are the variables and libraries, etc" and then there is a loop function which does the actions. My specific questions are lines 1, 4, 11.

For line 1, it looks like it's saying "I am using these pin holes" but I can't find what bStatuses[4] is doing.
For lines 4, 11, I see that it's a for loop, but I'm not sure what a for loop would do in the case of this project.
 
  • #4
bStatuses is actually completely unnecessary in this code. It is used to keep results of the digitalRead calls (see line 12). It would be easier to just read the pin status and use it immediately, there is no need to remember it.
 
  • Like
Likes opus
  • #5
Thank you kind sir.
 
  • #6
opus said:
11, I see that it's a for loop, but I'm not sure what a for loop would do in the case of this project.
Very good question!

bStatuses is used to quickly read the pin inputs at essentially the same time for all of them (or at least as fast as possible in this case). This is a common approach when interfacing with hardware. The IF statements take some time to execute, especially if they evaluate to TRUE.

If reading the pins were done in each IF statement, there is the possibility that another pin input could change to HIGH while a previous statement was executing. This would result in attempting multiple tones at the same time. For your tone generator this would sound bad. If you were controlling an industrial machine for instance, you might have two motors running at the same time resulting in some bent metal, a mad customer, and a repair bill.

It's these little hidden things that pop up when combining computers with the rest of the world that can turn around and bite you.

Cheers,
Tom

p.s. keep the curiosity going, it's the best way of learning!
 
  • Like
Likes opus
  • #7
Tom.G said:
bStatuses is used to quickly read the pin inputs at essentially the same time for all of them (or at least as fast as possible in this case). This is a common approach when interfacing with hardware. The IF statements take some time to execute, especially if they evaluate to TRUE.

Interesting point, haven't thought about it.
 
  • #8
Tom.G said:
Very good question!

bStatuses is used to quickly read the pin inputs at essentially the same time for all of them (or at least as fast as possible in this case). This is a common approach when interfacing with hardware. The IF statements take some time to execute, especially if they evaluate to TRUE.

If reading the pins were done in each IF statement, there is the possibility that another pin input could change to HIGH while a previous statement was executing. This would result in attempting multiple tones at the same time. For your tone generator this would sound bad. If you were controlling an industrial machine for instance, you might have two motors running at the same time resulting in some bent metal, a mad customer, and a repair bill.

It's these little hidden things that pop up when combining computers with the rest of the world that can turn around and bite you.

Cheers,
Tom

p.s. keep the curiosity going, it's the best way of learning!
Hey that's pretty cool! Thanks!
 

1. How does the Arduino code control hardware?

The Arduino code controls hardware by using specific commands and functions that are designed to communicate with the hardware components. These commands are written in the code and tell the hardware what actions to perform.

2. Can I modify the Arduino code?

Yes, you can modify the Arduino code to fit your specific needs. The code is open-source, which means it can be freely accessed and modified by anyone.

3. How does the Arduino code interact with sensors?

The Arduino code interacts with sensors by using input and output commands. The code reads data from the sensors and uses it to make decisions or perform actions. It can also send signals to the sensors to control their behavior.

4. Does the Arduino code have to be written in a specific language?

The Arduino code is written in a specific language called Arduino programming language, which is based on C++. However, it also has its own unique functions and syntax that make it easier to use for beginners.

5. How does the Arduino code communicate with other devices?

The Arduino code can communicate with other devices through various communication protocols such as serial, I2C, and SPI. These protocols allow the Arduino to send and receive data from other devices, such as computers, sensors, or other microcontrollers.

Similar threads

  • Programming and Computer Science
Replies
2
Views
351
  • DIY Projects
Replies
13
Views
2K
Replies
7
Views
1K
  • Programming and Computer Science
Replies
0
Views
727
  • Programming and Computer Science
Replies
11
Views
807
  • DIY Projects
Replies
32
Views
6K
  • Programming and Computer Science
Replies
1
Views
636
Replies
22
Views
2K
  • Programming and Computer Science
Replies
1
Views
233
  • DIY Projects
Replies
18
Views
3K
Back
Top