Good day! I find it hard to code what I wanna happen. I use MIDI and ATMEGA 328p. Everytime I push the button (assigned with correspnding sound) it should only play one. But, with my code, as i ress it harder, it would play successively. How can I modify the code to attain my goal ?
#include <avr/io.h>
//void USART_Transmit( unsigned char data );
int ON = 100;//velocity of MIDI notes (between 0 and 127)
int OFF = 0;
int noteON = 153;//midi note on channel 10 percussion
int note;
void setup()
{
DDRB = 0xFF; // Set port B as output
DDRD = 0x00; // Set port D as input
PORTD = 0x00; // Clear Port D pins
//Set MIDI baud rate:
Serial.begin(31250);
}
void loop()
{
if(PIND & (1<<2)) //If switch2 is pressed
{
//Bass drum
note = 35;
MIDImessage(noteON, note, ON);
PORTB = 0x01; //Turns ON LED1
delay(150);
MIDImessage(noteON, note, OFF);
//Serial.println("On");
}
if(PIND & (1<<3)) //If switch2 is pressed
{
//Snare drum
note = 38;
MIDImessage(noteON, note, ON);
PORTB = 0x02; //Turns ON LED2
delay(150);
MIDImessage(noteON, note, OFF);
//Serial.println("On");
}
if(PIND & (1<<4)) //If switch3 is pressed
{
//High Hat
note = 42;
MIDImessage(noteON, note, ON);
PORTB = 0x04; //Turns ON LED3
delay(150);
MIDImessage(noteON, note, OFF);
//Serial.println("On");
}
if(PIND & (1<<5)) //If switch4 is pressed
{
//Crash cymbal
note = 49;
MIDImessage(noteON, note, ON);
PORTB = 0x08; //Turns ON LED4
delay(150);
MIDImessage(noteON, note, OFF);
//Serial.println("On");
}
if(PIND & (1<<6)) //If switch5 is pressed
{
//right tom
note = 45;
MIDImessage(noteON, note, ON);
PORTB = 0x10; //Turns ON LED5
delay(150);
MIDImessage(noteON, note, OFF);
//Serial.println("On");
}
else
{
PORTB = 0x00; //Turns OFF LEDs
}
}
//send MIDI message
void MIDImessage(int command, int MIDInote, int MIDIvelocity)
{
USART_Transmit(command);//send note on or note off command
USART_Transmit(MIDInote);//send note number (determines what drum to play)
USART_Transmit(MIDIvelocity);//send velocity data
//Serial.write(command);
//Serial.write(MIDInote);
//Serial.write(MIDIvelocity);
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}