Git Product home page Git Product logo

aps1_embarcados's Introduction

APS1_embarcados

aps1_embarcados's People

Contributors

elijose55 avatar

Watchers

James Cloos avatar

aps1_embarcados's Issues

Sugestão: Poderia estar em um .h separado

Para ficar mais limpo e com menos defines no main.c. Como são muitas notas, você poderia ter criado um arquivo notes.h e ter colocados as notas lá. No arquivo principal "main.c" bastaria importar utilizando #include.

#define NOTE_B0 31 //Defining note frequency
#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

Poderia utilizar um switch

if(music == 1){
int size = sizeof(music1_melody) / sizeof(int);
play_music(music1_melody, music1_tempo, size);
}
else if(music == 2){
int size = sizeof(music2_melody) / sizeof(int);
play_music(music2_melody, music2_tempo, size);
}
else if(music == 3){
int size = sizeof(music3_melody) / sizeof(int);
play_music(music3_melody, music3_tempo, size);
}

Um switch funcionaria e deixaria o código mais limpo.

Funções dentro de funções, fora do padrão C

void buzz(long frequency, long length) {
pio_clear(PIOC, LED_PIO_IDX_MASK); //liga o led de acordo com os toques
long delayValue = 1000000 / frequency / 2;
long numCycles = frequency * length / 1000;
for (long i = 0; i < numCycles; i++) {
pio_set(BUZZER_PIO, BUZZER_PIO_IDX_MASK); // Coloca 1 no pino BUZZER
delay_us(delayValue); //delay de acordo com a frequencia e duracao do toque
pio_clear(BUZZER_PIO, BUZZER_PIO_IDX_MASK); // Coloca 0 no pino do BUZZER
delay_us(delayValue);
}
pio_set(PIOC, LED_PIO_IDX_MASK); //liga o led de acordo com os toques
}
void play_music(int melody[], int tempo[], int size){
for (int thisNote = 0; thisNote < size; thisNote++) {
int noteDuration = 1000 / tempo[thisNote];
buzz(melody[thisNote], noteDuration);
if(!(pio_get(BUT3_PIO, PIO_INPUT, BUT3_PIO_IDX_MASK))){ //se o botao de play for pressionado para a musica
buzz(0, noteDuration);
delay_s(2);
return;
}
int pauseBetweenNotes = noteDuration * 1.10;
delay_ms(pauseBetweenNotes); //tempo entre os toques
buzz(0, noteDuration);
}
}

Não sei se vocês repararam mas vocês declararam a função buzz e play_music dentro da função main! Quando temos função dentro de função, isto se trata de "nested functions". No caso, isso deu certo porque o compilador do Atmel Studio é o GCC e ele suporta isto.

No entanto, em outros microcontroladores isso não compilaria provavelmente, não sendo portável, por isto evitem...

Você pode verificar aqui: StackOverflow

Nesse caso, não vejo motivo para usar esse recurso, basta então tirar as funções do main.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.