// Intended for an ATTiny 1614 // Fab Academy 2022 // Jonathan Blok // Pin configuration const int bd_speaker = PIN_PA5; const int sd_speaker = PIN_PA6; const int hh_speaker = PIN_PA7; const int bd_st0_conf_pin = PIN_PA3; const int bd_st1_conf_pin = PIN_PB2; const int sd_st0_conf_pin = PIN_PA2; const int sd_st1_conf_pin = PIN_PB1; const int hh_st0_conf_pin = PIN_PA1; const int hh_st1_conf_pin = PIN_PA4; // Instrument configuration const int bd_hz = 200; const int bd_length = 10; const int sd_hz = 300; const int sd_length = 50; const int hh_hz = 420; const int hh_length = 25; // Configuration matrix int seq_conf[3][2] = {{0,0},{0,0},{0,0}}; const int bpm = 150; bool step; int bpm_in_ms; void setup() { Serial.begin(9600); step = false; // start at step 0 pinMode(bd_st0_conf_pin, INPUT_PULLUP); pinMode(bd_st1_conf_pin, INPUT_PULLUP); pinMode(sd_st0_conf_pin, INPUT_PULLUP); pinMode(sd_st1_conf_pin, INPUT_PULLUP); pinMode(hh_st0_conf_pin, INPUT_PULLUP); pinMode(hh_st1_conf_pin, INPUT_PULLUP); pinMode(bd_speaker, OUTPUT); pinMode(sd_speaker, OUTPUT); pinMode(hh_speaker, OUTPUT); bpm_in_ms = (int) (60.0 / (double) bpm * 1000.0); } void loop() { doStep(step); step = !step; // switch the step delay(bpm_in_ms); // bpm to ms checkSequencerConfiguration(); } void doStep(int step){ int sum_hz = 0; int sum_length = 0; int inst_counter = 0; if(seq_conf[0][step] == 1) { sum_hz += bd_hz; sum_length += bd_length; inst_counter++; } if(seq_conf[1][step] == 1) { sum_hz += sd_hz; sum_length += sd_length; inst_counter++; } if(seq_conf[2][step] == 1) { sum_hz += hh_hz; sum_length += hh_length; inst_counter++; } if(inst_counter != 0 ){ play_instrument(sum_hz/inst_counter, sum_length/inst_counter, hh_speaker); } } void checkSequencerConfiguration() { // Update sequencer configuration from buttons // Inverted due to pullup/pulldown seq_conf[0][0] = 1 - digitalRead(bd_st0_conf_pin); seq_conf[0][1] = 1 - digitalRead(bd_st1_conf_pin); seq_conf[1][0] = 1 - digitalRead(sd_st0_conf_pin); seq_conf[1][1] = 1 - digitalRead(sd_st1_conf_pin); seq_conf[2][0] = 1 - digitalRead(hh_st0_conf_pin); seq_conf[2][1] = 1 - digitalRead(hh_st1_conf_pin); } void play_instrument(int in_hz, int in_length, int speaker_pin) { tone(speaker_pin, in_hz, in_length); // minimally 31hz }