add integers to port names (solved)

how can i add integers to port names

example....

int pin1 = 1;
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;

here if the result was 2 of a given function / task ect i want to set output 2 on . without having "if = 2 , if = 3 , if = 4...." for each output.

setpin = "pin" + 2 // setpin = "pin2" this is what i want to be able to do but cant get it to do.

digitalWrite(setpin, LOW); // i normally get an error here

any ideas ?

any help would be really good

cheers

luke

Use an array
pin[] = { 4,5,7,8};

Then when you want to use it:-
digitalWrite(pin[result], HIGH);

That was the quickest reply i had on a forum. I think you hit the nail on the head , i will check it out tommorrow many thanks again

I always use that technique (an array of pin numbers) when I build a serious program.

It has another advantage in insulating all of your logic in code from the actual circuit. If you want to move wires to different pins on the board, you only change the pin number constant in one place in the code, not every place you reference it.

hey i cant seem to get this to work please can someone tell me where im going wrong im very new to the coding so it maybe something simple.

Example to set pin 5 on via an array

int pinCount = 7;

int pin[] = {5 ,6 ,7 ,8 ,9 ,10 ,11}; //Is this right ?

void setup()

{
int thisPin;
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(pin[thisPin], OUTPUT); // this part seems to count up ok

}
}

**digitalWrite(pin[5] , HIGH); //example to set pin 5 on **

So what have i done wrong guys ??

thanks again for your help

luke

When you put an item in the array, you have to reference it by the array index, remembering that the first element of the array is index 0!

Try this:

const int ledA = 0;      // or other mnemonic according to the pin's function
const int ledB = 1;
const int ledC = 2;
const int ledD = 3;
const int ledE = 4;
const int ledF = 5;
const int ledG = 6;

const int pinCount = 7; 

int pin [pinCount] = { 5 ,6 ,7 ,8 ,9 ,10 ,11 }; 



void setup() {
  
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode (pin [thisPin], OUTPUT); 
  }
  
}


void loop () {

  digitalWrite (pin [ledA], HIGH);    //example to set pin 5 on  

}

To set Pin 5, using your example:

digitalWrite(pin[0], HIGH);    //example to set pin 5 on

This is because, pin[0] = 5.

The idea behind the array is to translate a list of numbers to another list of numbers (usually a contiguous set of numbers to a random set of numbers).

If you just want to set the pin directly, assuming you know the pin number, then just use:

digitalWrite(5, HIGH);

The same would apply if you had that pin number stored directly in a variable, such as:

pinnumber = 5;

digitalWrite(pinnumber, HIGH);

I hope I'm helping matters, and not making it confusing for you.

b

thanks again andy but i need a way of controlling the outputs via a interger. Im building a web form page that just outputs the pin number that needs to be set.

I simply need to be able to have

(webserver code.......................

  • output "5" )

so all i want be able to do in theroy is ...

PINtoset = 5 // is this possible ??

sorry if i hav got a bit confusied

cheers
luke

Sure, you can do that directly.

I was just trying to advocate for good programming practice to insulate your basic logic from the details of the hardware connection. This is probably more important for large, more complex systems than your application.

But think about the case where you might have to change the circuitry and have the Arduino program use different pin connections. That would require you also changing the web code to address the new pin number.

Instead, you could define constants in your web code the same as what I had in my example (like ledC = 2). Then , from your web code, e.g., you send the number that means "LED C" (i.e., 2) to the Arduino program. When it receives that code, it uses it as an index into the pinNumber array to figure out which pin to write the HIGH value to. In my example that would be pin [2] = 7.

In this way, your web code never has to know anything about pin numbers on your Arduino, or even what the Arduino program will do with that code. This is just good modular system design.

But, as I said, it may be overkill for your application; I was just explaining the principle.

Thanks guys i have solved it ! it was simple than i thought .

this is my solution.... it may not be right but it works fine !

int pinCount = 7;

int pin[] = {5 ,6 ,7 ,8 ,9 ,10 ,11};  





void setup()


{
  int thisPin;
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
   pinMode(pin[thisPin], OUTPUT);  // this part seems to count up ok
 
}
}



digitalWrite(5 , HIGH);    // sets 5 on 

digitalWrite(4, HIGH);   // sets 4 on

It works fine, but you are not taking full advantage of keeping the array of pin numbers, except to replace 7 statements with one loop in setup ():

pinMode (5 , OUTPUT);
pinMode (6 , OUTPUT);
pinMode (7 , OUTPUT);
pinMode (8 , OUTPUT);
pinMode (9 , OUTPUT);
pinMode (10 , OUTPUT);
pinMode (11 , OUTPUT);

would do the same thing.