본문 바로가기

Arduino

Interrupt를 이용한 btn_toggle

//arduino_superloop
int blink_en = 1;
int fading_en = 1;
int blinkRGB_en = 1;
int fadingRGB_en = 1;

void setup() {
  blink_setup();
  fading_setup();
  blinkRGB_setup();
  fadingRGB_setup();
}

void loop() {
  if(blink_en == 1)  blink_loop();
  if(fading_en == 1)fading_loop();
  if(blinkRGB_en == 1) blinkRGB_loop();
  if(fadingRGB_en == 1) fadingRGB_loop();
}

//LEDblink
int blink_ledState = LOW;
long blink_previousMillis = 0;
long blink_interval = 1000;

const int blink_buttonPin = 5;
int blink_buttonState = 0;

void blink_setup() {
  pinMode(13, OUTPUT);
  pinMode(blink_buttonPin, INPUT);
}

void blink_loop() {
  blink_buttonState = digitalRead(blink_buttonPin);
  if(blink_buttonState == LOW) {
    digitalWrite(13, LOW);
    return;
  }

  unsigned long currentMillis = millis();
  
  if(currentMillis - blink_previousMillis >= blink_interval) {
    blink_previousMillis = currentMillis;

    if(blink_ledState == LOW) 
      blink_ledState = HIGH;
    else
      blink_ledState = LOW;
      
    digitalWrite(13, blink_ledState);
  }
}


//LEDfading
int fading_ledPin = 11;
long fading_previousMillis = 0;
long fading_interval = 30;
int fadeValue = 0;
int fadeDir = 1;

int buttonToggle = 0;

void fading_setup() {
  attachInterrupt(digitalPinToInterrupt(2), buttonIsr, RISING);  
}

void buttonIsr() {
  buttonToggle = !buttonToggle;
}

void fading_loop() {
  if(buttonToggle==0) {
    analogWrite(fading_ledPin, 0);
    return;
  }
  
  unsigned long currentMillis = millis();
  
  if(currentMillis - fading_previousMillis >= fading_interval) {
    fading_previousMillis = currentMillis; 

    if(fadeDir == 1) { 
      if(fadeValue <= 255) {        
        fadeValue += 5;
        if(fadeValue > 255) {          
          fadeValue = 255;
          fadeDir = 0;
        }  
      }
    } else { 
      if(fadeValue >= 0) {
        fadeValue -= 5;
        if(fadeValue < 0) {
          fadeValue = 0;
          fadeDir = 1;          
        }
      }
    }
    analogWrite(fading_ledPin, fadeValue);        
  }
}


//RGB blink
int blinkRGB_ledPin[3] = {12,7,8};
int blinkRGB_ledState[3] = {HIGH,HIGH,HIGH};
long blinkRGB_previousMillis[3] = {0,};
long blinkRGB_interval[3] = {1000,2000,3000};

const int blinkRGB_buttonPin = 4;
int blinkRGB_buttonState = 0;

void blinkRGB_setup() {
  for(int i=0;i<3;i++)
    pinMode(blinkRGB_ledPin[i], OUTPUT);  
  pinMode(blinkRGB_buttonPin, INPUT);
}

void blinkRGB_loop() {
  blinkRGB_buttonState = digitalRead(blinkRGB_buttonPin);
  if(blinkRGB_buttonState == LOW) {
    for(int i=0;i<3;i++)
      digitalWrite(blinkRGB_ledPin[i], HIGH);
    return;
  }
  
  unsigned long currentMillis = millis();
  
  for(int i=0;i<3;i++) {
    if(currentMillis - blinkRGB_previousMillis[i] >= blinkRGB_interval[i]) {
      blinkRGB_previousMillis[i] = currentMillis;

      if(blinkRGB_ledState[i] == LOW) 
        blinkRGB_ledState[i] = HIGH;
      else
        blinkRGB_ledState[i] = LOW;
        
      digitalWrite(blinkRGB_ledPin[i], blinkRGB_ledState[i]);
    }
  }
}

//RGB fading
int fadingRGB_ledPin[3] = {10,6,9};
long fadingRGB_previousMillis[3] = {0,0,0};
long fadingRGB_interval[3] = {30,60,120};
int fadeRGBValue[3] = {255,255,255};
int fadeRGBDir[3] = {0,0,0};

int fadeRGBbuttonToggle = 0;

void fadingRGB_setup() { 
  attachInterrupt(digitalPinToInterrupt(3), fadeRGBbuttonIsr, RISING);  
}

void fadeRGBbuttonIsr() {
  fadeRGBbuttonToggle = !fadeRGBbuttonToggle;
}

void fadingRGB_loop() {  
  if(fadeRGBbuttonToggle==0) {
    for(int i=0;i<3;i++)
      analogWrite(fadingRGB_ledPin[i], 255);
    return;
  }
  
  unsigned long currentMillis = millis();
  
  for(int i=0;i<3;i++) {
    if(currentMillis - fadingRGB_previousMillis[i] >= fadingRGB_interval[i]) {
      fadingRGB_previousMillis[i] = currentMillis;   

      if(fadeRGBDir[i] == 1) {
        if(fadeRGBValue[i] <= 255) {
          fadeRGBValue[i] += 5;
          if(fadeRGBValue[i] > 255) {
            fadeRGBValue[i] = 255; 
            fadeRGBDir[i] = 0;
          }  
        }
      } else {
        if(fadeRGBValue[i] >= 0) {
          fadeRGBValue[i] -= 5;
          if(fadeRGBValue[i] < 0) {
            fadeRGBValue[i] = 0;
            fadeRGBDir[i] = 1;
          }
        }
      }
      analogWrite(fadingRGB_ledPin[i], fadeRGBValue[i]);        
    }
  }
}

'Arduino' 카테고리의 다른 글

superloop ADC_BT serial  (0) 2022.03.18
superloop ADC  (0) 2022.03.18
BT input  (0) 2022.03.15
LED control with SerialRead  (0) 2022.03.14
RGB_fading  (0) 2022.03.11