Go Down

Topic: Initalizing many boolean statements (Read 650 times) previous topic - next topic

Processed

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: [Select]

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!

PaulS

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

You are trying, on this line:
Code: [Select]
      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: [Select]
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: [Select]
boolean ON[54];
would be sufficient to create an array of 54 elements, all set to false.

Processed

Worked perfectly, thanks!

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


Code: [Select]
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

KeithRB

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

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Processed

Thank you guys so much! Amazing help and I am so grateful!  XD

holmes4

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

Henry_Best


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.

westfw

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

Code: [Select]
for (int i =0; i <= 53; i++)
   pinMode(i, OUTPUT);


??

JarkkoL

#9
Jun 27, 2013, 05:01 am Last Edit: Jun 27, 2013, 06:16 am by JarkkoL Reason: 1
Code: [Select]

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.

lloyddean

#10
Jun 27, 2013, 05:47 am Last Edit: Jun 27, 2013, 05:49 am by lloyddean Reason: 1
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: [Select]

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);
   }
}

JarkkoL

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.

Go Up