Вы находитесь на странице: 1из 5

/* The lines below just Declares(or tells the code what external modules it need

s to be aware of)
which digital pins on the RedBoard PWM are connected to the modules to the bread
board. */
const int button1Pin = 10; // push button =10 pin
const int button2Pin = 11; // push button =11 pin
const int button3Pin = 2; // push button = 2 pin
const int button4Pin = 3; // push button = 3 pin
const int button5Pin = 4; // push button = 4 pin
const int button6Pin = 5; // push button = 5 pin
const int button7Pin = 6; // push button = 6 pin
const int button8Pin = 7; // push button = 7 pin
const int buzzerPin = 9; // buzzer pin = 9 pin
void setup() {
/* The lines below declares to the code that each of the external modules are in
puts,
like the buttons, or outputs, like the buzzer, and link the modules to the corre
sponding pin*/
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button4Pin, INPUT);
pinMode(button5Pin, INPUT);
pinMode(button6Pin, INPUT);
pinMode(button7Pin, INPUT);
pinMode(button8Pin, INPUT);
pinMode(buzzerPin, OUTPUT);
}

void loop() {
/* These declare the state (HIGH = ON, HIGH = OFF), of the buttons */
int button1State, button2State, button3State, button4State,
button5State, button6State, button7State, button8State ;
/* These lines compare the states (LOW-HIGH) to the voltage from the pins, and d
efines them as such*/
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
button3State = digitalRead(button3Pin);
button4State = digitalRead(button4Pin);
button5State = digitalRead(button5Pin);
button6State = digitalRead(button6Pin);
button7State = digitalRead(button7Pin);
button8State = digitalRead(button8Pin);

/* The lines below define the behaviour of one selected button. It plays if its
state is LOW, and the states of the rest of the buttons are HIGH.
The tone() makes the output Pin, which is buzzerPin, play a frequency and is wri
tten as tone(outputpin, frequency)*/

//"C4"
if ((button1State == LOW) && ((button2State == HIGH) &&
(button3State == HIGH) &&
(button4State == HIGH) &&
(button5State == HIGH) &&
(button6State == HIGH) &&
(button7State == HIGH) &&
(button8State == HIGH) ))
/* Once the behaviour is described, the tone function plays the note (in the "Wh
ite Key Range", to which pin the button is connected to,
and then ends with noTone for silence. The same applies for the rest of the sin
gle-button-press arguments below */

{
tone(buzzerPin,262);
}
// "D4"
else if ((button2State == LOW) && ((button1State == HIGH) &&
(button3State == HIGH) &&
(button4State == HIGH) &&
(button5State == HIGH) &&
(button6State == HIGH) &&
(button7State == HIGH) &&
(button8State == HIGH) ))
{
tone(buzzerPin, 294);
}
//"E4"
else if ((button3State == LOW) && ((button1State == HIGH) &&
(button2State == HIGH) &&
(button4State == HIGH) &&
(button5State == HIGH) &&
(button6State == HIGH) &&
(button7State == HIGH) &&
(button8State == HIGH) ))
{
tone(buzzerPin,330);
}
//"F4"
else if ((button4State == LOW) && ((button1State == HIGH) &&
(button2State == HIGH) &&
(button3State == HIGH) &&
(button5State == HIGH) &&
(button6State == HIGH) &&
(button7State == HIGH) &&
(button8State == HIGH) ))
{
tone(buzzerPin,349);
}
//"G4"
else if ((button5State == LOW) &&((button1State == HIGH) &&
(button2State == HIGH) &&
(button3State == HIGH) &&
(button4State == HIGH) &&
(button6State == HIGH) &&
(button7State == HIGH) &&
(button8State == HIGH) ))
{
tone(buzzerPin,392);
}
//"A4"
else if ((button6State == LOW) &&((button1State == HIGH) &&
(button2State == HIGH) &&
(button3State == HIGH) &&
(button4State == HIGH) &&
(button5State == HIGH) &&
(button7State == HIGH) &&
(button8State == HIGH) ))
{
tone(buzzerPin,440);
}
//"B4"
else if ((button7State == LOW) &&((button1State == HIGH) &&
(button2State == HIGH) &&
(button3State == HIGH) &&
(button4State == HIGH) &&
(button5State == HIGH) &&
(button6State == HIGH) &&
(button8State == HIGH) ))
{
tone(buzzerPin,494);
}
//"C5"
else if ((button8State == LOW) && ((button1State == HIGH) &&
(button2State == HIGH) &&
(button3State == HIGH) &&
(button4State == HIGH) &&
(button5State == HIGH) &&
(button6State == HIGH) &&
(button7State == HIGH) ))
{
tone(buzzerPin,523);
}

/* The next lines use a combination of two buttons' LOW states, and keeps the re
st of the states as HIGH, to create another set of notes ("Black Keys"). */
// "C#/Db 4"
else if ((button1State == LOW) && (button2State == LOW) &&! ((button3State == LO
W) &&
(button4State == LOW) &&
(button5State == LOW) &&
(button6State == LOW) &&
(button7State == LOW) ))

{
tone(buzzerPin ,277);
}
// "D#/Eb 4"
else if (((button2State == LOW) && (button3State == LOW)) &&! ((button1State ==
LOW) &&
(button4State == LOW) &&
(button5State == LOW) &&
(button6State == LOW) &&
(button7State == LOW) ))

{
tone(buzzerPin ,311);
}
// "F#/Gb 4"
else if (((button4State == LOW ) && (button5State == LOW)) &&! ((button1State ==
LOW) &&
(button2State == LOW) &&
(button3State == LOW) &&
(button6State == LOW) &&
(button7State == LOW) ))

{
tone(buzzerPin ,370);
}
// "G#/Ab 4"
else if ((button5State == LOW) && (button6State == LOW) &&! ((button1State == LO
W) &&
(button2State == LOW) &&
(button3State == LOW) &&
(button4State == LOW) &&
(button7State == LOW) ))

{
tone(buzzerPin ,415);
}
// "A#/Bb 4"
else if ((button6State == LOW) && (button7State == LOW) &&! ((button1State == LO
W) &&
(button2State == LOW) &&
(button3State == LOW) &&
(button4State == LOW) &&
(button5State == LOW) ))

{
tone(buzzerPin ,466);
/* If all buttons conditions are not met, then don't play a tone. */
}
else
{
noTone(buzzerPin);
}
}

Вам также может понравиться