|
-
- const int Output = 1; // Can be 1 or 4
- // Cater for 16MHz, 8MHz, or 1MHz clock:
- const int Clock = ((F_CPU/1000000UL) == 16) ? 4 : ((F_CPU/1000000UL) == 8) ? 3 : 0;
- const uint8_t scale[] PROGMEM = {239,226,213,201,190,179,169,160,151,142,134,127};
- void note (int n, int octave) {
- int prescaler = 8 + Clock - (octave + n/12);
- if (prescaler<1 || prescaler>15 || octave==0) prescaler = 0;
- DDRB = (DDRB & ~(1<<Output)) | (prescaler != 0)<<Output;
- OCR1C = pgm_read_byte(&scale[n % 12]) - 1;
- GTCCR = (Output == 4)<<COM1B0;
- TCCR1 = 1<<CTC1 | (Output == 1)<<COM1A0 | prescaler<<CS10;
- }
-
-
- int cm[] = {0,3,7};
- int fm[] = {0,5,8};
- int g[] = {11,7,2,5,8};
- int member = 3;
- int note_length = 50;
- int n = 0;
- int octave = 1;
- int akkord = 0;
- unsigned long seconds = 0;
- unsigned long prev = 0;
-
- void setup() {
- // put your setup code here, to run once:
- //pinMode(1, OUTPUT);
- }
-
- void loop() {
- // put your main code here, to run repeatedly:
-
- note_length = random(30,70);
- octave = random(3,7);
-
- seconds = millis();
- seconds = seconds/1000;
- if (prev != seconds)
- akkord = random(0,3);
- prev = seconds;
-
- switch (akkord) {
- case 0: // cm
- member = random(3);
- n = cm[member];
- break;
- case 1: // fm
- member = random(3);
- n = fm[member];
- break;
- case 2: // g
- member = random(5);
- n = g[member];
- break;
- }
-
- note(n,octave);
-
- delay(note_length);
- }
|