Electronics How does this Adruino Code Work?

  • Thread starter opus
  • Start date

opus

Gold Member
694
134
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. Im sure some of it has something to do with saying what is going into each port of the arduino, but I cant 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

Borek

Mentor
27,985
2,475
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.
 

opus

Gold Member
694
134
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 cant 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.
 

Borek

Mentor
27,985
2,475
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.
 

opus

Gold Member
694
134
Thank you kind sir.
 

Tom.G

Science Advisor
2,544
1,368
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!
 

Borek

Mentor
27,985
2,475
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.
 

opus

Gold Member
694
134
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 thats pretty cool! Thanks!
 

Want to reply to this thread?

"How does this Adruino Code Work?" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving

Hot Threads

Top