So. I would like to cycle through an array using a button. My current code goes through if statements when the button is pushed. In each if statement I have a loop to print out a list of strings. I am curious if there is anyway to directly cycle through an array using buttons instead of this indirect route I have taken.
Last question - Is there a way to set the indents in the Arduino IDE to make formatting easier here?
//------------------------------------------------------------------------------------------------
// Displays information when a push button is pressed. Also turns on and off a corresponding led.
// 5/3/2016
//------------------------------------------------------------------------------------------------
// declare variables
char* titles[] = {
"RELATION",
"FIRST NAME",
"LAST NAME",
"LOCATION",
"PHONE NUMBER"
};
char* user_info[] = {
"SELF",
"JOHN",
"SMITH",
"FLORIDA",
"999-888-6666"
};
char* econ_one[] = {
"BROTHER",
"MICHAEL",
"SMITH",
"FLORIDA",
"999-888-5555"
};
char* econ_two[] = {
"WIFE",
"EMILY",
"SMITH",
"FLORIDA",
"999-888-4444"
};
char* econ_three[] = {
"SISTER",
"SOPHIA",
"ADAMS",
"NEW YORK",
"888-777-3333"
};
int ledPinOne = 2;
int ledPinTwo = 3;
int ledPinThree = 4;
int ledPinFour = 5;
int buttonPin = 6;
int state = 0;
// setup - set pins
void setup() {
Serial.begin(9600);
pinMode(ledPinOne, OUTPUT);
pinMode(ledPinTwo, OUTPUT);
pinMode(ledPinThree, OUTPUT);
pinMode(ledPinFour, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.println("Emergency Contact");
}
// main
void loop() {
if (digitalRead(buttonPin)) {
if (state == 0) {
int x;
for (x = 0; x <= 4; x++) {
Serial.println(" ");
Serial.print(titles[x]);
Serial.print(" ");
Serial.print(user_info[x]);
}
Serial.println(" ");
setLights(HIGH, LOW, LOW, LOW);
state = 1;
} else if (state == 1) {
int y;
for (y = 0; y <= 4; y++) {
Serial.println(" ");
Serial.print(titles[y]);
Serial.print(" ");
Serial.print(econ_one[y]);
}
Serial.println(" ");
setLights(LOW, HIGH, LOW, LOW);
state = 2;
} else if (state == 2) {
int z;
for (z = 0; z <= 4; z++) {
Serial.println(" ");
Serial.print(titles[z]);
Serial.print(" ");
Serial.print(econ_two[z]);
}
Serial.println(" ");
setLights(LOW, LOW, HIGH, LOW);
state = 3;
} else if (state == 3) {
int q;
for (q = 0; q <= 4; q++) {
Serial.println(" ");
Serial.print(titles[q]);
Serial.print(" ");
Serial.print(econ_three[q]);
}
Serial.println(" ");
setLights(LOW, LOW, LOW, HIGH);
state = 0;
}
delay(1000);
}
}
// function to turn LEDs on/off
void setLights(int one, int two, int three, int four) {
digitalWrite(ledPinOne, one);
digitalWrite(ledPinTwo, two);
digitalWrite(ledPinThree, three);
digitalWrite(ledPinFour, four);
}
output image
https://www.dropbox.com/sc/fwll0qe7cyye1mt/AAD5vI9buU_X0bl04KXFAQyja
Ctrl+T
in the IDE to format the code.