Think Crate projects

Think Crate projects

Brandon has been playing around with Think Crate projects. He's built a piano (pictured above).

0:00
/

And he has also built a Planetarium project.

Here's his code for the Piano project:

bool activateSecondCode = false;
bool button11Pressed = false;
bool button12Pressed = false; // Add a variable to track button 12 state

float frequencies[3][8] = { {246.94, 220.00, 195.99, 174.61, 164.81, 146.83, 130.81, 100.0}, // lower octave
                            {261.63, 293.63, 329.63, 349.22, 391.99, 440.0,  493.88, 523.25}, // middle octave
                            {659.25, 698.45, 783.99, 880.00, 987.76, 1046.50, 1174.65, 1318.51 } // high octave
                          };
int selected_octave = 1;

void setup() {
  pinMode(2, INPUT);
  pinMode(10, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);

  pinMode(11, INPUT); // blue button
  pinMode(12, INPUT); // red button
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(11) == LOW && !button11Pressed) {
    button11Pressed = true; // Track button 11 state

    selected_octave -= 1;
    if (selected_octave < 0) {
      selected_octave = 0; // Limit to the minimum value (0)
    }
    Serial.println(selected_octave);
  } else if (digitalRead(11) == HIGH) {
    button11Pressed = false; // Reset button 11 state
  }

  if (digitalRead(12) == LOW && !button12Pressed) {
    button12Pressed = true; // Track button 12 state

    selected_octave += 1;
    if (selected_octave > 2) {
      selected_octave = 2; // Limit to the maximum value (2)
    }
    Serial.println(selected_octave);
  } else if (digitalRead(12) == HIGH) {
    button12Pressed = false; // Reset button 12 state
  }

  
  if (digitalRead(2) == LOW) {
    tone(10, frequencies[selected_octave][0]);  // play tone 60 (C5 = 261.63 Hz)
  } else if (digitalRead(3) == LOW) {
    tone(10, frequencies[selected_octave][1]);  // play tone 60 (C5 = 293.66 Hz)
  } else if (digitalRead(4) == LOW) {
    tone(10, frequencies[selected_octave][2]);  // play tone 60 (C5 = 329.63 Hz)
  } else if (digitalRead(5) == LOW) {
    tone(10, frequencies[selected_octave][3]); // play tone 60 (C5 = 349.22 Hz)
  } else if (digitalRead(6) == LOW) {
    tone(10, frequencies[selected_octave][4]);  // play tone 60 (C5 = 391.99 Hz)
  } else if (digitalRead(7) == LOW) {
    tone(10, frequencies[selected_octave][5]); // play tone 60 (C5 = 440.0 Hz)
  } else if (digitalRead(8) == LOW) {
    tone(10, frequencies[selected_octave][6]);  // play tone 60 (C5 = 493.88 Hz)
  } else if (digitalRead(9) == LOW) {
    tone(10, frequencies[selected_octave][7]); // play tone 60 (C5 = 523.25 Hz)
  } else {
    noTone(10);
  }
}