Hello
I have been programming my arduino to play certain musical notes when you push a button.
Basically you can say I have programmed it to act as a basic square wave synth.
Not to the question:
At the moment the synth is monophonic, but I want to add an arpeggiator function so that if I press two notes at the same time there will be an arpeggio between these notes.
I don't know how to write this in code and I've been googling around but I can't find a solution.
Here's the code. Help me if you can!
const int button1Pin = 2; // button 1 connected via I/0-pin no.2
const int button2Pin = 3; // etc
const int button3Pin = 4;
const int button4Pin = 5;
const int button5Pin = 6;
const int button6Pin = 7;
const int button7Pin = 8;
const int button8Pin = 9;
const int speakerPin = 13; // speaker pin
void setup()
{
pinMode(button1Pin, INPUT); // setting up the buttons to become inputs
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button4Pin, INPUT);
pinMode(button5Pin, INPUT);
pinMode(button6Pin, INPUT);
pinMode(button7Pin, INPUT);
pinMode(button8Pin, INPUT);
pinMode(speakerPin, OUTPUT); // setting the speakerpin as output (speaker connected to I/0 no.13
Serial.begin(9600); // starting serial messaging
}
void loop()
{
int button1State, button2State, button3State, button4State, button5State, button6State, button7State, button8State; // variables to hold the pushbutton states
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
button3State = digitalRead(button3Pin);
button4State = digitalRead(button4Pin);
button5State = digitalRead(button5Pin);
button6State = digitalRead(button6Pin);
button7State = digitalRead(button7Pin);
button8State = digitalRead(button8Pin);
if (button1State == HIGH){
tone(speakerPin, 130.81); // C ----- ((( variables = output pin and number value is frequenzy in hertz )))
Serial.println("C"); // Sending the name of the note via serial
}else if (button2State == HIGH){
tone(speakerPin, 146.83); // D
Serial.println("D");
}else if (button3State == HIGH){
tone(speakerPin, 164.81); // E
Serial.println("E");
}else if (button4State == HIGH){
tone(speakerPin, 174.61); // F
Serial.println("F");
}else if (button5State == HIGH){
tone(speakerPin, 196); // G
Serial.println("G");
}else if (button6State == HIGH){
tone(speakerPin, 220); // A
Serial.println("A");
}else if (button7State == HIGH){
tone (speakerPin, 246.94); // B
Serial.println("B");
}else if (button8State == HIGH){
tone(speakerPin, 261.63); // C2
Serial.println("C2");
}else{
noTone(speakerPin); // if no button is pushed - no sound will be played
}
}