can I use the variables of an array to add to the end of a variable?

I am trying to understand various aspects of the language and make it do things I have not done before.

I am trying to read the input of a remote unit.

It feeds a signal into d2-5 that I want to monitor and act on.

I have succeded in doing it with an instruction for each input so I am now wondering if I can do a similar thing with a sub routine (what do you call the extra sections that start 'void xxx()'?

I have used arrays before but not added the contents of the array to the end of a variable (can I do this?)
i.e. lastDebounceTime (s[r])
The sketch is here, it possibly explains it better.

int b=0;
int number_test;

const int fourbutton1 = 5;
const int fourbutton2 = 2;
const int fourbutton3 = 3;
const int fourbutton4 = 4;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int lastButtonState1 = 0;   // the previous reading from the input pin
int lastButtonState2 = 0;
int lastButtonState3 = 0;
int lastButtonState4 = 0;
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime1 = 0;  // the last time the output pin was toggled
long lastDebounceTime2 = 0;
long lastDebounceTime3 = 0;
long lastDebounceTime4 = 0;
long debounceDelay = 50;   

int r;
int s[] = {1,2,3,4};
int reading1;
int reading2;
int reading3;

void setup() {

  pinMode(fourbutton1, INPUT);
  pinMode(fourbutton2, INPUT);
  pinMode(fourbutton3, INPUT);
  pinMode(fourbutton4, INPUT);


void loop() {
//  buttonState2 = digitalRead(fourbutton2);
  // read the state of the switch into a local variable:
  reading1 = digitalRead(fourbutton1);
  reading2 = digitalRead(fourbutton2);
  reading3 = digitalRead(fourbutton3);
 // reading4 = digitalRead(fourbutton4);     // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited long enough since the last press to ignore any noise:
  if (reading3 != lastButtonState3) 
  {         // If the switch changed, due to noise or pressing:

void read_remote()
  int lastDebounceTime;
    (lastDebounceTime(s[r])) = millis(); // reset the debouncing timer
  if ((millis() - lastDebounceTime3) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state:    
    if (reading3 != buttonState3) {                // if the button state has changed:
      buttonState3 = reading3;
      if (buttonState3 == HIGH) {                  // only toggle the LED if the new button state is HIGH

 lastButtonState3 = reading3;  // save the reading.  Next time through the loop, it'll be the lastButtonState:



Make an array of 'lastDebounceTime', 'lastDebounceTime[4]', instead of individual variables then:-


Same with your button state and other variables. (Use arrays.)

And you need 'unsigned long' to store 'millis()' values, not 'long'. (Not large enough.):-

unsigned long lastDebounceTime[4];

thanks Steve, nice to hear from you again.
I had thought of that. The problem is, if you look at the section of 'void read_remote' there are a number of variables that would all want to change as the array changes i.e.
is it possible to just add something to the and of each one that takes its source from the array to change each one each time?
I wondered if it was possible without having to make then separate arrays.
The more I look at it the more it looks quite easy to do just that.

is it possible to just add something to the and of each one that takes its source from the array to change each one each time?

Unfortunately not. Those names only exist until compile time, so they can't be modified in that fashion at runtime, by tacking a number on the end.
After the compiler does it's thing, they're just addresses.

The best and most efficient way is to use arrays for all of those things, the variables and the pin numbers.
It will make things much easier in the long run, and avoid you having to repeat code, too.

(Sorry to be a spoilsport. :frowning: )

If as you say, the names only exist until compile time I don't think it is going to work.
I want to alter buttonState3, for instance, to = reading3 and compare them next time round.
Using an array won't allow this to work will it?

It will work if they’re all in arrays, by using a ‘for’ loop.

for(int i=0; i < 4; i++)
    reading[i] = digitalRead(button[i]);

    if(reading[i] != buttonState[i])
        buttonState[i] = reading[i];



This isn’t intended to be the actual code, but should give you the idea of how to go about things.
It’s very late here, after 2am, so I have to hit the sack before I fall off my chair. :smiley:

I see Paul’s on the job. :slight_smile:

Using an array won't allow this to work will it?

Yes, because the program does not care about the name of the arrays, only the address of the variable that you called buttonStack[3] and of the variable you called reading[3].

While you manipulate the contents of the 3th element of each array, the addresses do not change.

I have used arrays before but not added the contents of the array to the end of a variable (can I do this?)

Variables don't have ends.

A variable is a place that hold a value of a particular type. Some types are array types, in which case
you can access it only element-by-element, or structs or class types, which are accessed field-by-field.

Other types are atomic and the whole variable is updated as one (from the language's point of view).

The contents of an array has to be manipulated element-by-element in C, you cannot pass such values
around as a block (but you can and often do pass pointers to them around).

I think you are trying to ask "are C/C++'s native arrays dynamically extensible?" and the answer is no,
with a caveat for stack-allocated arrays (local to a function). They can have a different size on each
call to the function, each instance cannot be changed in size.

People often use malloc/free with pointers for this sort of thing, but you have to keep track of the sizes.

Variables don't have ends.

Then how come we have big-endian and little-endian systems?

Memory can be big or little endian, but that's a lower level than variables and types. Many languages
don't let you even see endianness, and C only shows it because the pointer/casting model is so permissive.