Learning C > Questions

setishock

Wizard of Wires
Messages
10,726
Location
4321
I got a book along with the Arduino goodies that explains C in real basic terms. To me a lot of the programming looks like the old basic language. I'm dinking with example sketches and getting a handle on C pretty quick. But I have questions about some of the syntax.

In this line:
Code:
digitalWrite (ledPin, HIGH) ;
The second half of the "word" the first character is capitalized. Is this mandatory to type it that way?
The other part is does HIGH always have to be in caps?

The reason I ask is I'm using Simon Monk's book "Programming Arduino - Getting Started with Sketches"
In the book he puts a lot of spaces between items in a line. And that lead to the second word first character in caps.
Is this really a part of how you have to type out in C?
 
I got a book along with the Arduino goodies that explains C in real basic terms. To me a lot of the programming looks like the old basic language. I'm dinking with example sketches and getting a handle on C pretty quick. But I have questions about some of the syntax.

In this line:
Code:
digitalWrite (ledPin, HIGH) ;
The second half of the "word" the first character is capitalized. Is this mandatory to type it that way?
The other part is does HIGH always have to be in caps?

The reason I ask is I'm using Simon Monk's book "Programming Arduino - Getting Started with Sketches"
In the book he puts a lot of spaces between items in a line. And that lead to the second word first character in caps.
Is this really a part of how you have to type out in C?

Have to? No - but it's basically industry standard and a very good habit to get into, just because it makes things MUCH easier to read / tell what things are at a glance.

Function names in C are usually lower-camelcase. Meaning you start with lowercase, and then every word in the name, you capitalize it. Other languages use Upper-camelcase for Functions to distinguish between functions/variables. So in that sense, it'd be "DigitalWrite(ledPin, HIGH);"

Same thing for variables - lower-camelcase. ledPin is a variable, starts with lowercase and each word after the first is capped.

Variables in all caps (such as HIGH) are constants (meaning the value assigned to it will be replaced at compile-time automatically by the compiler, but to change the value for you, you only have to do it in once place).

Like I said, it's just a way to make things easier to read while you're going through your code so you know what things are what by just glancing at them - this is especially useful if you're not using an editor / IDE that supports color-coding. I definitely recommend using something (even a basic editor like Notepad++), then it's even easier to distinguish what's what.

Make sense?
 
Yeah! Amazingly enough. Thank you. I may post some other questions as I go along. FYI I'm using the Arduino programming software. The compiler is built in.
I really like this gizmo. It's inexpensive and so far, a whole lot of fun.
 
Last edited:
I'm also using arduino but I got away from it because copying and pasting code is getting frustrating.I want to be able to know how to write it myself instead of relying on others' work. And I'm also too proud to ask for a lot of help. So I started learning C off of youtube. But it's nice to get going in the right direction with some input from others now and then.

That being said; can I put a loop in an if/else statement. I want to loop code x based on user input. It's a TV remote and IR receiver connected to the arduino. It's really simple; just blink a light on and off continuously, nothing too elaborate for a beginner's sake.

I want to initiate the code when the button is pushed. But I don' t want to use any other button to stop the loop. So I'm thinking..
if (detect button push)
{
blink indefinitely
}
else (detect same button push)
{
stop blinking
}

I'm pretty proud of learning how to convert the remote code and assign it the right code for the arduino to recognize and turn on and off a light, but I need to take it a litte further now. Not sure what type of loop would be best here (and why).
 
Back
Top Bottom