Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EEPROM.h>
- #include "classdefs.h"
- bool seq[3][4] {
- {1, 0, 0, 0},
- {0, 1, 0, 0},
- {0, 0, 1, 1}
- };
- int sLedPins[3] = {35, 36, 37};
- int cur_seq = 0;
- int cur_stp = 0;
- unsigned long lastTime = 0;
- // button and step object lists
- Button buttons[6] = {
- Button(14),
- Button(15),
- Button(16),
- Button(17),
- Button(33),
- Button(34)
- };
- Step steps[4] {
- Step(10),
- Step(9),
- Step(8),
- Step(7)
- };
- void setup() {
- for (int i = 0; i < 3; i++)
- pinMode(sLedPins[i], OUTPUT);
- Serial.begin(9600);
- readEEPROM();
- }
- void loop() {
- checkAllButtons();
- if (millis() > lastTime + 250) {
- setLeds();
- nextStep();
- lastTime = millis();
- }
- }
- void checkAllButtons() {
- for (int i = 0; i < 6; i++) {
- // first check button states
- buttons[i].check();
- if (i < 4 && buttons[i].isPressed()) {
- // this detects step buttons. on each press, the value is inverted and written to EEPROM
- seq[cur_seq][i] = !seq[cur_seq][i];
- EEPROM.write(i + (4 * cur_seq), seq[cur_seq][i]);
- setLeds();
- }
- if (i >= 4 && buttons[i].isPressed()) {
- // this detects sequence buttons. on each press it changes the sequence
- nextSeq(i - 4);
- setLeds();
- }
- }
- }
- void nextSeq(int s) {
- // switches the current sequence
- s == 1 ? cur_seq++ : cur_seq--;
- if (cur_seq < 0)
- cur_seq = 2;
- if (cur_seq > 2)
- cur_seq = 0;
- }
- void nextStep() {
- // plays the next step, setting the state from the seq array and playing the midi notes
- cur_stp++;
- if (cur_stp > 3)
- cur_stp = 0;
- for (int i = 0; i < 4; i++) {
- steps[i].setState(seq[0][i], seq[1][i], seq[2][i], cur_seq);
- if (i == cur_stp) {
- steps[i].stepOn();
- }
- else {
- steps[i].stepOff();
- }
- }
- }
- void setLeds() {
- // sets all LEDs
- for (int i = 0; i < 3; i++) {
- if (i == cur_seq) {
- digitalWrite(sLedPins[i], 1);
- }
- else {
- digitalWrite(sLedPins[i], 0);
- }
- }
- }
- void readEEPROM() {
- // reads from EEPROM and fills seq array, called in setup
- int i, j;
- for (i = 0; i < 3; i++) {
- for (j = 0; j < 4; j++) {
- seq[i][j] = EEPROM.read(j + (4 * i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement