본문 바로가기
C++/c++수업

ㅌㅔ스트

by sj0020 2020. 10. 28.
#include <MyButton.h>
#include <MyLed.h>
MyButton myBtn(2);
MyLed myLed1(8);
MyLed myLed2(9);

void setup() {
Serial.begin(9600); 
}

void loop() {
myLed2.ledblink();  
myBtn.isPressed();

if (myBtn.isPressed() == true){
  Serial.println("btn not pressed");
  myLed1.ledoff();
}
else{
  Serial.println("btn pressed");
  myLed1.ledon();   
}

}

 

#include <MyButton.h>
#include <MyLed.h>
MyButton myBtn(2);
MyLed myLed1(8);
MyLed myLed2(9);

const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 8;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // 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 setup() {
  Serial.begin(9600); 
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  myLed1.ledoff();
}

void loop() {
  int reading = digitalRead(2);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
        ledState = !ledState;
        Serial.println("button pressed. LED ON");
        myLed1.ledon(); 
      }
      else{
        myLed1.ledoff();
        }
    }
  }
  lastButtonState = reading;

  myBtn.isPressed();
  

}