switches with led per switch

hi there people,

i am still very new to this forum and arduino so bare with me. i am currently working on a midi controller pedal board and have tried a number of different things so far. my question today is- i want to use 8 switches (momentary) from an arduino uno r3 and would like to each send a seporate midi note, however i would like to also use an led for each switch that stays on until the switch is pressed again and it goes off. example: swicthed pressed 1st time, send midi not and led is on, press the switch 2nd time and no midi note and led goes off.

is this possible with the uno r3 or do i need more inputs/outputs like an arduino mega? and iff possible how would i go about it.

many thanks


Sure, just need 8 inputs, 8 outputs, and the serial port for MIDI. Still have 2 IO pins free.

but would that not then take up the rx and tx pins just on the switches and leds?

No, you have 20 IO pins available, D0-D13, and D14-D19 (A0-A5 pins). So D0, D1 for serial D2-D9 for switch inputs D10-D17 for LED outputs D18, D19 free

oh i see i forgot about the PWM lol

ok thats kl and i take it i can still run the rx and tx pins to and from my midi-usb interface cable?

could you give me an example of one line of code for the switch being pressed then the led on until swicth pressed again and send the midi note C-1 please

thanks for your help so far btw.

oh i see i forgot about the PWM

I suspect you may be confusing analogue input pins with digital PWM output pins.

Code could be something like this as a starting point. Read a switch, turn LED on/off with alternate presses.
(not compiled, should be pretty close)

D2-D9 are switch inputs
D10-D17 are LED outputs, High = LED on, don’t forget current limit resistors
D0, D1 are serial to start, MIDI IO later
LEDstate[ ] = {0,0,0,0,0,0,0,0,0,0}; // hold state of the LEDs, 2 extra since not using 0, 1
unsigned long readTime; // keep track of elapsed time, blink without delay style

void setup(){
// short loop to set up input pins with pullup resistors
for (byte x = 2; x<10; x=x+1){
pinMode (x, INPUT);
digitalWrite (x, HIGH);
// short loop to set up output pins
for (byte x = 10; x<18; x=x+1){
pinMode (x, OUTPUT);
digitalWrite (x, LOW);
Serial.begin(9600); // use serial monitor to send regular message to start, replace with MIDI at 31500 when ready
} // end setup

void loop(){
if (millis()>=readTime){
readTime = readTime +5; // read the switches every 5mS
// read the switches, flip status bits as needed
  for (byte x = 2; x<10; x=x+1){
    if (digitalRead (x) == LOW){
    LEDstate[x] = 1 – LEDstate[x];  // results in 1,0,1,0 on switch closures from pin to ground
    digitalWrite (x+8, LEDstate[x] ); // press 2, turn 10 on/off. press 3, turn 11 on/off. Etc.
   Serial.print (“switch & state : “);
   Serial.print (x);
   Serial.println (LEDstate[x]);
    } //end read/write
  } // end 2-9 loop
 } // end time check
} // end void loop

Sending MIDI signals thru your USB cable is also an option if you decide that you want more buttons
the tutorial for that is here: http://www.spikenzielabs.com/SpikenzieLabs/Serial_MIDI.html

furtermore if you want a momentary switch to act like a SPSTyou have to do so using software

/*using pin 1 as the input of the switch, an pin 2 as the led*/

boolean led1 =FALSE;        // the status of the led
boolean status1= FALSE;     //the status of the button

if (digitalRead(1)!=status1){   // if the button is different that in wat before
if (status1)led1!=led1;     //if the button is being pushed, toggle the led
status1 = digitalRead1(1);
digitalWrite(2,led1); // update the led

i hope this helps you