I am trying to write a simple program that will cycle through an array using push buttons. My current version of the program uses if statements and prints lines.

My goal is to have contact information for 4 people stored in an array. I can either have an array of strings per person or make different arrays of strings for the different type of information (ie names, phone numbers).

I would like one push button to cycle through the data in one direction and another push button to cycle through the data in the other direction. This is my code so far using the if statements. I modified the code from a traffic light tutorial hence you will see some of the variables named after colors.

//------------------------------- Declare variables
int ledPinOne = 2;
int ledPinTwo = 3;
int ledPinThree = 4;
int ledPinFour = 5;
int buttonPin = 6;
int state = 0;
char user_name_array[32];
char econ_one_name_array[32];
char econ_two_array[32];
char econ_three_name_array[32];

//------------------------------- Setup
void setup() {
  //------------------------------ Sets pins for I/O
  pinMode(ledPinOne, OUTPUT);
  pinMode(ledPinTwo, OUTPUT);
  pinMode(ledPinThree, OUTPUT);
  pinMode(ledPinFour, OUTPUT);
  pinMode(buttonPin, INPUT);

//------------------------------- Main Program
void loop() {
  if (digitalRead(buttonPin)) {
    if (state == 0) {
      Serial.println("John Smith");
      Serial.println("No allergies, no medications");
      Serial.println("  ");
      setLights(HIGH, LOW, LOW, LOW);
      state = 1;
    } else if (state == 1) {
      Serial.println("Emergency Contact - Sister");
      Serial.println("Eliza Smith");
      Serial.println("Cell - 333.888.777");
      Serial.println("  ");
      setLights(LOW, HIGH, LOW, LOW);
      state = 2;

    } else if (state == 2) {
      Serial.println("Emergency Contact - Hermosita");
      Serial.println("Jackie Smith");
      Serial.println("Cell - 777.888.7777");
      Serial.println("  ");
      setLights(LOW, LOW, HIGH, LOW);
      state = 3;
    } else if (state == 3) {
      Serial.println("Emergency Contact - Father");
      Serial.println("Michael Smith");
      Serial.println("Cell - 888.777.777");
      Serial.println("  ");
      setLights(LOW, LOW, LOW, HIGH);
      state = 0;

void setLights(int red, int yellow, int green, int blue) {
  digitalWrite(ledPinOne, red);
  digitalWrite(ledPinTwo, yellow);
  digitalWrite(ledPinThree, green);
  digitalWrite(ledPinFour, blue);

I also have a 4-LED setup. When the button is pushed one of the LEDs turns on and then when the button is pushed again it turns off and turns the second one on and so forth.

Thank you in advance for any help.


This question was asked again in a more detailed way and has more answers here:

Using a button to cycle an array

  • You should try and ask 1 question at a time. That way when other people search with a similar question they can find yours and its answers.
    – st2000
    May 2, 2016 at 13:04

1 Answer 1


Let's just deal with character strings.

The truth of the matter is that Arduino programming uses the language C. C is very common and you can search for help in many places.

There are several ways to deal with strings in C. In this example we are using an array of pointers to strings:

// let's make our own array of strings
char *states[] = {
    "California", "Oregon",
    "Washington", "Texas"
int num_states = 4;

for(i = 0; i < num_states; i++) {
    printf("state %d: %s\n", i, states[i]);

The output of this code looks like:

state 0: California
state 1: Oregon
state 2: Washington
state 3: Texas

I grabbed this example from here.

added later...

The method "printf" is normally not used in Arduino land. So that "for" loop would look more like:

for(i = 0; i < num_states; i++) 
 Serial.println("state ");
  • 4
    Ahem... printf DOES exists in Arduino land, just include <stdio.h> Apr 28, 2017 at 9:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.