Function and array question.


code: [select]
#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 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 does:
goes function , changes value of array 1 (on) 0 (off) , resets on when leave function , return it.

what needs do:
not reset function. want able push button 1 , turn off 2 or 3 or whatever leds , go main menu , push button 2 , same return button , and turn rest of lights off. need on once need able pick , choose lights shut off.

also know isnt correct syntax arduino once on command line can make work arduino.

thanks-
dustin

hi,

it not clear me, wynt program do. think 1 problem is, declare lcal arays led1[], led2[] inside function. if leave function , reenter later on, array created new , initialized 1. try decalre arrays befor fucntion button1 global variables:

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

int button1 ()
{
...

regards
mike


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Function and array question.


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums