суббота, 11 января 2014 г.

Smoke on the water на Arduino

Пианино на Arduino. Ну пианино это громко сказано, но кнопки и пищалка есть :)
Пищалку очень слабо тут слышно.




Для Arduino используется tone.

#include <Tone.h>

Tone tone1;

// digital pin 2 has a pushbutton attached to it. Give it a name:
int b2_code = 2;
int b3_code = 3;
int b4_code = 4;
int b5_code = 5;

int b2 = 0;
int b3 = 0;
int b4 = 0;
int b5 = 0;

// the setup routine runs once when you press reset:
void setup() {
  tone1.begin(13);
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(b2_code, INPUT);
  pinMode(b3_code, INPUT);
  pinMode(b4_code, INPUT);
  pinMode(b5_code, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int b2_new = digitalRead(b2_code);
  int b3_new = digitalRead(b3_code);
  int b4_new = digitalRead(b4_code);
  int b5_new = digitalRead(b5_code);
  // print out the state of the button:
  if (b2_new != b2) {
    outValue(b2_code, b2_new);
    b2 = b2_new;
    if (b2_new == 1) {
      tone1.play(NOTE_A3);
    }
  }
  if (b3_new != b3) {
    outValue(b3_code, b3_new);
    b3 = b3_new;
    if (b3_new == 1) {
      tone1.play(NOTE_C4);
    }
  }
  if (b4_new != b4) {
    outValue(b4_code, b4_new);
    b4 = b4_new;
    if (b4_new == 1) {
      tone1.play(NOTE_D4);
    }
  }
  if (b5_new != b5) {
    outValue(b5_code, b5_new);
    b5 = b5_new;
    if (b5_new == 1) {
      tone1.play(NOTE_DS4);
    }
  }
  if (b2_new == 0 && b3_new == 0 && b4_new == 0 && b5_new == 0) {
    tone1.stop();
  }
  delay(10);        // delay in between reads for stability
}

void outValue(int code, int value) {
  if (code == b2_code) {
    if (value == 0) {
      Serial.println("b2: 0");
    }
    else {
      Serial.println("b2: 1");
    }
  }

  if (code == b3_code) {
    if (value == 0) {
      Serial.println("b3: 0");
    }
    else {
      Serial.println("b3: 1");
    }
  }

  if (code == b4_code) {
    if (value == 0) {
      Serial.println("b4: 0");
    }
    else {
      Serial.println("b4: 1");
    }
  }

  if (code == b5_code) {
    if (value == 0) {
      Serial.println("b5: 0");
    }
    else {
      Serial.println("b5: 1");
    }
  }
}
На код и самому смотреть страшненько, но на C давно не писал, поэтому код в стиле спагетти.

1 комментарий:

  1. Slot Machines At Mohegan Sun - JT Hub
    Check out our casino slot machines 과천 출장마사지 at Mohegan Sun. Get 진주 출장마사지 free access 남양주 출장마사지 to exclusive 천안 출장샵 bonuses, games, promotions and 안동 출장샵 contact us today!

    ОтветитьУдалить