0

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.

6
  • 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. Commented Jul 30, 2012 at 1:08
  • Thank you so much for that (quick!) answer. I am curious this though: what type is your "pin_name" Is it a bool * or something else? And can you later do what I did modifying the value (and such)? Commented Jul 30, 2012 at 1:13
  • It'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. Commented Jul 30, 2012 at 1:14
  • Your use of web-searches seems to be failing, one of the first examples on the official Arduino page is a blinking led. Commented Jul 30, 2012 at 5:50
  • @Joachim: That is in the Arduino language: I figured it was different with C++, even with the Arduino library. I searched the web far before asking this question. It has been answered already. (But I can't mark it as answered until two days after asking it). Commented Jul 30, 2012 at 15:17

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

What library is pinMode in? I thought that that was an "Arduino Programming Language" function. Is there a library where I can get that for C++?
pinMode is indeed in the arduino standard library. I haven't heard of a pure C++ equivalent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.