본문 바로가기

Arduino

LED control with SerialRead

superloop.ino

int blink_en =1;
int fading_en =1; 
int blinkRGB_en =1;
int fadingRGB_en = 0;

void setup() {
  blink_setup();
  fading_setup();
  blinkRGB_setup();
  fadingRGB_setup();
  serial_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();
  serial_loop();
}

blink.ino

int blink_state=LOW;
int blink_pin=13;
long blink_prevTime=0;
long blink_interval=1000;

void blink_setup(){
  pinMode(blink_pin,OUTPUT);  
}

void blink_loop(){
  unsigned long blink_currTime=millis();
  if(blink_currTime-blink_prevTime>=blink_interval){
    blink_prevTime=blink_currTime;
    if(blink_state==LOW){
      digitalWrite(blink_pin,HIGH);
      blink_state=HIGH;
    }else{
      digitalWrite(blink_pin,LOW);
      blink_state=LOW;
    }
  } 
}

fading.ino

int fading_pin=11;
int fading_state=0;
unsigned long fading_prevTime=0;
unsigned long fading_interval=30;
int fadingVal=0;

void fading_setup(){
}

void fading_loop(){
  
  unsigned long currTime=millis();
  if(currTime-fading_prevTime>=fading_interval){
    fading_prevTime=currTime;
  if(fading_state==0){
    fadingVal+=5;
    if(fadingVal>=255){
     fadingVal=255;
     fading_state=1;
    }
  }
  else{
    fadingVal-=5;
    if(fadingVal<0){
     fadingVal=0;
     fading_state=0;
    }
  }
  analogWrite(fading_pin,fadingVal);
  }
}

blinkRGB.ino

unsigned long rgb_prevTime[3]={0};
unsigned long rgb_interval[3]={1000,2000,3000};
int rgb_pin[3]={12,7,8};
int rgb_state[3]={LOW,LOW,LOW};

void blinkRGB_setup(){
    for(int i=0;i<3;i++){
      pinMode(rgb_pin[i],OUTPUT);
    }
    Serial.begin(115200);
}

void blinkRGB_loop(){
  unsigned long currTime=millis();
  for(int i=0;i<3;i++){
  if(currTime-rgb_prevTime[i]>=rgb_interval[i]){
    rgb_prevTime[i]=currTime;
    if(rgb_state[i]==LOW){
    rgb_state[i]=HIGH;  
    }else{
      rgb_state[i]=LOW;
    }
  }
   digitalWrite(rgb_pin[i],rgb_state[i]);
  }
}

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(){
  Serial.begin(115200);
}

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]);
    }    
}

serial.ino

void showMenu(){ 
Serial.println( " 1.Led Blink Enable/Disable " ); 
Serial.println( " 2.Led Fading Enable/Disable " ); 
Serial.println( " 3.RGB Led Blink/Fading Toggle " ); 
} 

void serial_setup(){
Serial.begin(115200); 
showMenu();
} 
int iUserCmd=0;

void serial_loop(){
if (Serial.available()>0){
  iUserCmd=Serial.read();
  switch(iUserCmd){
    case'1':blink_en=!blink_en;
    Serial.print("blink_en : ");
    Serial.println(blink_en);
    break;
    case'2':fading_en=!fading_en;
    Serial.print("fading_en : ");
    Serial.println(fading_en);
    break;
    case'3':blinkRGB_en=!blinkRGB_en;
    fadingRGB_en = !blinkRGB_en;
    Serial.print(" blinkRGB_en : ");
    Serial.print(blinkRGB_en);
    Serial.print(" // fadingRGB_en : ");
    Serial.println(fadingRGB_en);
    break;
    default : break;
  }
  }
}

'Arduino' 카테고리의 다른 글

Interrupt를 이용한 btn_toggle  (0) 2022.03.17
BT input  (0) 2022.03.15
RGB_fading  (0) 2022.03.11
RGB_Blink  (0) 2022.03.11
LED_blink_fading_delay  (0) 2022.03.10