본문 바로가기

Embedded/Arduino

Arduino v220712

아기상어 + LED 깜빡이기

#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_G4  392
#define NOTE_A4  440
 
#define PIEZO_PIN 8
 
int ledArr[] = {9,6,5,3};
int ledSize = sizeof(ledArr)/sizeof(int);
 
//notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
  NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
  NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,
  0,
  NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
  NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
  NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,
  0,
  NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
  NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
  NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,  
};
 
// note durations: 4 = quarter note, 8 = eighth note, etc
int noteDurations[] = {
  2, 2, 4, 4, 4, 8, 4, 8, 4,
  4, 4, 4, 4, 4, 8, 4, 8, 4,
  4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2,
  2, 
  2, 2, 4, 4, 4, 8, 4, 8, 4,
  4, 4, 4, 4, 4, 8, 4, 8, 4,
  4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2, 
  2, 
  2, 2, 4, 4, 4, 8, 4, 8, 4,
  4, 4, 4, 4, 4, 8, 4, 8, 4,
  4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2, 
};
 
void setup(){
  for(int i=0; i<ledSize; i++)
    pinMode(ledArr[i], OUTPUT);
  
  for (int thisNote = 0; thisNote < sizeof(melody) / sizeof(melody[0]); thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(PIEZO_PIN, melody[thisNote],noteDuration);
    ledON(melody[thisNote]);
 
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    
    noTone(PIEZO_PIN); 
    ledOFF();
  }
}
 
void loop(){}
  
void ledON(int piezo_melody)
{ 
  if((piezo_melody == NOTE_E4) || (piezo_melody == NOTE_G4))
   digitalWrite(ledArr[0],HIGH);
  if(piezo_melody == NOTE_C4)
   digitalWrite(ledArr[1],HIGH);
  if((piezo_melody == NOTE_A4) || piezo_melody == NOTE_D4)
   digitalWrite(ledArr[2],HIGH);
  if(piezo_melody == NOTE_F4)
   digitalWrite(ledArr[3],HIGH);      
}
 
void ledOFF()
{
  for(int i=0; i<ledSize; i++){
    digitalWrite(ledArr[i], LOW);
  }
}

학교종이 땡땡땡 + LED 깜빡이기

#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330
#define NOTE_G4  392
#define NOTE_A4  440
 
#define PIEZO_PIN 8
 
int ledArr[] = {9,6,5,3};
int ledSize = sizeof(ledArr)/sizeof(int);
  
int melody[] = {
NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, 
NOTE_G4, NOTE_G4, NOTE_E4,   
NOTE_G4, NOTE_G4, NOTE_E4, NOTE_E4,
NOTE_D4, 0,
NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4,
NOTE_G4, NOTE_G4, NOTE_E4,
NOTE_G4, NOTE_E4, NOTE_D4, NOTE_E4,
NOTE_C4,0};
 
int noteDurations[] = {
1,1,1,1,
1,1,2,
1,1,1,1,
3,1,
1,1,1,1,
1,1,2,
1,1,1,1,
3,1};
 
void setup() {
  for(int i=0; i<ledSize; i++)
    pinMode(ledArr[i], OUTPUT);
  
  for (int thisNote = 0; thisNote < 26; thisNote++) {
    int noteDuration = 250 * noteDurations[thisNote];
    tone(PIEZO_PIN, melody[thisNote],noteDuration);
    ledON(melody[thisNote]);
      
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(PIEZO_PIN);
    ledOFF();
  }
}
void loop() {}
  
void ledON(int piezo_melody)
{ 
  if((piezo_melody == NOTE_C4) || (piezo_melody == NOTE_D4))
   digitalWrite(ledArr[0],HIGH);
  if(piezo_melody == NOTE_E4)
   digitalWrite(ledArr[1],HIGH);
  if(piezo_melody == NOTE_G4)
   digitalWrite(ledArr[2],HIGH);
  if(piezo_melody == NOTE_A4)
   digitalWrite(ledArr[3],HIGH);      
}
 
void ledOFF()
{
  for(int i=0; i<ledSize; i++){
    digitalWrite(ledArr[i], LOW);
  }
}

 

 

피에조 스피커 테스트

#define NOTE_C4  262
#define NOTE_G3  196
#define NOTE_A3  220
#define NOTE_B3  247
#define NOTE_C4  262


// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}

#LED FADE(3번핀)

#define UP 0
#define DOWN 1
 
const int minPWM = 0;
const int maxPWM = 255;
int pwmLED[] = {3};
int ledSize = sizeof(pwmLED)/sizeof(pwmLED[0]);
 
byte fadeDirection = UP;
int fadeValue = 0;
byte fadeIncrement = 5;
unsigned long previousFadeMillis;
int fadeInterval = 30;
int led_cnt = 0;
 
 
void setup() {
  for(int i=0; i<ledSize; i++){
    pinMode(pwmLED[i], OUTPUT);
   	analogWrite(pwmLED[i], fadeValue);
  }
  Serial.begin(9600);
  Serial.println(led_cnt);
}
 
 
 
void doTheFade(unsigned long thisMillis) {
   if (thisMillis - previousFadeMillis >= fadeInterval) {
      if (fadeDirection == UP) {
         fadeValue = fadeValue + fadeIncrement;
         if (fadeValue > maxPWM) {
           fadeValue = maxPWM;
           fadeDirection = DOWN;
         }
      } else {
         fadeValue = fadeValue - fadeIncrement;
         if (fadeValue < minPWM) {
           fadeValue = minPWM;
           fadeDirection = UP;
           led_cnt = (led_cnt+1 == ledSize) ? 0 : led_cnt+1;
           Serial.println(led_cnt);
         }
      }
      analogWrite(pwmLED[led_cnt], fadeValue);
      previousFadeMillis = thisMillis;
   }
}
 
 
void loop() {
   unsigned long currentMillis = millis();
   doTheFade(currentMillis);
}

 

LED 누적해서 깜빡이기

const long interval = 200;  
int ledArr[] = {3,5,6,9,10,11};
int ledSize = sizeof(ledArr)/sizeof(int);
 
unsigned long previousMillis = 0;
 
int sys_cnt = 0;
void setup()
{
  for(int i=0; i<ledSize; i++){
    pinMode(ledArr[i], OUTPUT);
  }
}
 
void loop() {
  unsigned long currentMillis = millis();
 
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    led_blink_weighted_LR();
  }
}

void led_off()
{
  for(int i=0; i<ledSize; i++)
    digitalWrite(ledArr[i], LOW);
}

void led_blink_each_LR()
{
  sys_cnt = (sys_cnt%ledSize == 0) ? 0 : sys_cnt;
  for(int i=0; i<ledSize; i++){
    if(i == sys_cnt){
      digitalWrite(ledArr[i], HIGH);
    }
    else{
      digitalWrite(ledArr[i], LOW);
    }
  }
  sys_cnt++;  
}

void led_blink_weighted_RL()
{
  int n = sys_cnt % (ledSize+1);
  if (n == ledSize)
    led_off();
  else
  	digitalWrite(ledArr[n], HIGH);
  sys_cnt++;  
}

void led_blink_weighted_LR()
{
  int n = sys_cnt % (ledSize+1);
  if (n == ledSize)
    led_off();
  else
  	digitalWrite(ledArr[ledSize-1-n], HIGH);
  sys_cnt++; 
}

void led_blink_each_RL()
{
  sys_cnt = (sys_cnt%ledSize < 0) ? ledSize-1 : sys_cnt;
  for(int i=0; i<ledSize; i++){
    if(i == sys_cnt){
      digitalWrite(ledArr[i], HIGH);
    }
    else{
      digitalWrite(ledArr[i], LOW);
    }
  }
  sys_cnt--;  
}


void led_blink_all()
{
  sys_cnt = (sys_cnt%2 == 0) ? 0 : sys_cnt;
  if(sys_cnt==0){
    for(int i=0; i<ledSize; i++){
      digitalWrite(ledArr[i], HIGH);
    }
  }
  else{
    for(int i=0; i<ledSize; i++){
      digitalWrite(ledArr[i], LOW);
    }
  }
  sys_cnt++;
}

 

LED동시에 번갈아 깜빡이기(오른쪽에서 왼쪽으로)

const long interval = 200;  
int ledArr[] = {3,5,6,9,10,11};
int ledSize = sizeof(ledArr)/sizeof(int);
 
unsigned long previousMillis = 0;
 
int sys_cnt = 0;
void setup()
{
  for(int i=0; i<ledSize; i++){
    pinMode(ledArr[i], OUTPUT);
  }
}
 
void loop() {
  unsigned long currentMillis = millis();
 
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    led_blink_each_RL();
  }
}

void led_blink_each_LR()
{
  sys_cnt = (sys_cnt%ledSize == 0) ? 0 : sys_cnt;
  for(int i=0; i<ledSize; i++){
    if(i == sys_cnt){
      digitalWrite(ledArr[i], HIGH);
    }
    else{
      digitalWrite(ledArr[i], LOW);
    }
  }
  sys_cnt++;  
}

void led_blink_each_RL()
{
  sys_cnt = (sys_cnt%ledSize < 0) ? ledSize-1 : sys_cnt;
  for(int i=0; i<ledSize; i++){
    if(i == sys_cnt){
      digitalWrite(ledArr[i], HIGH);
    }
    else{
      digitalWrite(ledArr[i], LOW);
    }
  }
  sys_cnt--;  
}

LED동시에 깜빡이기

const long interval = 200;  
int ledArr[] = {3,5,6,9,10,11};
int ledSize = sizeof(ledArr)/sizeof(int);
 
unsigned long previousMillis = 0;
 
int sys_cnt = 0;
void setup()
{
  for(int i=0; i<ledSize; i++){
    pinMode(ledArr[i], OUTPUT);
  }
}
 
void loop() {
  unsigned long currentMillis = millis();
 
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    sys_cnt = (sys_cnt%2 == 0) ? 0 : sys_cnt;
    
    if(sys_cnt==0){
      for(int i=0; i<ledSize; i++){
      	digitalWrite(ledArr[i], HIGH);
      }
    }
    else{
      for(int i=0; i<ledSize; i++){
      	digitalWrite(ledArr[i], LOW);
      }
    }
	sys_cnt++;
  }
}

'Embedded > Arduino' 카테고리의 다른 글

arduino v220714  (0) 2022.07.14
lilypad_v220713  (0) 2022.07.13
lilyPad v220708 -2  (0) 2022.07.08
lilyPad v220708  (0) 2022.07.08
lilyPad v220707  (0) 2022.07.07