Function and array question.

#include "stdafx.h"
#include <iostream>
using namespace std;

int button1 ()
{   char ans;
    int button;
      int led1[5] ={1,1,1,1,1};
    do {
cout << led1[0]<<led1[1]<<led1[2]<<led1[3]<<led1[4];
cout <<endl;
cout <<"01234"<<endl;
cout <<"Enter Button: ";
cin >>button;
if (button <= 4)
{
led1[button] = 0;
}
else if (button == 5)
{   cout << "Done (Y or N): " << endl;
    cin>> ans;
      cout << endl;}
else if (button >= 6)
{cout <<endl;
      cout << "Invalid input" <<endl;
cout <<endl;}
}
    while ((ans !='Y')&&(ans !='y'));
return true;
}
int button2 ()
{   char ans;
    int button;
    int frstrn = 0;
    int led2[5] = {1,1,1,1,1};
    frstrn = frstrn + 1;
    do {
cout << led2[0]<<led2[1]<<led2[2]<<led2[3]<<led2[4];
cout <<endl;
cout <<"012345"<<endl;
cout <<"Enter Button: ";
cin >>button;
if (button <= 4)
{
led2[button] = 0;
}
else if (button == 5)
{   cout << "Done (Y or N): " << endl;
    cin>> ans;
      cout << endl;}
else if (button >= 6)
{cout <<endl;
      cout << "Invalid input" <<endl;
cout <<endl;}
}
    while ((ans !='Y')&&(ans !='y'));
return true;
}
int button3 ()
{
    char ans;
    int button;
    int frstrn = 0;
    int led3[5] = {1,1,1,1,1};
    frstrn = frstrn + 1;
    do {
cout << led3[0]<<led3[1]<<led3[2]<<led3[3]<<led3[4];
cout <<endl;
cout <<"012345"<<endl;
cout <<"Enter Button: ";
cin >>button;
if (button <= 4)
{
led3[button] = 0;
}
else if (button == 5)
{   cout << "Done (Y or N): " << endl;
    cin>> ans;
      cout << endl;}
else if (button >= 6)
{cout <<endl;
      cout << "Invalid input" <<endl;
cout <<endl;}
}
    while ((ans !='Y')&&(ans !='y'));
return true;
}
int button4 ()
{
    char ans;
    int button;
    int frstrn = 0;
    int led4[5] = {1,1,1,1,1};
    frstrn = frstrn + 1;
    do {
cout << led4[0]<<led4[1]<<led4[2]<<led4[3]<<led4[4];
cout <<endl;
cout <<"012345"<<endl;
cout <<"Enter Button: ";
cin >>button;
if (button <= 4)
{
led4[button] = 0;
}
else if (button == 5)
{   cout << "Done (Y or N): " << endl;
    cin>> ans;
      cout << endl;}
else if (button >= 6)
{cout <<endl;
      cout << "Invalid input" <<endl;
cout <<endl;}
}
    while ((ans !='Y')&&(ans !='y'));
return true;
}
int main()
{   
    int button;
    do {
    cout << " " << endl;
    cout << "Main menu" << endl;
    cout << " " << endl;
    cout << "1.....Button 1" << endl;
    cout << "2.....Button 2" << endl;
    cout << "3.....Button 3" << endl;
    cout << "4.....Button 4" << endl;
    cout << "5.....Ends Program" << endl;
    cout << "Please make your choice: ";
    cin >> button;
    cout << " " << endl;
    if (button == 1)
    {
        int led1[5] = {1,1,1,1,1};
        button1();}
    else if (button == 2)
    button2();
    else if (button == 3)
    button3();
    else if (button == 4)
    button4();
    }
    while (button != 5);
    return 0;
}

what it does:
goes to a function and changes the value of an array from 1 (on) to 0 (off) and resets to on when i leave the function and then return to it.

what it needs to do:
not reset the function. i want to be able to push button 1 and turn off two or three or whatever leds and then go to main menu and push button 2 and do the same then return to button and and turn the rest of the lights off. they only need to be on once but i need to be able to pick and choose what lights are shut off.

also i know this isnt the correct syntax for the arduino but once i get it on the command line i can make it work for the arduino.

thanks-
dustin

Hi,

it is not really clear for me, what you wynt your program to do. But i think one problem is, that you declare lcal arays led1[], led2[] inside the function. If you leave the function and reenter it later on, the array will be created new and will be initialized with all 1. Try to decalre the arrays befor fucntion button1 as global variables:

int led1[5] = {1,1,1,1,1}; int led2[5] = {1,1,1,1,1}; ...

int Button1 () { ...

regards Mike

yeah i thought that too and i thought i needed a place holder like if the value is this then led1 newled1 or something… basically i was making this whole thing WAY to hard (i do that a lot) i got the code working this way:

/*
      Damion.cpp Prgram for my friend Damion to turn
      off an LED after the button has been pressed.
*/
#include "stdafx.h"
#include "stdlib.h"   //used for system("cls"); to clear the screen
#include <iostream>

using namespace std;

int main()
{   char button;  //used to make sure a-z isnt pushed instead
      int led1[5] = {1,1,1,1,1}; // my arrays of led's (turned on)
      int led2[5] = {1,1,1,1,1};
      int led3[5] = {1,1,1,1,1};
      int led4[5] = {1,1,1,1,1};
      do {
      system("cls");  //clear screen
    cout << endl;
    cout << "Main menu" << endl;
    cout << " " << endl;
    cout << "1.....Button 1" << endl;
    cout << "2.....Button 2" << endl;
    cout << "3.....Button 3" << endl;
    cout << "4.....Button 4" << endl;
    cout << "5.....Ends Program" << endl;
    cout << "Please make your choice: ";
    cin >> button;
    cout << " " << endl;  
      if (button == '1')
      {   system("cls");
            int btn;
            do {
            cout<<led1[0];cout<<" ";cout<<led1[1];cout<<" ";cout<<led1[2];cout<<" ";cout<<led1[3];cout<<" ";cout<<led1[4];cout<<" "<<endl;
            cout<<"0 1 2 3 4"<<endl;
            cout<<"Enter button to turn off 5 to return: ";
            cin>>btn;
            if (btn <=4)
            led1[btn] = 0;
            }while (btn != 5);
      }
    else if (button == '2')
    {   system("cls");
            int btn2;
            do {
            cout<<led2[0];cout<<" ";cout<<led2[1];cout<<" ";cout<<led2[2];cout<<" ";cout<<led2[3];cout<<" ";cout<<led2[4];cout<<" "<<endl;
            cout<<"0 1 2 3 4"<<endl;
            cout<<"Enter button to turn off 5 to return: ";
            cin>>btn2;
            if (btn2 <=4)
            led2[btn2] = 0;
            }while (btn2 != 5);
      }
      else if (button == '3')
    {   system("cls");
            int btn3;
            do {
            cout<<led3[0];cout<<" ";cout<<led3[1];cout<<" ";cout<<led3[2];cout<<" ";cout<<led3[3];cout<<" ";cout<<led3[4];cout<<" "<<endl;
            cout<<"0 1 2 3 4"<<endl;
            cout<<"Enter button to turn off 5 to return: ";
            cin>>btn3;
            if (btn3 <=4)
            led3[btn3] = 0;
            }while (btn3 != 5);
      }
      else if (button == '4')
    {   system("cls");
            int btn4;
            do {
            cout<<led4[0];cout<<" ";cout<<led4[1];cout<<" ";cout<<led4[2];cout<<" ";cout<<led4[3];cout<<" ";cout<<led4[4];cout<<" "<<endl;
            cout<<"0 1 2 3 4"<<endl;
            cout<<"Enter button to turn off 5 to return: ";
            cin>>btn4;
            if (btn4 <=4)
            led4[btn4] = 0;
            }while (btn4 != 5);
            
      }      
      } while(button != '5');
    return 0;
}

screen shot 1
http://www.flickr.com/photos/26028708@N04/3823397550/
screen shot 2
http://www.flickr.com/photos/26028708@N04/3823396864/in/photostream/

thanks for your help :slight_smile:

my new question is this... the arduino only has so many outputs how would i control 144 leds? 12 sets of 12? like

button1 has 1-12 button2 has 1-12 etc etc....