[DemoCode下载] NUC131在Arduino下的Button操作

[复制链接]
 楼主| 稳稳の幸福 发表于 2024-2-28 10:17 | 显示全部楼层 |阅读模式
  1. /*
  2.   Button

  3. Turns on and off a light emitting diode(LED) connected to digital  
  4. LED1 is light, when pressing a pushbutton attached to SW1. It work for NuEdu-NUC240, NuEdu-NANO130 and NuEdu-M451.
  5. LED3 is light, when pressing a pushbutton attached to SW2. It work for NuEdu-NUC240, NuEdu-NANO130 and NuEdu-M451.
  6. LED5 is light, when pressing a pushbutton attached to SW3. It work for NuEdu-NUC240, NuEdu-NANO130 and NuEdu-M451.
  7. LED7 is light, when pressing a pushbutton attached to SW4. It work for NuEdu-NUC240, NuEdu-NANO130 and NuEdu-M451.

  8. Note: This sample does not support NuEdu-NUC131.

  9. This example code is in the public domain.

  10. */

  11. // constants won't change. They're used here to
  12. // set pin numbers:
  13. #define NUM 4
  14. const int buttonPin[NUM] = {44, 39, 24, 25 };     // the number of the pushbutton pin,  SW1, SW2, SW3, SW4
  15. const int ledPin[NUM]    = {34, 36, 38, 27 };     // the number of the LED pin, LED1, LED3, LED5, LED7

  16. // variables will change:
  17. int buttonState = 0;         // variable for reading the pushbutton status

  18. void setup() {
  19.   // initialize the LED pin as an output:
  20.   for(int i=0;i<NUM;i++)
  21.    pinMode(ledPin[i], OUTPUT);      
  22.   // initialize the pushbutton pin as an input:
  23.   for(int i=0;i<NUM;i++)
  24.     pinMode(buttonPin[i], INPUT);     
  25. }

  26. void loop(){
  27.   // read the state of the pushbutton value:
  28.    for(int i=0;i<NUM;i++)
  29.    {
  30.       buttonState = digitalRead(buttonPin[i]);
  31.    
  32.       // check if the pushbutton is pressed.
  33.       // if it is, the buttonState is HIGH:
  34.       if (buttonState == HIGH) {     
  35.         // turn LED on:   
  36.         digitalWrite(ledPin[i], HIGH);  
  37.       }
  38.       else {
  39.         // turn LED off:
  40.         digitalWrite(ledPin[i], LOW);
  41.       }
  42.   }
  43. }
对比KEIL下的开发,这个怎么样?
xixi2017 发表于 2024-2-28 10:52 | 显示全部楼层
按键操作确实很方便了。
天灵灵地灵灵 发表于 2024-2-28 15:04 | 显示全部楼层
发现用这个开发板,会提高移植性,因为代码是一致的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

203

主题

3409

帖子

8

粉丝
快速回复 在线客服 返回列表 返回顶部

203

主题

3409

帖子

8

粉丝
快速回复 在线客服 返回列表 返回顶部