24 leds from momentary switches, plus servo, plus speed controller

I am very much a newbie to this so please bear with me.

I am using a Mega2560 board. 24 input lines (momentary low as input is activated, 24 leds to show position activated by momentary switch) The board also controls a single channel servo output plus a DC motor speed controller.

It all works well using Virtronics Simulator for Arduino. The code as written compiles and runs fine on the Mega board, everything works.

The problem I am having is a simple one, I can't work out how to enable the INPUT_PULLUP for pins 5 to 28 (the switches defined as switch_up and switch_down) Using that will save me using 24 external pullup resistors.

PaulPB was very helpful in a previous posting (Controlling LEDS with momentary inputs - LEDs and Multiplexing - Arduino Forum), but I can't get his code for enabling the pullup to work - I'm certain its how I am trying to implement it

Any help would be appreciated.

const byte switch_up[12] = {5,7,9,11,13,15,17,19,21,23,25,27};
const byte switch_down[12] = {6,8,10,12,14,16,18,20,22,24,26,28};
const byte led_up[12] = {30,32,34,36,38,40,42,44,46,48,50,53};
const byte led_down[12] = {29,31,33,35,37,39,41,43,45,47,49,51};

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
 
int servopot0 = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
int motorpot1 = A1;
int motorPin = 2;
int potValue = 0;
int motorValue = 0;
void setup() 
{ 
  myservo.attach(3);  // attaches the servo on pin 3 to the servo object 
  }
 
void loop() { 
  val = analogRead(servopot0);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 

  potValue = analogRead(motorpot1);  
  motorValue = map(potValue, 0, 1023, 0, 255);
  analogWrite(motorPin, motorValue);  
  delay(2);    

for (byte i = 0; i < 12; i++) {
  if (digitalRead(switch_up[i]) == LOW) {
    digitalWrite(led_up[i], HIGH);
    digitalWrite(led_down[i], LOW);
  }
  if (digitalRead(switch_down[i]) == LOW) {
    digitalWrite(led_down[i], HIGH);
    digitalWrite(led_up[i], LOW);
  }
}
 }

Hello,

See pinMode

Thanks Guix,

I did indeed look at "pinMode"

Its easy if I want to list line by line for 24 pins, however I had hoped for rather less than 24 lines added to my code - any suggestions or pointers on how to optimize from you - other than your suggestion of "See pinMode"

Please don't forget, we all started learning somewhere and we all did not become experts overnight - hence my question.

As you can see from the code the pins are listed as const bytes

Use a for loop,

for ( uint8_t pin = 5; pin <= 28; pin++ )
  pinMode( pin, INPUT_PULLUP );

or maybe better in your case:

for ( uint8_t i = 0; i < 12; i++ )
{
  pinMode( switch_up[ i ], INPUT_PULLUP );
  pinMode( switch_down[ i ], INPUT_PULLUP );
}

My thanks for that - the second suggestion works really well. I read up on uint8_t and that is very helpful, the code is running well

Thanks again