exceuse me,how to explain this code

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

void setup() {

void loop() {
   inbyte = Serial.read();
   switch (inbyte){
     case '0':
     case '1':

void touch(){
 val = digitalRead(apin);
 if(val==HIGH)//set 1=HIGH
     digitalWrite(led,HIGH);//pin3 access
     Serial.println("no 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.


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.


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.