본문 바로가기

Arduino

RGB_fading

superloop.ino

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

void loop() {
  blink_loop();
  fading_loop();
  //blinkRGB_loop();
  fadingRGB_loop();
}

fadingRGB.ino

int fadingRGB_pin[3]={12,7,8};
unsigned long fadingRGB_prevTime[3]={0};
unsigned long fadingRGB_interval[3]={30,60,120};
int fadingRGB_val[3]={0};
int fadingRGB_dir[3]={1,1,1};

void fadingRGB_setup(){
  
}

void fadingRGB_loop(){
  unsigned long currTime=millis();
  for(int i=0;i<3;i++){
    if(currTime-fadingRGB_prevTime[i]>=fadingRGB_interval[i]){
      fadingRGB_prevTime[i]=currTime;
      if(fadingRGB_dir[i]==1){
        if(fadingRGB_val[i]<=255){
        fadingRGB_val[i]+=5;  
        }
        if(fadingRGB_val[i]>255){
          fadingRGB_val[i]=255;
          fadingRGB_dir[i]=0;
        }
      }
      if(fadingRGB_dir[i]==0){
        if(fadingRGB_val[i]>=0){
        fadingRGB_val[i]-=5;  
        }
        if(fadingRGB_val[i]<0){
          fadingRGB_val[i]=0;
          fadingRGB_dir[i]=1;
        }
        }
    }
    analogWrite(fadingRGB_pin[i],fadingRGB_val[i]);
    }
}

'Arduino' 카테고리의 다른 글

BT input  (0) 2022.03.15
LED control with SerialRead  (0) 2022.03.14
RGB_Blink  (0) 2022.03.11
LED_blink_fading_delay  (0) 2022.03.10
LED_Blink_Fading_millis  (0) 2022.03.10