当前位置: 代码迷 >> 综合 >> 【掌控板-arduino】5 按键
  详细解决方案

【掌控板-arduino】5 按键

热度:91   发布时间:2023-12-05 20:34:47.0

前言

尝试按键B用于点亮led灯

硬件信息

在这里插入图片描述
按键B使用的是IO2

代码


#include <Arduino.h>
#include <Adafruit_NeoPixel.h>Adafruit_NeoPixel pixels(3,17, NEO_GRB + NEO_KHZ800);// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pinbool keyB_status = false;// variables will change:
int buttonState = 0;         // variable for reading the pushbutton statusvoid led_on(){
    pixels.setPixelColor(0, pixels.Color(10, 0, 0));pixels.show();Serial.println("led_on");
}void led_off(){
    pixels.setPixelColor(0, pixels.Color(0, 0, 0));pixels.show();Serial.println("led_off");
}void setup() {
    Serial.begin(115200);// initialize the LED pin as an output:pinMode(ledPin, OUTPUT);// initialize the pushbutton pin as an input:
// pinMode(buttonPin, INPUT);
}void loop() {
    // read the state of the pushbutton value:buttonState = digitalRead(buttonPin);// check if the pushbutton is pressed. If it is, the buttonState is HIGH:if (buttonState == LOW) {
    // turn LED on:
// digitalWrite(ledPin, HIGH);//延迟除抖delay(500);if(keyB_status == false){
    led_on();keyB_status = true;}else{
    led_off();keyB_status = false;}}
}
  相关解决方案