Toying with variables

Hello! I was wondering how to use a short and where to use it. The plan was to have a very basic setup where I have three LEDs and a button. The LEDs are controlled via a short deciding which LED to turn on (if 1 turn on red and turn off previous, and so on) When pressed the button would add one to the short (which starts at 1); when the short is greater than 3 it is put back to one. I was wondering am I using this right and if so how to use it, or even if I should be using a short. Thank you for your time and support of a fresh baby to the beautiful world of Arduino! :slight_smile:

int Green = 5;
int Yellow = 6;
int Red = 7;
int Button = 8;
short lightNumber = 1;
void setup() {
pinMode(Button, INPUT);
pinMode(Green, OUTPUT);
pinMode(Yellow, OUTPUT);
pinMode(Red, OUTPUT);
}
void loop() {
  if(digitalRead(Button) == HIGH)
  {
    digitalWrite(lightNumber +1) 
  }
 else
 {
  digitalWrite(lightNumber, + 1);
 }
}

"short" is just another name for "int", so there is no difference between any of these:

int lightNumber = 1;
short int lightNumber = 1;
short lightNumber = 1;

"long" however, is different to "int".

It appears you need to go through some of the basic examples that come with the IDE.
Once you master the basics, proceed with your project.

Hello and welcome,

  • For most arduinos, short is the same thing as int: a 16 bits, signed variable. Do you need a 16 bits, signed variable to store a number between 1 and 3 ? No, 3 can be stored in 2 bits, so you can use the smallest unsigned variable type, which is the byte ( or: uint8_t ).

  • digitalWrite takes 2 parameters: a pin number and the state to be written to that pin.

  • You have to assign a new value to lightNumber, if you want it to increase.

lightNumber = lightnumber + 1;

, or you can also use the increment operator

lightNumber++;
  • You also have to use a debouncing method and, if you want to increment by 1 every time you press the button, you also need a state change detection method. You can also use a button library, which will do all the hard work for you. This library also contains an example that is almost exactly what you want to do.. :wink:

  • To constraint the variable between 1 and 3, just check (after increment) if the variable equals 3, in which case you set it to 1.

For your coding, this example should get you a long way there:

Thank you for all the help.