前言
尝试按键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 pin
bool keyB_status = false;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void 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;
}
}
}