For statement inside setup and loop function

const int pos_00_sensorPIN = 22;

here are the rest of the pos_##_sensorPIN = # declared

int positions_check[6] ={pos_00_sensorPIN,pos_01_sensorPIN,pos_02_sensorPIN,pos_10_sensorPIN,pos_11_sensorPIN,pos_12_sensorPIN,};

setup() {
  for(int i=0; i<2; i++) 
     pinMode(positions_check [i],INPUT);//setting the context of this table as inputs
}//setup



loop() {
   for(int i=0; i<2 ;i++) 
      positions_check[i] = digitalRead(positions_check [i]); //reads the current status of position sensors
}//loop

What i want to ask are these “for” valid inside the setup and loop functions?
for an unknown reason the brackets in positions_check [ i ] are not shown

Moderator edit: [code][/code] tags added. (Nick Gammon)

Read this before posting a programming question

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

pantherturm:
What i want to ask are these “for” valid inside the setup and loop functions?
for an unknown reason the brackets in positions_check are not shown
const int pos_00_sensorPIN = 22;
here are the rest of the pos_##_sensorPIN = # declared
int positions_check[6] ={pos_00_sensorPIN,pos_01_sensorPIN,pos_02_sensorPIN,pos_10_sensorPIN,pos_11_sensorPIN,pos_12_sensorPIN,};
setup() {

  • for(int i=0; i<2; i++)*
    pinMode(positions_check ,INPUT);//setting the context of this table as inputs
    }//setup
    loop() {
    * for(int i=0; i<2 ;i++)*
    positions_check = digitalRead(positions_check ); //reads the current status of position sensors
    }//loop
    [/quote]

How about posting all your code? In code tags. Compiling what you posted, I got:

sketch_nov09c:0: error: 'pos_00_sensorPIN' was not declared in this scope
sketch_nov09c:0: error: 'pos_01_sensorPIN' was not declared in this scope
sketch_nov09c:0: error: 'pos_02_sensorPIN' was not declared in this scope
sketch_nov09c:0: error: 'pos_10_sensorPIN' was not declared in this scope
sketch_nov09c:0: error: 'pos_11_sensorPIN' was not declared in this scope
sketch_nov09c:0: error: 'pos_12_sensorPIN' was not declared in this scope
sketch_nov09c:2: error: ISO C++ forbids declaration of 'setup' with no type
sketch_nov09c.cpp: In function 'int setup()':
sketch_nov09c:2: error: new declaration 'int setup()'
/Applications/Arduino_1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Arduino.h:117: error: ambiguates old declaration 'void setup()'
sketch_nov09c.cpp: At global scope:
sketch_nov09c:9: error: ISO C++ forbids declaration of 'loop' with no type
sketch_nov09c.cpp: In function 'int loop()':
sketch_nov09c:9: error: new declaration 'int loop()'
/Applications/Arduino_1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Arduino.h:118: error: ambiguates old declaration 'void loop()'

This compiles, so yes, you can use “for” inside setup and loop:

int positions_check[6] ={ 1,2,3,4,5,6 };

void setup() {
  for(int i=0; i<2; i++) 
    pinMode(positions_check [i],INPUT);//setting the context of this table as inputs
}//setup

void loop() {
  for(int i=0; i<2 ;i++) 
    positions_check[i] = digitalRead(positions_check [i]); //reads the current status of position sensors
}//loop

Cheers mate! It took me a while though, to figure out how to edit what you've asked me :stuck_out_tongue: haven't post anything on a forum before!

This looks very weird though:

for(int i=0; i<2 ;i++) 
    positions_check[i] = digitalRead(positions_check [i]); //reads the current status of position sensors

Is position_check the pins numbers? After you go through the loop once they will be either 0 or 1.

const int pos_20_sensorPIN = 22; //these are the pins that are connected to every sensor. I have more than 3 in my project...
const int pos_21_sensorPIN = 23;
const int pos_22_sensorPIN = 24;

int positions_check[3] = {pos_20_sensorPIN,pos_21_sensorPIN,pos_22_sensorPIN};//assigning them to a table for easier management

setup() {
for(int i=0; i<2; i++)
   pinMode(positions_check[i],INPUT); //setting positions_check[i] as an input
}

loop() {
for(int i=0; i<2; i++)
   positions_check[i] = digitalRead positions_check[i]; //reads the current status of the position sensors
}

My project it’s about a parking lot, in which i want to check for empty parking spaces or if it is full. pos_#_sensorPIN are sensors placed on parking spaces. Since there are more than 3 sensors i wanted to put them in a table so that i won’t have to check every sensor individually. I have compiled this code with the version 1.01 and it was ok. And yes it’s 0 and 1 that i want to check. If it’s 1 space is occupied if it’s 0 then it’s free. I just wanted to know if the “for” are valid and since i’ve managed to compile the code, i thought i might have some runtime errors.
I’m sorry about my previous post but as said above i’m new to posts and i didn’t know how to post properly.

How about posting code that compiles? Yours gave:

sketch_nov09f:6: error: ISO C++ forbids declaration of 'setup' with no type
sketch_nov09f.cpp: In function 'int setup()':
sketch_nov09f:6: error: new declaration 'int setup()'
/Applications/Arduino_1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Arduino.h:117: error: ambiguates old declaration 'void setup()'
sketch_nov09f.cpp: At global scope:
sketch_nov09f:11: error: ISO C++ forbids declaration of 'loop' with no type
sketch_nov09f.cpp: In function 'int loop()':
sketch_nov09f:11: error: new declaration 'int loop()'
/Applications/Arduino_1.0.1.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Arduino.h:118: error: ambiguates old declaration 'void loop()'
sketch_nov09f:13: error: invalid conversion from 'int (*)(uint8_t)' to 'int'
sketch_nov09f:13: error: expected `;' before 'positions_check'

You can't leave "void" off the front of setup and loop.

... since i've managed to compile the code, ...

How, exactly?

I forgot to add void before setup and loop and the parentheses when i use the function digitalRead. With these changes the code compiles.

int positions_check[3] = {pos_20_sensorPIN,pos_21_sensorPIN,pos_22_sensorPIN};//assigning them to a table for easier management

Why are you setting positions_check to a bunch of pin numbers here and then

for(int i=0; i<2; i++)
   positions_check[i] = digitalRead positions_check[i]; //reads the current status of the position sensors

setting them to the values read from the pins?

After the first iteration of loop(), they array will contain either 0 or 1, in each position. From then on, you are only reading from two pins that were not among the set you first read from.

You don’t seem to understand the difference between the state of a pin and the number of the pin?

pantherturm:
I forgot to add void before setup and loop and the parentheses when i use the function digitalRead. With these changes the code compiles.

However, you are still over-writing your array of pin numbers with the values read from those pins. After you have read every pin once, all your subsequent code will just be reading pins 0 and 1 - which is unlikely to give you anything useful.

PaulS:
You don't seem to understand the difference between the state of a pin and the number of the pin?

I mentioned that in reply #6 but was ignored.