首先创建一个类 class, 类里面的内容叫做封装
#include <Arduino.h>
class Led
{ // 创建一个类
public:
byte pin;
void on();
void off();
void toggle();
};
Led ledGreen, ledYellow, ledRed; // 创建对象
void Led::on(){ //开
digitalWrite(pin, HIGH);
}
void Led::off(){ //关
digitalWrite(pin, LOW);
}
void Led::toggle(){
digitalWrite(pin, !digitalRead(pin));
}
void setup() {
Serial.begin(115200);
ledGreen.pin = 33; //设置LED引脚
pinMode(33,OUTPUT);
ledGreen.on(); delay(1000);
ledGreen.off();delay(1000);
}
void loop(){
ledGreen.toggle();
delay(1000);
}