티스토리 뷰

// 스위치로 LED ON OFF
// 스위치 오동작은 캐패시터로 해결
int ledPin = 12;
int switchPin = 7;

int swStatus = LOW;
int ledStatus = LOW;

void setup()  {
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP);
  digitalWrite(ledPin, LOW);
}

void loop()  {
  if(digitalRead(switchPin) == LOW && swStatus == LOW)  {
    swStatus = HIGH;
  }else if(digitalRead(switchPin) == HIGH && swStatus == HIGH)  {
    swStatus = LOW;
    ledStatus = !ledStatus;
  }
  
  digitalWrite(ledPin, ledStatus);
}

int ledPin = 12;
int switchPin = 7;

int swStatus = LOW;
int ledStatus = LOW;
int ledFlag = LOW;
unsigned long prevMillis = 0;

// 스위치로 LED ON 2초 점멸, OFF
// delay 사용 시 delay  수행 중 digitalRead 정상적으로 수행하지 않는다. -> millis() 사용
void setup()  {
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP);
  digitalWrite(ledPin, LOW);
}

void loop()  {
  unsigned long currentMillis = millis();
  unsigned long delta = currentMillis - prevMillis;
  
  if(digitalRead(switchPin) == LOW && swStatus == LOW)  {
    swStatus = HIGH;
  }else if(digitalRead(switchPin) == HIGH && swStatus == HIGH)  {
    swStatus = LOW;
    ledStatus = !ledStatus;
  }
  
  if(ledStatus == HIGH)  {
    if(delta > 500)  {
      digitalWrite(ledPin, ledFlag);
      prevMillis = millis();
      ledFlag = !ledFlag;
    }
  }else  {
    digitalWrite(ledPin, LOW);
  }  
}

// msTimer2 인터럽트 사용
#include 

int ledPin = 12;
int switchPin = 7;

boolean swStatus = LOW;
boolean ledStatus = LOW;
boolean ledFlag = HIGH;

int delayTime = 0;

void setup()  {
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP);
  digitalWrite(ledPin, LOW);
  
  MsTimer2::set(1, flash);
  MsTimer2::start();
}

void loop()  {
  if(digitalRead(switchPin) == LOW && swStatus == LOW)  {
    swStatus = HIGH;
  }else if(digitalRead(switchPin) == HIGH && swStatus == HIGH)  {
    swStatus = LOW;
    ledStatus = !ledStatus;
  }   
  if(ledStatus == HIGH)  {
    if(delayTime > 500)  {
      delayTime = 0;
      digitalWrite(ledPin, ledFlag);
      ledFlag = !ledFlag;
    }
  }else  {
    digitalWrite(ledPin, LOW);
  }
}

void flash() {
  delayTime++;
}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
TAG
more
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함