21 September 2019

Multi Switch : Making Music

GOAL: Design a system to control multiple LEDs with switches and code.

OBJECTIVES: Practice fundamental coding for Arduino using digital inputs and outputs.

MATERIALS USED:
  • 3 LEDs
  • 3 Switches / buttons
  • 3 Resistors appropriate to the LEDs
  • 3 Resistors appropriate to the buttons/switches
  • 1 Arduino
  • 1 Breadboard
  • 13 Jumper wires
  • 1 Passive Buzzer (It might be active, honestly, I forgot)


Playin' Music

Like most people, I like music.
Honestly, I'm not sure who doesn't, but I guess they're out there (y'all are weirdos though).

Anyway, thanks to this affinity and my impatience to get to other components in the elegoo box, I decided to create a makeshift sound board. Or, after I discovered it's a little hard to play more than two tones simultaneously, a makeshift music box. Set on this idea, I began to hash it out using good old fashion doodling:

Demonstrating connections through
an unintentional Powerpuff Girl motif

Messy, but a start. 

In the beginning, I knew that I wanted each button to correspond to an LED and, in turn, have a specific impact on the speaker. So, logically, a button would be pressed and, using a conditional, the code would fire off a song sequence and light up an LED. The building process itself took this idea, refined it several times, and expanded on it by adding a toggle state to the final button. By doing so, each button plays a song-- except the third, which actually plays *drum roll* two!

Here's a look at the result:



Step aside Marble Soda, there's a new mash up in town

The sound board uses seven pins total to connect the main acting components. I also tried to set it up a little more ergonomically; as a result, some of the wires are color coded, and the placement of each button correlates with the placement of an LED. In other words, the furthest left button generally lights the furthest left LED.

To further refine this design, I also attempted (keyword, here) to create a schematic and code that are like-wise easy to view or understand. These renditions can be seen below:

Pin-wise: the speaker was 3, the LEDs were 5-7, and the buttons were 8-10,
though I wasn't quite sure how to communicate this yet.

(Note: the pitches.h file is the same used here.)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "pitches.h"

// Firstly: Thanks @ Arduino toneMelody() tutorial

/*************************************************
 * Songs and Tempos
 *************************************************/

// Left non-const because I want to try changing these via button in the future.
// Song tempos may not be totally accurate because I made these by ear.

int zeldasSong[] = {
  NOTE_E4, NOTE_G4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_G4, NOTE_D4
};

int sariasSong[] = {
  NOTE_F4, NOTE_A4, NOTE_B4, NOTE_F4, NOTE_A4, NOTE_B4,
  NOTE_F4, NOTE_A4, NOTE_B4, NOTE_E5, NOTE_D5
};

int eponasSong[] {
  NOTE_D5, NOTE_B4, NOTE_A4, R, NOTE_D5, NOTE_B4, NOTE_A4, R,
  NOTE_D5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_A4
};

int marioSong[] = {
  NOTE_E4, NOTE_E4, NOTE_E4,
  NOTE_C4, NOTE_E4, NOTE_G4, R, NOTE_G3
};

int zeldaTempo[] = {
  2,4,2,8,8,2,4,2
};

int sariaTempo[] = {
  8,8,4,8,8,4,8,8,8,8,4
};

int eponaTempo[] {
  3,5,1,12,3,5,1,12,3,5,2,2,1
};

int marioTempo[] = {
  8,4,4,8,4,3,6,3
};

/*************************************************
 * Pins
 *************************************************/
// These are a bit more organic as that's how I've best memorized variables in past. 
// Speaker

const int speakerBoy = 3;

// LEDs

const int blueDude = 5;
const int greenDude = 6;
const int redDude = 7;

// Buttons

const int blueHome = 8;
const int greenHome = 9;
const int redHome = 10;
int redToggle = 0;

/*************************************************
 * Setup
 *************************************************/

void setup() {
  // setting up pins
  pinMode(speakerBoy, OUTPUT);
  
  pinMode(blueDude, OUTPUT);
  pinMode(redDude, OUTPUT);
  pinMode(greenDude, OUTPUT);

  pinMode(blueHome,INPUT);
  pinMode(greenHome,INPUT);
  pinMode(redHome,INPUT);

  // this is just for aesthetics
  delay(500);                     
  digitalWrite(greenDude, HIGH);   
  delay(500); 
  digitalWrite(redDude, HIGH);
  delay(500);
  
  digitalWrite(blueDude, LOW);
  digitalWrite(greenDude, LOW);
  digitalWrite(redDude, LOW);

  // Serial.begin(9600);
}

/*************************************************
 * Loop
 *************************************************/

void loop() {
  // assign variables based on button state
  
  int blueState = digitalRead(blueHome);
  int greenState = digitalRead(greenHome);
  int redState = digitalRead(redHome);

  // FIRST: first button, first song, blue LED
  // If button is pressed, light LED and play song
  // Otherwise, turn off LED when song ends
  
  if (blueState == HIGH) {
    digitalWrite(blueDude, HIGH);
    // first melody
    // run through lullaby while changing note and duration
    for (int thisNote = 0; thisNote < 8; thisNote++) {
      int noteDuration = 1000/zeldaTempo[thisNote];
      tone(speakerBoy, zeldasSong[thisNote], noteDuration);
      int notePause = noteDuration * 1.30;
      delay(notePause);
    } 
  } else {
    digitalWrite(blueDude, LOW);
  }

  // SECOND: second button, second song, green LED
  
  if (greenState == HIGH) {
    digitalWrite(greenDude, HIGH);
    // second melody
    for (int thisNote = 0; thisNote < 11; thisNote++) {
      int noteDuration = 1000/sariaTempo[thisNote];
      tone(speakerBoy, sariasSong[thisNote], noteDuration);
      int notePause = noteDuration * 1.30;
      delay(notePause);
    }
  } else {
    digitalWrite(greenDude, LOW);
  }

  // THIRD: Third button, last two songs, mix of LEDs :^)
  // Uses a toggle based on 'red' button state for a bit more complexity
  // If button is pressed, first check toggle to decide what LEDs and song to run
  
  if (redState == HIGH) {
      if (redToggle == 0) {
      digitalWrite(redDude, HIGH);
      redToggle++;
      // third melody
        for (int thisNote = 0; thisNote < 13; thisNote++) {
          int noteDuration = 1000/eponaTempo[thisNote];
          tone(speakerBoy, eponasSong[thisNote], noteDuration);
          int notePause = noteDuration * 1.30;
          delay(notePause);
         }
      } else {
        // third alt melody
        digitalWrite(redDude, HIGH);
        digitalWrite(blueDude, HIGH);
        redToggle = 0;
        for (int thisNote = 0; thisNote < 8; thisNote++) {
          int noteDuration = 1000/marioTempo[thisNote];
          tone(speakerBoy, marioSong[thisNote], noteDuration);
          int notePause = noteDuration * 1.30;
          delay(notePause);
        }
      }
  } else {
    digitalWrite(redDude, LOW);
    digitalWrite(blueDude, LOW);
  }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.