This week i tried to recreate music mix pult with the help of arduino. In the circuit there are 3 buttons, each corresponds to a certain sound, pressing them with different duration you can get different sounds. Also i included micro servo thing, which can be managed by photo resister.
To make micro servo “play” i combined it with the metal straw, at the same time i took a knife to hit them to get interesting and clear sound. As a stick is really heavy comparing to the servo i placed it the table side.
Code:
#include "pitches.h" #include<Servo.h> int but1 = 13; int but2 = 12; int but3 = 11; int sound = 9; Servo servo; int i =0; void setup() { pinMode(but1, INPUT); pinMode(but2, INPUT); pinMode(but3, INPUT); pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(sound, OUTPUT); servo.attach(7); servo.write(180); Serial.begin(9600); } void loop() { int b1 = digitalRead(but1); int b2 = digitalRead(but2); int b3 = digitalRead(but3); int photoValue = analogRead(A1); int triggerInterval = map(photoValue, 0, 100, 0, 100); for ( i= 0; i<180; i++);{ servo.write(i); delay(triggerInterval); } for ( i=180; i>=1; i--);{ servo.write(i); delay(triggerInterval); } //Serial.println(triggerInterval); if (b1 == 1){ tone (sound, NOTE_A3, 200); delay(100); tone (sound, NOTE_A1, 100); delay(90); tone (sound, NOTE_A3, 100); delay(80); tone (sound, NOTE_A1, 100); delay(80); } if (b2 == 1){ //digitalWrite(led2, HIGH); tone (sound, NOTE_A3, 100); delay(100); tone (sound, NOTE_A1, 100); delay(80); tone (sound, NOTE_A3, 100); delay(100); tone (sound, NOTE_A1, 500); tone (sound, NOTE_A3, 500); tone (sound, NOTE_A4, 500); tone (sound, NOTE_A4, 500); } if (b3 == 1){ tone (sound, NOTE_A1, 500); delay(80); tone (sound, NOTE_A3, 500); delay(80); } }
Nice avant-garde band! The last few lines of your code won’t work as you expect though. You can’t play multiple notes at a time with the tone library. Also, let’s move away from using delay while having interaction. All the delays you have will make the interaction be perceived as not working very well, since many times when you press a button the program will be stopped somewhere with one of the delays.