工作室授课记录

2018-11-18

变量

类型

  • int
  • byte
  • string
  • double
  • float
举例

点亮led

int led = 6;
void setup() {
  // put your setup code here, to run once:
    pinMode(led, OUTPUT);   //3,
    Serial.begin(9600);
    Serial.println("system start:");
}

void loop() {
  // put your main code here, to run repeatedly:
    digitalWrite(led, HIGH);
    Serial.println("led on");
    delay(500);
}

变量的声明范围

int lightDegree = 20;

void setup() {
  pinMode(4, INPUT);
  pinMode(8, INPUT);
  pinMode(6, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int big = digitalRead(4);
  int small =  digitalRead(8);
  
  if ( big == HIGH) {
    lightDegree= lightDegree + 25;  //pwm : 0~255;
    if (lightDegree >255){
        lightDegree = 255;
    }
    analogWrite(6, lightDegree);
    Serial.println(lightDegree);
    delay(1000);
  }  
  if ( small == HIGH) {
    lightDegree= lightDegree - 25;  //pwm : 0~255;
    if (lightDegree < 20){
        lightDegree = 20;
    }
    analogWrite(6, lightDegree);
    Serial.println(lightDegree);
    delay(1000);
  }  
}

常量

猜你喜欢

转载自blog.csdn.net/acktomas/article/details/84201085