Variable ranges

Hi guys,

To give a bit of context I am using an LCD screen to control some lights. I would like to have a menu of which if you select the 'up' button a variable is increased and the 'down' button the variable is decreased.

The issue is I do not want it to change in a set interval.

E.g

value =600
Up Button
value =630
Up Button
value=1000

SO I want to be able to determine a range of values that this variable can be and I would like some help.

i was thinking of maybe using an array and changing the variable to be a value from the array however i don't know how to do this..

Thanks for your help!

An array sounds like the way to go

Declare an array with the required values in it
Count how many times the button becomes pressed
Use the count as the index to the array to determine the value to be used

Which part are you stuck on ?

Hi UKHeliBob,

I have never used arrays before so I was wondering if you could point me to an example of the code you mentioned or any relevant documentation outlining how to do this?

Regards,

Isaac.

Try Googling "arduino array".

A small example for you of declaring at array of values and using the value taken from the array

unsigned int arrayOfValues[4] = {1000, 2000, 3000, 4000};

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  for (int arrayIndex = 0; arrayIndex < 4; arrayIndex++)
  {
    Serial.print("arrayIndex : ");
    Serial.print(arrayIndex);
    Serial.print("\tvalue : ");
    int currentValue = arrayOfValues[arrayIndex];
    Serial.println(currentValue);
    delay(currentValue);
  }
  Serial.println();
}

The main thing to note is that the array has 4 values in it and they are accessed with index values of 0, 1, 2 and 3. The most common mistake when using arrays is to forget that the index starts at 0 rather than 1.

Note that if you change the number of values in the array then you need to change the array index limit to match anywhere that it is used in the program. The example has the number of array elements hard coded as 4. This is not good practice and you can make the program work out how many elements there are for you but I didn’t want to complicate things for you.

To meet your requirement you need to read a button input and set the array index to a value depending on how many times the button becomes pressed and use the number as the index to the array. Note the emphasis on detecting when the button becomes pressed rather than when it is pressed. If you are not familiar with that technique then have a look at state change detection tutorial

Arrays are part of the C/C++ language so you should find plenty of tutorial advice on line if you search for “C arrays”