Serial.available not turning LED on

I am using Processing with Arduino and I know for sure Processing is sending data to Arduino because I am using print out statements to see what Processing is sending, and I can see Processing is sending the correct values. However, Arduino seems to not be turning the LED on when put inside this Serial.available statement:

Processing:

   myPort.write("20");  
    println("20");

Arduino:

char val;
 
void setup() 
{ 

  Serial.begin(9600); // Start serial communication at 9600 bps

  pinMode(13, OUTPUT);
    pinMode(12, OUTPUT);
      pinMode(11, OUTPUT);
        pinMode(10, OUTPUT);
          pinMode(9, OUTPUT);
            pinMode(8, OUTPUT);
              pinMode(7, OUTPUT);
                pinMode(6, OUTPUT);
                  pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
pinMode(1, OUTPUT);
pinMode(0, OUTPUT);
} 
 
void loop() 
{  
     if (Serial.available()) 
   { // If data is available to read,
    val= Serial.read(); // read it and store it in val
   
   digitalWrite(val, HIGH);  

   }
      
}

Any idea what could be wrong?

D0 and D1 are used as Serial RX and TX on an UNO.
Don't use them as general I/O

  pinMode(13, OUTPUT);
    pinMode(12, OUTPUT);
      pinMode(11, OUTPUT);
        pinMode(10, OUTPUT);
          pinMode(9, OUTPUT);
            pinMode(8, OUTPUT);
              pinMode(7, OUTPUT);

What
the
hell
is
this?

pinMode(1, OUTPUT);
pinMode(0, OUTPUT);

Why are you f**king with the hardware serial pins?

   val= Serial.read(); // read it and store it in val
   
   digitalWrite(val, HIGH);

You send a string. There is no pin '2' (50) or '0' (48).

Why you are sending "20", when there aren't 20 pins is a mystery, too.

Ok, to answer some questions: I have two arduinos hooked up through USB. Obviously I don't have 20 pins, so I am using the % operator to allocate pins after 13 to the other arduino starting at pin 4 of that other Arduino. I didn't know 0 and 1 pins were hardware serial pins...Took them off now.

I changed Serial.available() to

     if (Serial.available()) 
   { // If data is available to read,
    val= Serial.read(); // read it and store it in val
   int pin= int(val); 
   digitalWrite(pin, HIGH);  

   }

No lights turning on though...Processing is sending over pin numbers 4, ,5, 12 and Arduino isn't turning the LED on.

    val= Serial.read(); // read it and store it in val

And? What IS in val? If you send a string, "20", you read ONE character, the '2'. The ASCII value for '2' is 50.

Why do I sound like a broken record?

Casting a character to an int is useless. You must CONVERT the character to an int. LOOK at the ASCII table for a clue.

You'll also need to look at any of the bazillion threads on how to receive multi-character numbers, like 12.

Very basic serial LED control.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
      Serial.println("LED ON");
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
      Serial.println("LED OFF");
    }
    readString="";
  } 
}

I fixed the problem. I had similar problem on my other projects using Arduino - Processing and now this all makes sense! This char issue was my problem this whole time! Thank you all for your help!

This was the solution:

    if (Serial.available()) 
   { // If data is available to read,
    val= Serial.read(); // read it and store it in val
    
  inString += (char)val;
  pin = inString.toInt();
   digitalWrite(pin, HIGH);  

   }

I am new to Arduino-Processing communication so this was a learning experience. I am very glad to have all of you and your help.

I implemented something to take into account pins like 10, 11, or 12 and now my Arduino lights aren't turning on again...What did I screw up?

Processing component:

   myPort.write(12);  
   println("12");

Arduino:

void loop() 
{  
     if (Serial.available() > 0) 
   { // If data is available to read,
   int val= Serial.read(); // read it and store it in val
        if (isDigit(val)) {
      // convert the incoming byte to a char
      // and add it to the string:
      inString += (char)val;
    }
    
       // if you get a newline, print the string,
    // then the string's value:
    if (val == '\n') {
      
   digitalWrite(inString.toInt(), HIGH); 
      // clear the string for new input:
      inString = "";
    }  
   }   
      
}

I tried ZoomKat's solution below but it didn't work-- no led turning on.

Arduino:

void loop() 
{  
     if (Serial.available() > 0) 
   { // If data is available to read,
   char val= Serial.read(); // read it and store it in val
        if (isDigit(val)) {
      // convert the incoming byte to a char
      // and add it to the string:
      inString += val;
    }
    
   digitalWrite(inString.toInt(), HIGH); 
      // clear the string for new input:
      inString = "";
      
   }      
}

Processing:

  myPort.write(11);
  println("11");

There is a difference, you know, between myPort.write("20"); and myPort.write(11); (besides the numeric value).

Are you using the analog inputs pins? A0 to A5 can be access as D14-D19, so maybe you don't need the 2nd Arduino at all.

CrossRoads:
Are you using the analog inputs pins? A0 to A5 can be access as D14-D19, so maybe you don't need the 2nd Arduino at all.

That is really good to know because now I won't need that second Arduino.

I have SOMETHING working but it isn't working quite right. One of the line makes the light work but it also makes another light turn on which isn't right....Removing that line turns all lights off, so I don't know what's going on:

Processing:

myport.write("11");

Arduino:

void loop() 
{  
     if (Serial.available() > 0) 
   { // If data is available to read,
   char val= Serial.read(); // read it and store it in val
        if (isDigit(val)) {
      // convert the incoming byte to a char
      // and add it to the string:
      inString += (char)val;
    }
    //   digitalWrite(inString.toInt(), HIGH);    adding this line makes 2 lights turn on, 11 and 1 other light (7)
//removing the above line makes all lights go out......
    if (val=='\n')
    {
    
   digitalWrite(inString.toInt(), HIGH); 
  
      // clear the string for new input:
      inString = "";
    }
      
   }      
}

The examples in serial input basics are simple reliable ways to receive data.

...R

If you are going to do your best not to use delimiters, look into Serial.parseInt().