Understanding Pinmode.

Hi,

Could some helpful person please explain this code?

I see from the code that the switches are set from 1 to 4 but why is it that the pinmode goes from 2 to 5?

As does the digital read.

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
int switch1 = 0;
int switch2 = 0;
int switch3 = 0;
int switch4 = 0;

byte last_switch1 = LOW;
byte last_switch2 = LOW;
byte last_switch3 = LOW;
byte last_switch4 = LOW;

void setup() {
  // put your setup code here, to run once:
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  MIDI.begin(14);
}

void loop() {
  // put your main code here, to run repeatedly:
  switch1 = digitalRead(2);
  switch2 = digitalRead(3);
  switch3 = digitalRead(4);
  switch4 = digitalRead(5);

I've tried google but can't find an explanation. :o

Thanks,

Martin

I see from the code that the switches are set from 1 to 4 but why is it that the pinmode goes from 2 to 5?

the switch number is just for the variable name, you can use any name you want.

The 2...5 used in pinmode() are the actual pins that the switches are wired to. The variables just happened to be named switch1, switch2... (which, by the way are TERRIBLE names. Names should mean something like "ResetSwitch" or "PowerSwitch"...)

well - when you start numbering variables, it means it's time to use an array...

Hi,

Thanks for the excellent replies. :slight_smile:

Please don't blame me for the coding, it was one I found on the net that was close to doing what I wanted.

I agree an array would be better but it would most likely take up as many lines. :confused:

Martin

I agree an array would be better but it would most likely take up as many lines

I bet it wouldn't and in any case the number of lines does not matter, it is the amount, efficiency and ease of understanding and maintenance of the code that matters

I think it's shorter :slight_smile:

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
int switch[4] ;
byte last_switch[4];

void setup() {
  for (byte i = 0; i<4; i++)  pinMode(2+i, INPUT);
  MIDI.begin(14);
}

void loop() {
  for (byte i = 0; i<4; i++) switch[i] = digitalRead(2+i);
..

Hi J-M-L,

Thanks very much for the reply. :slight_smile:

That is much better, I still have a long way to go to be able to code that well. :o

Unfortunately, I am getting expected unqualified-id before 'switch'

Martin

I had left a bit of homework for you :slight_smile:

Seems you are new to C/C++ so here is the answer:
switch is actually a reserved programming word (see switch...case) so we can’t use it for a variable name of course... use switch[b]es[/b] for example instead of switch

Replace int switch[4] ;withint switches[4] ;

Also change the loop() into

void loop() {
  for (byte i = 0; i<4; i++) switches[i] = digitalRead(2+i);
..

At the bottom part I see that it says:
switch1 = digitalRead(2);
switch2 = digitalRead(3);
switch3 = digitalRead(4);
switch4 = digitalRead(5);
that means every switch is related one of your outputs
switch1 is equal to digitalRead(2)
switch2 is equal to digitalRead(3)
switch3 is equal to digitalRead(4)
switch4 is equal to digitalRead(5)

Hi,

Thanks again for the replies. :slight_smile:

Yes, I'm new to C++ but trying to learn quickly.

Sometimes despite a long google session I just can't find the answers. :confused:

Thanks for the advice, I'll change the variable name in all locations.

Martin