exceuse me,how to explain this code

int val;
int led=13;
int apin=3;
char inbyte;
bool mts=true;

void setup() {
 pinMode(apin,NULL);
 Serial.begin(9600);
 if(digitalRead(apin)==LOW){Serial.println(apin);}
}

void loop() {
if(mts==true){
 touch();
 }else{mts=false;}
 
 if(Serial.available()>0){//序列初始化
   inbyte = Serial.read();
   switch (inbyte){
     case '0':
     apin=NULL;
     mts=false;
     Serial.println("Off");
     break;
     
     case '1':
     apin=3;
     mts=true;
     Serial.println("On");
     break;
   }
  }
}

void touch(){
 val = digitalRead(apin);
 if(val==HIGH)//set 1=HIGH
    {
     digitalWrite(led,HIGH);//pin3 access
     delay(100);
     Serial.println("no touch");
     }
 if(val==LOW){
     digitalWrite(led,LOW);
     delay(100);
     Serial.println("touch"); 
      }
 }

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

It looks like it was written by someone with little understanding of programming concepts or the language.

 pinMode(apin,NULL);

NULL isn't really a good term here. It probably evaluates to 0 and ends up being treated as INPUT, but it would be much more proper to use INPUT if you mean INPUT.

if(mts==true){
  touch();
  }else{mts=false;}

Think about this, if it isn't true then what else could it possibly be? What's the point of the else clause there?

Aside from that it looks like it looks for a character from the serial line and sets up to use either pin 0 or pin 3 as an input and sets a boolean flag to decide whether or not to call the touch function. That function just reads a pin and sets an LED on or not depending on the state of that pin.

Which part exactly were you not understanding?

thx a lot,i am understanding.

Whenever a '1' comes in on the serial input, the sketch will start repeatedly checking pin 3. Whenever a '0' comes in, the sketch will stop checking pin 3.

Each time the sketch checks pin 3, it sets the pin 13 led, delays 1/10th of a second, and prints 'touch' or 'no touch' to the serial.

'apin' is a variable. Perhaps at one stage this sketch was going to allow you to detect touches on various pins depending on what you sent to the serial. type a '1' and it starts checking pin 3, type a '2' and it checks pin 4, etc.

val and inbyte are global for no reason - they are only ever used locally.

The timing of touch() is weird. The printing to the serial will get done after the delay, which means at the time of the start of the next 10th of a second. But presumably this is just debugging and will be removed later.