How does this Adruino Code Work?

  • Context: Electronics 
  • Thread starter Thread starter opus
  • Start date Start date
  • Tags Tags
    Code Work
opus
Gold Member
Messages
717
Reaction score
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: 602
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   Reactions: opus and jim hardy
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.
 
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   Reactions: opus
Thank you kind sir.
 
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   Reactions: opus
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.
 
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!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 0 ·
Replies
0
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
10K
  • · Replies 3 ·
Replies
3
Views
3K