'for' looping problem

I'm trying write a piece of code that enables setting digital pins high or low over serial port (Max/MSP). I used two 'for' loops to loop through an array containing the pin numbers 0-13 to set them to output mode and to check whatever int is coming from the serial (between 0 and 13). The second loop doesn't work, I can't set any of the pins high. Where did I go wrong?

int pinCount = 14;
int digiPins[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13};

void setup() {
Serial.begin(9600);
int thisPin;
for (int thisPin = 0; thisPin < pinCount; thisPin++){
pinMode(digiPins[thisPin], OUTPUT);
}

}
void loop() {
for(int thisPin = 0; thisPin < pinCount; thisPin++){
if(Serial.read() == thisPin){
digitalWrite(thisPin,HIGH);
}
else {
digitalWrite(thisPin,LOW);
}
}

You need to check if there is data to read first.

if (Serial.available() > 0){

// read the byte, convert from ASCII character '5' for example to 0x05, check ASCIItable.com for the characters
thisPin = Serial.read() - 48;  // subtract 48 if pins are 0-9. 

digitalWrite (digiPins[thisPin], HIGH); // set the pin  high
delay (100); // hold it high for 1/10th of a second?
digitalWrite (digiPins[thisPin], LOW);  // and back low.
}

Have to complicate things more if want to accept 2 digits, like 02 thru 13, or 1 or 2 digits. If first digit is 2-9, there is no nd digit. If first digit is 1, then it may be 10,11,12 ... 19.

Great, thanks!

You probably don't want to be messing with pins 0 and 1, since they are used for serial communication.

Checking the pins with a scope but they don't seem to change their state (from low to high). Here's the full code:

int pinCount = 14;
int digiPins[] = {
0,1,2,3,4,5,6,7,8,9,10,11,12,13};
int thisPin;

void setup() {
Serial.begin(9600);

for (int thisPin = 0; thisPin < pinCount; thisPin++){
pinMode(digiPins[thisPin], OUTPUT);
}
}
void loop() {
if (Serial.available() > 0){
// read the byte, convert from ASCII character '5' for example to 0x05, check ASCIItable.com for the characters
thisPin = Serial.read() - 48; // subtract 48 if pins are 0-9.

digitalWrite (digiPins[thisPin], HIGH); // set the pin high
delay (1000); // hold it high for 1/10th of a second?
digitalWrite (digiPins[thisPin], LOW); // and back low.
}
delay(1);
}

int digiPins[] = {
0,1,2,3,4,5,6,7,8,9,10,11,12,13}; << 0, 1 are the serial port pins - don't include them here
int thisPin;

void setup() {
Serial.begin(9600);

for (int thisPin = 0; thisPin < pinCount; thisPin++){ << 0,1 are the Serial port pins - don't include them here.
pinMode(digiPins[thisPin], OUTPUT);

void loop() {
if (Serial.available() > 0){
// read the byte, convert from ASCII character '5' for example to 0x05, check ASCIItable.com for the characters
thisPin = Serial.read() - 48; // subtract 48 if pins are 0-9.
Serial.print (thisPin); <<< add this print here, confirm what pin you are trying to access

  thisPin = Serial.read() - 48;  // subtract 48 if pins are 0-9.

    digitalWrite (digiPins[thisPin], HIGH); // set the pin  high
    delay (1000); // hold it high for 1/10th of a second?
    digitalWrite (digiPins[thisPin], LOW);  // and back low.

Put an "if" there, since going outside the bounds of an array may cause a crash. Also just use '0' rather than 48, looks nicer. eg.

    thisPin = Serial.read() - '0';  

    if (thisPin >= 0 && thisPin <= 9)
     {
     digitalWrite (digiPins[thisPin], HIGH); // set the pin  high
     delay (1000); // hold it high for one second
     digitalWrite (digiPins[thisPin], LOW);  // and back low.
     }

Plus, this method just won't work with pins 10 onwards. You need to do more serial reads.

I just tested with those amendments and it worked (at least typing in "4567").

Plus, this method just won't work with pins 10 onwards. You need to do more serial reads.

What do you exactly mean?

Still doesn't work, can you show me your amendments?

int pinCount = 14;
int digiPins[] = {
  0,1,2,3,4,5,6,7,8,9,10,11,12,13};
int thisPin;

void setup() {
  Serial.begin(115200);

  for (int thisPin = 0; thisPin < pinCount; thisPin++){
    pinMode(digiPins[thisPin], OUTPUT);     
  }
}
void loop() {
  if (Serial.available() > 0){
    // read the byte, convert from ASCII character '5' for example to 0x05, check ASCIItable.com for the characters
    thisPin = Serial.read() - '0';  

    if (thisPin >= 0 && thisPin <= 9)
    {
      digitalWrite (digiPins[thisPin], HIGH); // set the pin  high
      delay (1000); // hold it high for one second
      digitalWrite (digiPins[thisPin], LOW);  // and back low.
    }

  }
  delay(1);
}

Note I used the higher baud rate, which is my habit.

alkopop79:
Plus, this method just won't work with pins 10 onwards. You need to do more serial reads.

What do you exactly mean?

Well you are doing a single Serial.read. How do you suppose "10" is going to fit into one byte?

The digits 10,11,12,13 ... are two characters. '1', '0', and '1', '1', etc.
So you have to do 2 reads and put the results together so that
digiPins[thisPin]
will work on pin 10, 11, 12, etc.

Or use a different single character to represent pins 10,11,12, etc.
perhaps A =10, B=11, C=12, D=13, E=14, F=15.

The digits 10,11,12,13 ... are two characters. '1', '0', and '1', '1', etc.

Had no idea about that, thanks!

Or use a different single character to represent pins 10,11,12, etc.
perhaps A =10, B=11, C=12, D=13, E=14, F=15.

You mean store symbols in the array instead of ints?

By the way, don't bother testing with 0 and 1, as the serial hardware takes over those 2 ports.

Thanks!

Still doesn't work, tired it with Max/MSP and the Arduino IDE serial monitor. I can see the RX led blinking but the pins won't go high.

int pinCount = 12;
int digiPins[] = {
2,3,4,5,6,7,8,9,10,11,12,13};
int thisPin;

void setup() {
Serial.begin(9600);

for (int thisPin = 0; thisPin < pinCount; thisPin++){
pinMode(digiPins[thisPin], OUTPUT);
}
}
void loop() {
if (Serial.available() > 0){
// read the byte, convert from ASCII character '5' for example to 0x05, check ASCIItable.com for the characters
thisPin = Serial.read() - 48; // subtract 48 if pins are 0-9.
Serial.println(thisPin);
digitalWrite (digiPins[thisPin], HIGH); // set the pin high
delay (1000); // hold it high for 1/10th of a second?
digitalWrite (digiPins[thisPin], LOW); // and back low.
}
delay(1);
}

Can you put your code in [ code ] tags please? And what happened to my "if"? What do you see echoed back in the serial monitor?

Have this start at 2:
for (int thisPin = 0; thisPin < pinCount; thisPin++){
pinMode(digiPins[thisPin], OUTPUT);

"perhaps A =10, B=11, C=12, D=13, E=14, F=15.
You mean store symbols in the array instead of ints?"

No, I chose letters because they are hexadecimal values.
So your array could consist of {2,3,4,5,6,7,8,9,10,11,12,13};
then do Nick's test for whatever characters you decide to use, such as 'A' thru 'F'

int  incomimgPin = Serial.read(); 
    Serial.println(incomingPin);

// test for range 2-9
    thisPin = incomingPin - 48; 
    Serial.println(thisPin);

    if (thisPin >= 0 && thisPin <= 9)
    {
      digitalWrite (digiPins[thisPin], HIGH); // set the pin  high
      delay (1000); // hold it high for one second
      digitalWrite (digiPins[thisPin], LOW);  // and back low.
    }
// test for range A-F
thisPin = incomingPin - 65 + 10;  
    Serial.println(thisPin);
    if (thisPin >= 10 && thisPin <= 15)
    {
      digitalWrite (digiPins[thisPin], HIGH); // set the pin  high
      delay (1000); // hold it high for one second
      digitalWrite (digiPins[thisPin], LOW);  // and back low.
    }

Why, oh why do I get this error even if incomingPin is declared before void setup????: serial_tester_thingy_2:17: error: 'incomingPin' was not declared in this scope

(moderator - need to use the # sign above - results in [ code ] paste_your_code [ / code ] (without the spaces) )

int pinCount = 12;
int digiPins[] = {
  2,3,4,5,6,7,8,9,10,11,12,13};
int thisPin;
//int  incomimgPin;  << oops - incoming mis-spelled

void setup() {
  Serial.begin(9600); 

  for (int thisPin = 0; thisPin < pinCount; thisPin++){
    pinMode(digiPins[thisPin], OUTPUT);      
  }
}
void loop() {
  if (Serial.available() > 0){
    // read the byte, convert from ASCII character '5' for example to 0x05, check ASCIItable.com for the characters
    int incomimgPin = Serial.read();   << mis-spelled incoming here too 
    Serial.println(incomingPin);

// test for range 2-9
    thisPin = incomingPin - 48; 
    Serial.println(thisPin);

    if (thisPin >= 0 && thisPin <= 9)
    {
      digitalWrite (digiPins[thisPin], HIGH); // set the pin  high
      delay (1000); // hold it high for one second
      digitalWrite (digiPins[thisPin], LOW);  // and back low.
    }
// test for range A-F
thisPin = incomingPin - 65 + 10;  
    Serial.println(thisPin);
    if (thisPin >= 10 && thisPin <= 15)
    {
      digitalWrite (digiPins[thisPin], HIGH); // set the pin  high
      delay (1000); // hold it high for one second
      digitalWrite (digiPins[thisPin], LOW);  // and back low.
    }
  
  delay(1);
}