Using Memory Address

Helo, my name's João Victor and I'm trying to develop a controller for boxes of water. I want to send information about sensors and OUTPUTS for a SCADA Software (Elipse or SCADABR).

So I defined thing like:

pino_bomba1 = 48; pino_bomba2 = 49;

And at the setup configuration:

pinMode(pino_bomba1,OUTPUT); pinMode(pino_bomba2,OUTPUT);

But when I try to send like:

enviaModbus(pino_bomba1); enviaModbus(pino_bomba2);

The arduino sends the number 48 and 49. But I really want the status of pin 48 and the pin 49, I want that the SCADA knows if they're active or not. So I'm asking if someone knows the address of that in memory and rather than send the number of the pins I will try to send:

enviaModbus(*addressOfPin48) enviaModbus(*addressOfPin49).

Or, If someone had a better idea for help me with this problem. I'll greatful. Thanks a Lot !

The individual pins have don't have distinct memory addresses.

Would digitalRead(pino_bomba1) not be a way to check the state?

Sending the pin number isn't going to help you much. You want to do a digitalRead of the pin and send the result you get.

This might get you closer to what you want...

enviaModbus(digitalRead(pino_bomba1));
enviaModbus(digitalRead(pino_bomba2));

Yes the pins do have addresses and you can read from them as if they were memory but that's not a good idea. The Arduino digitalRead() function will work on every different Arduino, even with fundamentally different processor chips.

MorganS: Yes the pins do have addresses and you can read from them as if they were memory but that's not a good idea.

A pin has no memory address.

There is a memory location in wich one of the bits represents the pin value, the selection of the address and the correct mask is handled by digitalRead().

Whandall, I think I can't use that : enviaModbus(digitalRead(pino_bomba1) because I inicialize that pin as an OUTPUT. In SETUP, I wrote:

pinMode(pino_bomba1, OUTPUT);

I had an idea to start an variable that follows the pins state, for example:

const int pino_bomba1 = 48; boolean estado_bomba1 = LOW;

void setup(){ pinMode(pino_bomba1, OUTPUT); }

void setup(){ . . .

digitalWrite(pino_bomba1,LOW); estado_bomba1 = LOW; . . . }

But I thought that I could minimize the system using memory address.

There is no problem in reading back a value from a pin, even if its configured as OUTPUT.

joaobalde: digitalWrite(pino_bomba1,LOW); estado_bomba1 = LOW;

But I thought that I could minimize the system using memory address.

I think you are confusing {A} the system of pin numbers that the Arduino system allocates and {B} the actual locations in the Atmega chip where the pins exist.

You can, at any time, read the value of any 8 I/O pins on an I/O port with PINx. For example portAval = PINA; returns the value of the 8 pins on PortA.

I think you would benefit from studying the relevant Atmel datasheet.

In practical terms it is far simpler to save the value in a C/C++ variable unless you are critically short of memory.

...R

Well, I didn't know that I can read an OUTPUT pin. I'll try to do that. Thanks Whandall.

Robin2, I'm not critically short of memory yet, but I want to know different ways of using the Arduino, because I'm a young programmer yet, thanks. I've never read the Atmel datasheet for arduino. I think It's a great idea too. Thanks.

Especially out of a practical view reading back the pin seems to me advantageous.

It does not consume RAM.
Its automatically synchronized.

When digitalRead() is used, it’s portable as a bonus.

For details have a look at the datasheet.

Thanks, I'll try to do that, If other problems happen, I came back. Thanks everybody.

If you are up to accessing a single pin via its register (in memory), you will end up writing your own version of digitalRead() (see code tag).

If you want to handle more than one bit residing in one PINx register at a time, direct access of the register has definitely advantages.

int digitalRead(uint8_t pin)
{
    uint8_t timer = digitalPinToTimer(pin);
    uint8_t bit = digitalPinToBitMask(pin);
    uint8_t port = digitalPinToPort(pin);

    if (port == NOT_A_PIN) return LOW;

    // If the pin that support PWM output, we need to turn it off
    // before getting a digital reading.
    if (timer != NOT_ON_TIMER) turnOffPWM(timer);

    if (*portInputRegister(port) & bit) return HIGH;
    return LOW;
}