Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to write a simple program that will cycle threw an array using push buttons. My current version of the program uses if statements and print 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 style threw the data in one direction and another push button to cycle threw 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() {

Serial.begin(9600);

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

Serial.println("ID");
Serial.println(user_name_array);

}


//------------------------------- Main Program
void loop() {

if (digitalRead(buttonPin)) 
{
  if (state == 0)
  {
    Serial.println("Athlete");
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;

  }

delay(1000);

}

}

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 4 leds setup. When the button is pushed one of the LEDs turn on and then when the button is pushed again it turns it off and turns the second one on and so forth.

thank you in advance for any help.

share|improve this question
    
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 at 13:04

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...

Sorry, of course there is no "printf" in Arduino land. So that for loop would look more like:

for(i = 0; i < num_states; i++) 
{
 Serial.println("state ");
 Serial.println(i)
 Serial.println(states[i]);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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