Skip to main content
Bumped by Community user
Bumped by Community user
deleted 314 characters in body
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

So. I would like to cycle thruthrough an array using a button. My current code goings thrugoes 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 thruthrough an array using buttons instead of this indirect route I have taken.

Last question - Is there a way to set the indents in the arduinoArduino 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[] char*= titles[]={"RELATION", 
               "RELATION",
  "FIRST NAME",
                 "LAST NAME", 
                 "LOCATION",
                 "PHONE NUMBER" 
};
              char* user_info[] = {
char* user_info[]={ "SELF",
                  "JOHN",
                  "SMITH", 
                  "FLORIDA",
                  "999-888-6666" 
};             
char* econ_one[] = 
 {
char* econ_one[]={ "BROTHER", 
                 "MICHAEL",
                 "SMITH", 
                 "FLORIDA",
                 "999-888-5555" 
};
 
char* econ_two[]={"WIFE", 
           econ_two[] = {
  "WIFE",
  "EMILY",
                 "SMITH", 
                 "FLORIDA",
                 "999-888-4444" 
};
 
char* econ_three[] char*= econ_three[]={"SISTER", 
               "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;
= 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 = 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 = 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=      state = 2;
 
  }
  } else if (state == 2)
  {

 
      int z;
      for (z=0;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=      state = 3;
  }

  } else if (state == 3)
  {
 
      int q;
      for (q=0;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

So. I would like to cycle thru an array using a button. My current code goings thru 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 thru 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

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

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 86 characters in body
Source Link
Source Link

Using a button to cycle an array

So. I would like to cycle thru an array using a button. My current code goings thru 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 thru 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);
  }