Saturday, June 16, 2018

Cardboard Electronic Pianos

We built Cardboard Electronic Pianos at the Queen Creek Library and Glendale Velma Teague Library. These are really basic with a single C scale using Arduino Nano, cardboard cut on the laser, copper tape, and a few wires. Removed a lot of complexity and left some room for upgrades to the code.

Below is the code for the piano. Could use some debounce, or the ability to play multiple notes.

#include "pitches.h"

int buttonState = 0;

int buttons[] = {
  2, 3, 4, 5, 6, 7, 8, 9
};

int tones[] = {
  NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};


void setup() {
  Serial.begin(9600);
  pinMode(buttons[0], INPUT_PULLUP);
  pinMode(buttons[1], INPUT_PULLUP);
  pinMode(buttons[2], INPUT_PULLUP);
  pinMode(buttons[3], INPUT_PULLUP);
  pinMode(buttons[4], INPUT_PULLUP);
  pinMode(buttons[5], INPUT_PULLUP);
  pinMode(buttons[6], INPUT_PULLUP);
  pinMode(buttons[7], INPUT_PULLUP);
  //pinMode(11, OUTPUT);
  //digitalWrite(11, LOW);
}

void loop() {
  int keypressed = false;
  for (int x = 0; x < 8; x++) {
    buttonState = digitalRead(buttons[x]);
//      Serial.print("button - ");
//      Serial.print(buttons[x]);
//      Serial.print(" - ");
//      Serial.println(buttonState);
      if (buttonState == LOW) {
        tone(12, tones[x]);
        delay(10);
        keypressed = true;
        break;
    }
  }
 
  if (!keypressed) noTone(12);

  // delay(1);
}

No comments:

Post a Comment