Arduino-怎么点亮LED?

初级点灯

int ledPin = 13; // LED connected to digital pin 13
void setup()
{
 pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
 digitalWrite(ledPin, HIGH); // sets the LED on
 delay(1000); // waits for a second
 digitalWrite(ledPin, LOW); // sets the LED off
 delay(1000); // waits for a second
}

进阶点灯

1>一边点灯,一遍执行其他代码
int ledPin = 13; // LED connected to digital pin 13
int value = LOW; // previous value of the LED
long previousMillis = 0; // will store last time LED was updated上一次激活时间
long interval = 1000; // interval at which to blink (milliseconds)间隔
void setup()
{
     pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
	// here is where you'd put code that needs to be running all the time.
    
	// check to see if it's time to blink the LED; that is, is the difference
	// between the current time and last time we blinked the LED bigger than
	// the interval at which we want to blink the LED.
    if (millis() - previousMillis > interval) {
    	previousMillis = millis(); // remember the last time we blinked the LED
		// if the LED is off turn it on and vice-versa.
        if (value == LOW)
        	value = HIGH;
        else
            value = LOW;
		digitalWrite(ledPin, value);
    }
}
2>按钮点灯
int ledPin = 13; // choose the pin for the LED
int inPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
	pinMode(ledPin, OUTPUT); // declare LED as output
	pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
    val = digitalRead(inPin); // read input value
 	if (val == HIGH) { // check if the input is HIGH (button released)
 		digitalWrite(ledPin, LOW); // turn LED OFF
 	} else {
 		digitalWrite(ledPin, HIGH); // turn LED ON
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农菌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值