Cosine
Cosine

Reputation: 572

Basics of C++ with Arduino

I have read about Arduino, and how it uses a language that is similar but not equal to C. I am very familiar with C++, and I was wondering how one would do basic tasks with the Arduino, such as communicating with the I/O pins. I figure that one would need the memory address to the pins, and then do something like this for a "flashing led":

int main()    {
    while (1)    {
        bool * out_pin = /* Whatever that memory address was for that pin */;
        *out_pin = 1;
        // Some sort of sleep function? (I only know of "windows.h"'s "Sleep" function)
        *out_pin = 0;
    }
    return 0; // Kind of unneeded, I suppose, but probably compiler errors otherwise.
}

I'm probably really wrong: that's why I'm asking this question.

Upvotes: 0

Views: 848

Answers (2)

Cosine
Cosine

Reputation: 572

This is copied from the comments below my question. David Schwartz answered my questions:

Close. The pins don't have memory addresses (they're register mapped, not memory mapped). Generally, the compiler already maps them to variables for you. So you just do pin_name = pin_value; (like PORTD = 7;) and the compiler does the magic. – David Schwartz 7 mins ago

[PORTD's] a keyword for a register. It behaves like a variable. When the compiler sees PORTD = 7; is compiles it to the necessary assembly code to load a 7 into the PORTD register. When it sees i = PORTD; is loads the value from the PORTD register and stored it in the variable i. The compiler just makes it work. – David Schwartz 2 mins ago

Thank you!

Upvotes: 1

Arnab Datta
Arnab Datta

Reputation: 5226

You need to use pinMode(your_pin) to activate the IO pin. Then you can use digital/analog write/read to communicate with them

Upvotes: 0

Related Questions