Pages: [1]   Go Down
Author Topic: Initalizing many boolean statements  (Read 551 times)
0 Members and 1 Guest are viewing this topic.
South Carolina
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys! First post ever!

I'm trying to initialize 53 pins and then boolean statements... I'm having a lot of trouble with the boolean statements. I don't know how else to put it besides giving you my portion of the code:

 
Code:
int Pinset[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53}; //Array
boolean ON[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53}; //Array for boolean
void setup()
{
  for(int j=0;j<54;j++)
      pinMode(Pinset[j],OUTPUT); //All these pins will be considered output pins
     
  for (int z=0;z<54;z++)
      boolean ON[z]=false;  //Trouble Portion -- Why won't this work and what's the solution?
     
 Serial.begin(9600);
 
}

I get an error "variable-sized object 'ON' may not be initalized"

What's the solution to bringing in 54 boolean statements without having to type them all out?

Thank you ahead of time for being nice!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I get an error "variable-sized object 'ON' may not be initalized"
You are trying, on this line:
Code:
      boolean ON[z]=false;  //Trouble Portion -- Why won't this work and what's the solution?
To create another array called ON, with a variable size. This is NOT allowed.

To initialize the nth element of the existing array, loose the boolean keyword.

This:
Code:
boolean ON[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53}; //Array for boolean
is wrong, too. The elements in ON should be true or false, not numeric values.

The default value for each element of a global array of booleans is false. So,
Code:
boolean ON[54];
would be sufficient to create an array of 54 elements, all set to false.
Logged

South Carolina
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Worked perfectly, thanks!

But now I'm getting an error saying it wasn't declared in the scope in my loop()!


Code:
int Pinset[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53};

void setup()
{
  for(int j=0;j<54;j++)                //Initializing all pins as OUTPUT pins
      pinMode(Pinset[j],OUTPUT);
     
  boolean ON[54]; //53 boolean statements all set to false
     
 Serial.begin(9600);
 
}



void loop()
{
  int input=Serial.read();
 
  if(input==2)
  {
    ON[2]=!ON[2];  //Arduino doesn't like this
    if(ON[2]==true) //or this
      digitalWrite(Pinset[2],HIGH);
    else
    digitalWrite(Pinset[2],LOW);
  }

}

What'd I do wrong again?  smiley-sweat

Thank you, PaulS
Logged

Offline Offline
Edison Member
*
Karma: 32
Posts: 1377
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Move it out of setup() and into the global section.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4018
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The ON array is declared in the setup() function so it is local to that function and its value is not available in the loop() function.  Declare the ON array  at the start of the program and it will be available globally.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

South Carolina
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you guys so much! Amazing help and I am so grateful!  smiley-lol
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2199
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

By the way the array "pinset" looks to be pointless as each element is set to its own index. So unless you will be changing then late in your code .....

Mark
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

By the way the array "pinset" looks to be pointless as each element is set to its own index. So unless you will be changing then late in your code .....

Mark

Not pointless. He'd need 54 lines of code to set each pin to OUTPUT, if he got rid of it.
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6636
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
He'd need 54 lines of code to set each pin to OUTPUT, if he got rid of it.
Code:
for (int i =0; i <= 53; i++)
   pinMode(i, OUTPUT);

??
Logged

Montreal
Offline Offline
Full Member
***
Karma: 4
Posts: 179
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
enum {num_pins=54};
static bool pin_states[num_pins]={false};

void setup()
{
  for(unsigned i=0; i<num_pins; ++i)
    pinMode(i, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int input=Serial.read();
  if(input<num_pins)
  {
    pin_states[input]=!pin_states[input];
    digitalWrite(input, pin_states[input]?HIGH:LOW);
  }
}
You are welcome! (:

edit: forgot Serial.begin(9600);
edit2: I guess you really just want to switch states of all the pins based on the data read from Serial.
« Last Edit: June 26, 2013, 11:16:18 pm by JarkkoL » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd recommend the Arduino type 'boolean' over 'bool' as a 'boolean' is a single byte and a 'bool' is 2 bytes.

I'd aslo recommend the usage of some type of symbol for pin specification over raw numbers that are hard to go back and change or even read.

Code:
const size_t    PIN_START               =  0;
const size_t    PIN_RANGE               = 54;
const size_t    PIN_TRAP                = PIN_START + 2;

boolean         statePins[pinRANGE] = { false };

void setup()
{
    for ( size_t i = PIN_START; i < PIN_RANGE; ++i )
    {
        pinMode(i, OUTPUT);
    }

    Serial.begin(9600);
}

void loop()
{
    int input = Serial.read();
    if ( input == PIN_TRAP )
    {
        statePins[PIN_TRAP] = !statePins[PIN_TRAP];
        digitalWrite(PIN_TRAP, statePins[PIN_TRAP] ? HIGH : LOW);
    }
}
« Last Edit: June 26, 2013, 10:49:28 pm by lloyddean » Logged

Montreal
Offline Offline
Full Member
***
Karma: 4
Posts: 179
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'd recommend the Arduino type 'boolean' over 'bool' as a 'boolean' is a single byte and a 'bool' is 2 bytes.
bool is 1 byte on Arduino Uno at least.
Logged

Pages: [1]   Go Up
Jump to: