Anecha Khumjun

Bookmark this to keep an eye on my project updates!


Project maintained by Koonpoy Hosted on GitHub Pages — Theme by mattgraham

C/C++ Programming Arduino LED

LED changing pattern: A

// LED changing pattern: A


const int LED_PINS[] = {5,18,19,21} ;
const int NUM_LEDS = sizeof(LED_PINS)/sizeof(int) ; 

#define OFF (LOW) 
#define ON (HIGH)

void setup() { 
  for (int i=0 ; i < NUM_LEDS ; i++) {
    pinMode(LED_PINS[i], OUTPUT);
    digitalWrite(LED_PINS[i] , (i==0) ? ON:OFF);
  }
}

void loop() {
  for(int i=0 ; i < NUM_LEDS ; i++) {
    digitalWrite(LED_PINS[i], ON);
    delay(1500);
    digitalWrite(LED_PINS[i],OFF);

  }
}

LED changing pattern: B

// LED changing pattern: B

const int LED_PINS[] = {5,18,19,21} ;
const int NUM_LEDS = sizeof(LED_PINS)/sizeof(int) ; 

#define OFF (LOW) 
#define ON (HIGH)


void setup() {
  // put your setup code here, to run once:
    for (int i=0 ; i < NUM_LEDS ; i++) {
    pinMode(LED_PINS[i], OUTPUT);
    }

}


void loop() {
  // put your main code here, to run repeatedly:
  int i = 0;
  for ( i ; i < NUM_LEDS ; i++){
    digitalWrite(LED_PINS[i],ON);
    delay(1000);

  }
  if (i == NUM_LEDS) {
    for ( i-- ; i >= 0; i--){
    digitalWrite(LED_PINS[i],OFF);
    delay(1000);
  }

  }
}

LED changing pattern: C

// LED changing pattern: C

const int LED_PINS[] = {13,12,14,27} ;
const int NUM_LEDS = sizeof(LED_PINS)/sizeof(int) ; 
const int PWM_RESOLUTION = 8 ;
const int DUTY_MAX = 1 << (PWM_RESOLUTION) ;
const int PWM_FREQ = 5000 ;


#define OFF (LOW) 
#define ON (HIGH)


void setup() {
  // put your setup code here, to run once:
for (int i=0 ; i < NUM_LEDS ; i++){
  pinMode(LED_PINS[i], OUTPUT);
}
}

void loop() {
  // put your main code here, to run repeatedly:  
  int i=0 ;

  for (i ; i < NUM_LEDS ; i++) {
    ledcSetup(i,PWM_FREQ,PWM_RESOLUTION) ;
    ledcAttachPin(LED_PINS[i],i) ;
      for (int x=0 ; x < DUTY_MAX ; x++){
        ledcWrite(i,x);
        delay(10);

  }
  }
  for (i-- ; i >= 0 ; i--){
    ledcSetup(i,PWM_FREQ,PWM_RESOLUTION) ;
    ledcAttachPin(LED_PINS[i],i) ;
      for (int x=DUTY_MAX ; x >= 0 ; x--){
        ledcWrite(i,x);
        delay(10);


  }
  }
  delay(1000);
}

LED changing pattern: D


const int LED_PINS[] = {27,26,25,33} ;
const int NUM_PINS = sizeof(LED_PINS)/sizeof(int) ;
const int PWM_RESOLUTION = 8 ;
const int DUTY_MAX = 1 << (PWM_RESOLUTION) ;
const int PWM_FREQ = 5000 ;
const int DUTY_PERCENT[] = { DUTY_MAX /* 100% */ , DUTY_MAX*0.5 /* 50% */ , DUTY_MAX*0.25 /* 25% */, DUTY_MAX*0.1 /* 10% */ } ;
const int NUM_DUTY = sizeof(DUTY_PERCENT)/sizeof(int) ;

void setup() {
  // put your setup code here, to run once:
  for(int i=0 ; i < NUM_PINS ; i++) {
    pinMode(LED_PINS[i] , OUTPUT);
    ledcSetup(i,PWM_FREQ,PWM_RESOLUTION) ;
    ledcAttachPin(LED_PINS[i],i) ;
  }
  }
  


void loop() {
  for(int i=0 ; i < NUM_PINS ; i++) {
    for (int x=0 ; x < NUM_DUTY ; x++){
      ledcWrite(i,DUTY_PERCENT[x]);
      delay(1000);
    }
}
}