/hardware/

Ximon78-piano

2020-12-19 15:30:00

Piano.ino

#include <LedControl.h>

const int DIN_PIN = 4; // LED
const int CS_PIN = 3; // LED
const int CLK_PIN = 2; // LED
LedControl display = LedControl(DIN_PIN, CLK_PIN, CS_PIN); // LED


const uint64_t NUMBERS[] = {
  0x3c66060606663c00,
  0x3e66666666663e00,
  0x7e06063e06067e00,
  0x0606063e06067e00
  };

const int buttonPin1 = A1;
const int buttonPin2 = A2;
const int buttonPin3 = A3;
const int buttonPin4 = A4;

int b1 = 0;
int b2 = 0;
int b3 = 0;
int b4 = 0;

const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;

const int buzzerPin = 5;
void setup() {   
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);

  display.clearDisplay(0); // LED
  display.shutdown(0, false); // LED
  display.setIntensity(0, 1); // LED
}

void loop(){

  b1 = digitalRead(buttonPin1);
  b2 = digitalRead(buttonPin2);
  b3 = digitalRead(buttonPin3);
  b4 = digitalRead(buttonPin4);

  if (b1 == HIGH) {
    displayImage(NUMBERS[0]);
    beep(c, 255);
  }
  if (b2 == HIGH) {
    displayImage(NUMBERS[1]);
    beep(d, 255);
  }
  if (b3 == HIGH) {
    displayImage(NUMBERS[2]);
    beep(e, 255);
  }
  if (b4 == HIGH) {
    displayImage(NUMBERS[3]);
    beep(f, 255);
  }
}

void beep(int note, int duration) {
  tone(buzzerPin, note, duration);
  delay(200);
  noTone(buzzerPin);
}

void displayImage(uint64_t image) { // LED
  for (int i = 0; i < 8; i++) { // LED
    byte row = (image >> i * 8) & 0xFF; // LED    
    for (int j = 0; j < 8; j++) { // LED
      display.setLed(0, i, j, bitRead(row, j)); // LED
    } // LED
  } // LED
} // LED