语言C我可以帮助解释代码吗? [英] Language C can I help to explain de code ?

查看:62
本文介绍了语言C我可以帮助解释代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





你能帮我理解下面的代码吗?

Hi,

Could you please help me understand the following code?

int usbChipPin=12;
int SensorPin=7;
int BlueLedPin=6;
int RedLedPin=5;
int counter=0;
int resetCounter=0;
int ledState=1;
int ledDirection=1;
boolean isSend=false;
unsigned long pulseDuration=0;


void setup() {
  // make led pin ready
  pinMode(BlueLedPin, OUTPUT);
  // make wake up pin ready
  pinMode(12, INPUT);
}

void loop() {

  // make led light steady while sensing, or dim led while idle
  if (counter>0 && isSend == false) {
    analogWrite(BlueLedPin, 200);
  } else if (isSend == false) {
    if (ledDirection == 1) {
      analogWrite(BlueLedPin, ledState);
      if (ledState == 125) { ledDirection=-1; } else { ledState = ledState + 1; }
    } else {
      analogWrite(BlueLedPin, ledState);
      if (ledState < 1) { ledDirection=1; } else { ledState = ledState - 1; }
    }
  } else {
    analogWrite(RedLedPin, 255);
    analogWrite(BlueLedPin, 0);
  }
  
   // measure distance: send "Ping"
   pinMode(SensorPin, OUTPUT);
   digitalWrite(SensorPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(SensorPin, LOW);

   // measure distance: listen for "Ping"
   pinMode(SensorPin, INPUT);
   pulseDuration=pulseIn(SensorPin, HIGH);
 
   // if someone is in reach, count up, or reset if not (Se alguém estiver no alcance, contar, ou redefinir, se não)
   if ( pulseDuration < 10000 ) {
     counter = counter + 1;
     if (counter > 50) { resetCounter=0; }
   } else {
     resetCounter = resetCounter +1;
       if (resetCounter > 100) {
       counter=0;
       resetCounter=0;
       isSend=false;
       analogWrite(RedLedPin, 0);
       }
   }
 
  // if someone is there for 300 loops, start up pc
  if (isSend == false && counter > 100) {
  // reset usb-to-serial chip
  pinMode(usbChipPin, OUTPUT);
  digitalWrite(usbChipPin, LOW);
  delay(250);
  pinMode(12, INPUT);
  // don't do it again
  isSend=true;
  }
  
  delay(10);
}

谢谢

推荐答案

您是否知道逐行解释代码的工作量是多少?

每一行都需要一段解释!例如:

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();



创建一个名为next的新变量,它可以包含一个整数值。从先前声明的Random实例r,调用Next方法获取一个新的随机数,并将其分配给next变量。



可以你想象我们需要多长时间才能解释一个像你的例子那样相当短的代码片段,一行一行?



不会发生这种情况。如果您有特定问题,请询问有关它的问题。但首先考虑一下 - 您是否希望坐下45分钟并且没有充分的理由输入逐行描述?


Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a reasonably short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?


这篇关于语言C我可以帮助解释代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆