Turn on the LED when button is pressed, turn it off otherwise What we want to achieve is simple: when the button is not pressed, the LED is off. And when we press the button the LED should be on. The code #define LED_PIN 8 #define BUTTON_PIN 7 voidsetup(){ pinMode(LED_PIN, ...
=buttonState){buttonState=reading;// only toggle the LED if the new button state is HIGHif(buttonState==HIGH){ledState=!ledState;}}}// set the LED:digitalWrite(ledPin,ledState);// save the reading. Next time through the loop, it'll be the lastButtonState:lastButtonState=reading...
Loop()部分: 非常简单,通过调用库里的函数tick(),时刻检测按钮的动作,按钮1就用button1.tick(),如果是多按钮就用button2.tick()...buttonN.tick(),就可以了,只不过这个示例程序里,并没有给button2任何动作,所以loop里button2.tick()是多余的。 在loop()函数后面,开始完善触发事件的函数:比如触发单击(attach...
pinMode(buttonPin, INPUT); } voidloop() { //… } 1.2 loop() 在setup() 函数中初始化和定义了变量,然后执行 loop() 函数。顾名思义,该函数在程 序运行 过程中不断的循环,根据一些反馈,相应改变执行情况。通过该函数动态控制 Arduino 主控板。 示例 intbuttonPin=3;//setup 中初始化串口和按键针脚....
1.2 loop() 二、结构控制 2.1 if 2.2 if...else 2.3 for 2.4 switch case 2.5 while 2.6 do... while 2.7 break 2.8 continue 2.9 return 2.10 goto 三、扩展语法 3.1 ;(分号) 3.2 {}(花括号) 3.3 //(单行注释) 3.4 /* */(多行注释) ...
*/if(error==1)//skip loop if no controller foundreturn;if(type==2){//Guitar Hero Controllerps2x.read_gamepad();//read controllerif(ps2x.ButtonPressed(GREEN_FRET))Serial.println("Green Fret Pressed");if(ps2x.ButtonPressed(RED_FRET))Serial.println("Red Fret Pressed");if(ps2x.ButtonPres...
<script language="javascript"> <!-- function onsub() { //生成新月的字符串 ...
1.2 loop() 二、结构控制 2.1 if 2.2 if...else 2.3 for 2.4 switch case 2.5 while 2.6 do... while 2.7 break 2.8 continue 2.9 return 2.10 goto 三、扩展语法 3.1 ;(分号) 3.2 {}(花括号) 3.3 //(单行注释) 3.4 /* */(多行注释) ...
*/if(error==1)//skip loop if no controller foundreturn;if(type==2){//Guitar Hero Controllerps2x.read_gamepad();//read controllerif(ps2x.ButtonPressed(GREEN_FRET))Serial.println("Green Fret Pressed");if(ps2x.ButtonPressed(RED_FRET))Serial.println("Red Fret Pressed");if(ps2x.ButtonPres...
voidloop() {btstate= digitalRead(11); } Connect one of the button pins to pin 11 through a ~ 5 kOhm resistor, and the other pin to ground (GND). Press the button. You can now use ‘digitalWrite’ or other other functions to switch on an LED, relay, adjust PWM duty cycle (for ...