Advertisement
Guest User

Digital Lab 7 - Ethan "lab_week_7.ino"

a guest
Oct 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <EEPROM.h>
  2. #include "classdefs.h"
  3.  
  4. bool seq[3][4] {
  5.   {1, 0, 0, 0},
  6.   {0, 1, 0, 0},
  7.   {0, 0, 1, 1}
  8. };
  9. int sLedPins[3] = {35, 36, 37};
  10. int cur_seq = 0;
  11. int cur_stp = 0;
  12. unsigned long lastTime = 0;
  13.  
  14. // button and step object lists
  15. Button buttons[6] = {
  16.   Button(14),
  17.   Button(15),
  18.   Button(16),
  19.   Button(17),
  20.   Button(33),
  21.   Button(34)
  22. };
  23. Step steps[4] {
  24.   Step(10),
  25.   Step(9),
  26.   Step(8),
  27.   Step(7)
  28. };
  29.  
  30. void setup() {
  31.   for (int i = 0; i < 3; i++)
  32.     pinMode(sLedPins[i], OUTPUT);
  33.   Serial.begin(9600);
  34.   readEEPROM();
  35. }
  36.  
  37. void loop() {
  38.   checkAllButtons();
  39.   if (millis() > lastTime + 250) {
  40.     setLeds();
  41.     nextStep();
  42.     lastTime = millis();
  43.   }
  44. }
  45.  
  46. void checkAllButtons() {
  47.   for (int i = 0; i < 6; i++) {
  48.     // first check button states
  49.     buttons[i].check();
  50.  
  51.     if (i < 4 && buttons[i].isPressed()) {
  52.       // this detects step buttons. on each press, the value is inverted and written to EEPROM
  53.       seq[cur_seq][i] = !seq[cur_seq][i];
  54.       EEPROM.write(i + (4 * cur_seq), seq[cur_seq][i]);
  55.       setLeds();
  56.     }
  57.     if (i >= 4 && buttons[i].isPressed()) {
  58.       // this detects sequence buttons. on each press it changes the sequence
  59.       nextSeq(i - 4);
  60.       setLeds();
  61.     }
  62.   }
  63. }
  64.  
  65. void nextSeq(int s) {
  66.   // switches the current sequence
  67.   s == 1 ? cur_seq++ : cur_seq--;
  68.   if (cur_seq < 0)
  69.     cur_seq = 2;
  70.   if (cur_seq > 2)
  71.     cur_seq = 0;
  72. }
  73.  
  74. void nextStep() {
  75.   // plays the next step, setting the state from the seq array and playing the midi notes
  76.   cur_stp++;
  77.   if (cur_stp > 3)
  78.     cur_stp = 0;
  79.   for (int i = 0; i < 4; i++) {
  80.     steps[i].setState(seq[0][i], seq[1][i], seq[2][i], cur_seq);
  81.     if (i == cur_stp) {
  82.       steps[i].stepOn();
  83.     }
  84.     else {
  85.       steps[i].stepOff();
  86.     }
  87.   }
  88. }
  89.  
  90. void setLeds() {
  91.   // sets all LEDs
  92.   for (int i = 0; i < 3; i++) {
  93.     if (i == cur_seq) {
  94.       digitalWrite(sLedPins[i], 1);
  95.     }
  96.     else {
  97.       digitalWrite(sLedPins[i], 0);
  98.     }
  99.   }
  100. }
  101.  
  102. void readEEPROM() {
  103.   // reads from EEPROM and fills seq array, called in setup
  104.   int i, j;
  105.   for (i = 0; i < 3; i++) {
  106.     for (j = 0; j < 4; j++) {
  107.       seq[i][j] = EEPROM.read(j + (4 * i));
  108.     }
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement