0% found this document useful (0 votes)
8 views7 pages

Message

The document defines a program for controlling a buzzer and LEDs using an Arduino, featuring various musical notes and modes of operation. It includes functions for manual and automatic LED control, as well as playing a melody through the buzzer. The program utilizes interrupts for button presses to switch between different modes of operation.

Uploaded by

tiasandro05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Message

The document defines a program for controlling a buzzer and LEDs using an Arduino, featuring various musical notes and modes of operation. It includes functions for manual and automatic LED control, as well as playing a melody through the buzzer. The program utilizes interrupts for button presses to switch between different modes of operation.

Uploaded by

tiasandro05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

#define NOTE_B0 31

#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
#define REST 0

const int boutonPin = 2;


const int buzzerPin = A1;
const int ledMode1 = 11;
const int ledMode2 = 12;
const int ledMode3 = 13;
const int leds[] = {3, 4, 5, 6, 7, 8, 9, 10};
const unsigned long clignoteDuration = 3000; // Durée du clignotement (3 sec)
const unsigned long eteintDuration = 2000; // Durée des LEDs éteintes (2 sec)

enum Mode { MANUEL, AUTOMATIQUE_NORMAL, AUTOMATIQUE_RAPIDE, AUTOMATIQUE_MOITIER,


AUTOMATIQUE_CLINIOTE, AUTOMATIQUE_PANTHERE };
Mode modeActuel = MANUEL;
unsigned long lastPressTime = 0;
unsigned long debounceDelay = 50;
unsigned long startTime = millis();
unsigned long noteStartTime = 0;
bool playing = false;
bool boutonEtatPrecedent = HIGH;
int currentNote = 0;
int ledIndex = 0;
bool boutonAppuye = false;
unsigned long boutonPressDuration = 0;
const unsigned long longPressThreshold = 500;
void jouerBuzzer(int frequence, int duree) {
tone(buzzerPin, frequence, duree);
}
int melody[] = {
REST, REST, REST, NOTE_DS4,
NOTE_E4, REST, NOTE_FS4, NOTE_G4, REST, NOTE_DS4,
NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_C5, NOTE_B4, NOTE_E4, NOTE_G4, NOTE_B4,
NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_E4, NOTE_D4,
NOTE_E4, REST, REST, NOTE_DS4,

NOTE_E4, REST, NOTE_FS4, NOTE_G4, REST, NOTE_DS4,


NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_C5, NOTE_B4, NOTE_G4, NOTE_B4, NOTE_E5,
NOTE_DS5,
NOTE_D5, REST, REST, NOTE_DS4,
NOTE_E4, REST, NOTE_FS4, NOTE_G4, REST, NOTE_DS4,
NOTE_E4, NOTE_FS4, NOTE_G4, NOTE_C5, NOTE_B4, NOTE_E4, NOTE_G4, NOTE_B4,

NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_E4, NOTE_D4,


NOTE_E4, REST,
REST, NOTE_E5, NOTE_D5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E4,
NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_A4,
NOTE_G4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4
};

int durations[] = {
2, 4, 8, 8,
4, 8, 8, 4, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
2, 16, 16, 16, 16,
2, 4, 8, 4,

4, 8, 8, 4, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
1,
2, 4, 8, 8,
4, 8, 8, 4, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,

2, 16, 16, 16, 16,


4, 4,
4, 8, 8, 8, 8, 8, 8,
16, 8, 16, 8, 16, 8, 16, 8,
16, 16, 16, 16, 16, 2
};

void setup() {
pinMode(ledMode1, OUTPUT);
pinMode(ledMode2, OUTPUT);
pinMode(ledMode3, OUTPUT);
pinMode(buzzerPin, OUTPUT);
for (int i = 0; i < 8; i++) {
pinMode(leds[i], OUTPUT);
}
pinMode(boutonPin, INPUT_PULLUP);
modeActuel = MANUEL;
afficherModeLEDs();
attachInterrupt(digitalPinToInterrupt(boutonPin), gestionModeAutomatiqueNormal,
FALLING);
attachInterrupt(digitalPinToInterrupt(boutonPin), gestionModeAutomatiqueRapide,
FALLING);
attachInterrupt(digitalPinToInterrupt(boutonPin), gestionModeAutomatiqueMoitier,
FALLING);
attachInterrupt(digitalPinToInterrupt(boutonPin), gestionModeAutomatiqueCliniote,
FALLING);
attachInterrupt(digitalPinToInterrupt(boutonPin), gestionModeAutomatiquePanthere,
FALLING);

void loop() {
bool boutonEtatActuel = digitalRead(boutonPin);

if (boutonEtatActuel != boutonEtatPrecedent) {
lastPressTime = millis();
}

if (boutonEtatActuel == LOW) {
if (boutonPressDuration == 0) {
boutonPressDuration = millis();
}
} else {
if (boutonPressDuration > 0) {
unsigned long pressTime = millis() - boutonPressDuration;
if (pressTime >= longPressThreshold) {
changerMode();
} else {
if (modeActuel == MANUEL) {
boutonAppuye = true;
}
}
boutonPressDuration = 0;
}
}

boutonEtatPrecedent = boutonEtatActuel;

switch (modeActuel) {
case MANUEL:
gestionModeManuel();
break;
case AUTOMATIQUE_NORMAL:
gestionModeAutomatiqueNormal();
break;
case AUTOMATIQUE_RAPIDE:
gestionModeAutomatiqueRapide();
break;
case AUTOMATIQUE_MOITIER:
gestionModeAutomatiqueMoitier();
break;
case AUTOMATIQUE_CLINIOTE:
gestionModeAutomatiqueCliniote();
break;
case AUTOMATIQUE_PANTHERE:
gestionModeAutomatiquePanthere();
break;
}
}

void changerMode() {
if (modeActuel == MANUEL) {
modeActuel = AUTOMATIQUE_NORMAL;
} else if (modeActuel == AUTOMATIQUE_NORMAL) {
modeActuel = AUTOMATIQUE_RAPIDE;
}else if (modeActuel == AUTOMATIQUE_RAPIDE) {
modeActuel = AUTOMATIQUE_MOITIER;
}else if (modeActuel == AUTOMATIQUE_MOITIER) {
modeActuel = AUTOMATIQUE_CLINIOTE;
}else if (modeActuel == AUTOMATIQUE_CLINIOTE) {
modeActuel = AUTOMATIQUE_PANTHERE;
} else {
modeActuel = MANUEL;
}
afficherModeLEDs();
jouerBuzzer(1000, 1000);
}

void afficherModeLEDs() {
digitalWrite(ledMode1, LOW);
digitalWrite(ledMode2, LOW);
digitalWrite(ledMode3, LOW);

if (modeActuel == MANUEL) {
digitalWrite(ledMode1, LOW);
digitalWrite(ledMode2, HIGH);
digitalWrite(ledMode3, HIGH);
} else if (modeActuel == AUTOMATIQUE_NORMAL) {
digitalWrite(ledMode1, HIGH);
digitalWrite(ledMode2, LOW);
digitalWrite(ledMode3, HIGH);
} else if (modeActuel == AUTOMATIQUE_RAPIDE) {
digitalWrite(ledMode1, HIGH);
digitalWrite(ledMode2, HIGH);
digitalWrite(ledMode3, LOW);
} else if (modeActuel == AUTOMATIQUE_MOITIER) {
digitalWrite(ledMode1, LOW);
digitalWrite(ledMode2, LOW);
digitalWrite(ledMode3, HIGH);
} else if (modeActuel == AUTOMATIQUE_CLINIOTE) {
digitalWrite(ledMode1, LOW);
digitalWrite(ledMode2, HIGH);
digitalWrite(ledMode3, LOW);
}else if (modeActuel == AUTOMATIQUE_PANTHERE) {
digitalWrite(ledMode1, HIGH);
digitalWrite(ledMode2, LOW);
digitalWrite(ledMode3, LOW);
}
}

void gestionModeManuel() {
if (boutonAppuye) {
for (int i = 0; i < 8; i++) {
digitalWrite(leds[i], LOW);
}
digitalWrite(leds[ledIndex], HIGH);
ledIndex = (ledIndex + 1) % 8;
boutonAppuye = false;
}
}
void gestionModeAutomatiqueNormal() {
for (int i = 0; i < 8; i++) {
digitalWrite(leds[i], HIGH);
delay(250);
digitalWrite(leds[i], LOW);
}
}

void gestionModeAutomatiqueRapide() {
for (int i = 0; i < 8; i++) {
digitalWrite(leds[i], HIGH);
}
delay(100);
for (int i = 0; i < 8; i++) {
digitalWrite(leds[i], LOW);
}
}
void gestionModeAutomatiqueMoitier() {
for (int i = 0; i < 8; i++) {
if (i % 2 == 0) {
digitalWrite(leds[i], HIGH);
} else {
digitalWrite(leds[i], LOW);
}
}
delay(2000);
for (int i = 0; i < 8; i++) {
if (i % 2 == 0) {
digitalWrite(leds[i], LOW);
} else {
digitalWrite(leds[i], HIGH);
}
}
delay(2000);
}

void gestionModeAutomatiqueCliniote(){
for (int i = 0; i < 8; i++) {
digitalWrite(leds[i], HIGH);
}
delay(1000);

for (int i = 0; i < 8; i++) {


digitalWrite(leds[i], LOW);
}
delay(1000);
}

void gestionModeAutomatiquePanthere() {
if (!playing && currentNote < sizeof(durations) / sizeof(durations[0])) {
int duration = 1000 / durations[currentNote];
tone(buzzerPin, melody[currentNote], duration);

noteStartTime = millis();
playing = true;
}

if (playing && (millis() - noteStartTime > (1000 / durations[currentNote] *


1.30))) {
noTone(buzzerPin);
currentNote++;
playing = false;

if (currentNote >= sizeof(durations) / sizeof(durations[0])) {


currentNote = 0;
}
}
}

You might also like