|
发表于 2022-4-29 15:32:22
|
显示全部楼层
10%输出改为PD3,20%输出改为PD4
https://pan.baidu.com/s/1FQbuxb4SHPraRqo0IGUWHA?pwd=9d5b 提取码: 9d5b
arduino代码,仅供参考
- #include <Arduino.h>
- #include <Serial.h>
- unsigned long timeLastGet = 0;
- unsigned long timeCnt = 0;
- // constants won't change. They're used here to set pin numbers:
- const int pinButton = PC5; // the number of the pushbutton pin
- const int pinLed1 = PC4; // 10% LED pin
- const int pinLed2 = PC3; // 20% LED pin
- const int pinPwm1 = PD3; // 10% output pin
- const int pinPwm2 = PD4; // 20% output pin
- // Variables will change:
- int pwmState = 0; // pwm state, 0:10%, 1:20%
- int buttonState = LOW; // the current reading from the input pin
- int lastButtonState = LOW; // the previous reading from the input pin
- // the following variables are unsigned longs because the time, measured in
- // milliseconds, will quickly become a bigger number than can be stored in an int.
- unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
- unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
- void KeyProcess() {
- // read the state of the switch into a local variable:
- int reading = digitalRead(pinButton);
- //Serial_println_i(reading);
- // check to see if you just pressed the button
- // (i.e. the input went from LOW to HIGH), and you've waited long enough
- // since the last press to ignore any noise:
- // If the switch changed, due to noise or pressing:
- if (reading != lastButtonState) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- // whatever the reading is at, it's been there for longer than the debounce
- // delay, so take it as the actual current state:
- // if the button state has changed:
- if (reading != buttonState) {
- buttonState = reading;
- // only toggle the LED if the new button state is HIGH
- if (buttonState == HIGH) {
- pwmState = 1 - pwmState;
- if (pwmState == 1) { // 10%
- digitalWrite(pinLed1, HIGH);
- digitalWrite(pinLed2, LOW);
- analogWrite(pinPwm1, 25);
- analogWrite(pinPwm2, 0);
- Serial_print_s("10%");
- } else {
- digitalWrite(pinLed1, LOW);
- digitalWrite(pinLed2, HIGH);
- analogWrite(pinPwm1, 0);
- analogWrite(pinPwm2, 50);
- Serial_print_s("20%");
- }
- }
- }
- }
- // save the reading. Next time through the loop, it'll be the lastButtonState:
- lastButtonState = reading;
- }
- void setup()
- {
- Serial_begin(115200);
- pinMode(pinLed1, OUTPUT);
- pinMode(pinLed2, OUTPUT);
- pinMode(pinPwm1, OUTPUT);
- pinMode(pinPwm2, OUTPUT);
- pinMode(pinButton, INPUT_PULLUP);
- Serial_print_s("PWM generate");
- }
- void loop()
- {
- KeyProcess();
- }
复制代码
|
打赏
-
查看全部打赏
|