#include "button.h"
Button btn1;
#define LED_BUILTIN 5
//const int BUTTON_PIN = 12;
byte buttonState;
byte lastButtonState = LOW;
int ledState = LOW;
int ledStatecopy = LOW;
unsigned long previousMillis1 = 0;
const long interval1 = 500;
unsigned long previousMillis2 = 0;
const long interval2 = 100;
int i = 0;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(4, OUTPUT);
pinMode(15, OUTPUT);
btn1.begin(13);
}
void loop() {
unsigned long currentMillis1 = millis();
unsigned long currentMillis2 = millis();
// 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 (btn1.debounce())
{
i++;
}
if (currentMillis1 - previousMillis1 >= interval1) {
previousMillis1 = currentMillis1;
if (ledState == LOW) {
ledState = HIGH; // Note that this switches the LED *off*
} else {
ledState = LOW; // Note that this switches the LED *on*
}
if(ledState == HIGH)
{
analogWrite(LED_BUILTIN, 45);
}
else
{
analogWrite(LED_BUILTIN, 0);
}
}
if (i >= 4)
{
i = 0;
}
if (currentMillis2 - previousMillis2 >= interval2) {
previousMillis2 = currentMillis2;
switch(i)
{
case 0:
analogWrite(4, 0); // analogRead 값은 0부터 1023까지, analogWrite 값은 0 부터 255까지
analogWrite(15, 0); // analogRead 값은 0부터 1023까지, analogWrite 값은 0 부터 255까지
break;
case 1:
analogWrite(4, 15); // analogRead 값은 0부터 1023까지, analogWrite 값은 0 부터 255까지 // 50
analogWrite(15, 50); // analogRead 값은 0부터 1023까지, analogWrite 값은 0 부터 255까지
break;
case 2:
analogWrite(4, 35); // analogRead 값은 0부터 1023까지, analogWrite 값은 0 부터 255까지 // 150
analogWrite(15, 150); // analogRead 값은 0부터 1023까지, analogWrite 값은 0 부터 255까지
break;
case 3:
analogWrite(4, 120); // analogRead 값은 0부터 1023까지, analogWrite 값은 0 부터 255까지 // 245
analogWrite(15, 245); // analogRead 값은 0부터 1023까지, analogWrite 값은 0 부터 255까지
break;
default:
break;
}
}
}
Button.h ===================================================
#ifndef button_h
#define button_h
#include "Arduino.h"
class Button
{
private:
uint8_t btn;
uint16_t state;
public:
void begin(uint8_t button) {
btn = button;
state = 0;
pinMode(btn, INPUT_PULLUP);
}
bool debounce() {
state = (state<<1) | digitalRead(btn) | 0xfe00;
return (state == 0xff00);
}
};
#endif
댓글 없음:
댓글 쓰기