Array/binairy question

Hello today I worked on this project and in the switch/case I use alot of digitalWrite with 4 leds.
Is there away to write this shorter?

const int  buttonPin = A1;    // the pin that the pushbutton is attached to
const int ledPin0 = 13;       // the pin that the LED is attached to
const int ledPin1 = 12;       // the pin that the LED is attached to
const int ledPin2 = 11;       // the pin that the LED is attached to
const int ledPin3 = 10;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 15;   // counter for the number of button presses
bool buttonState = 0;         // current state of the button
bool lastButtonState = 1;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, LOW);
  // initialize the LED as an output:
  pinMode(ledPin0, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  buttonPushCounter = 0;
  while(buttonPushCounter <= 15){
  
  
  // read the pushbutton input pin:
  buttonState = !digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
   switch (buttonPushCounter) {
      case 0:
        digitalWrite(ledPin0, HIGH);
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, HIGH);
        break;

      case 1:

        digitalWrite(ledPin0, LOW);
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, HIGH);
        break;

      case 2:

        digitalWrite(ledPin0, HIGH);
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, HIGH);
        break;

      case 3:

        digitalWrite(ledPin0, LOW);
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, HIGH);
        break;

      case 4:

        digitalWrite(ledPin0, HIGH);
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, HIGH);
        break;

      case 5:

        digitalWrite(ledPin0, LOW);
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, HIGH);
        break;

      case 6:

        digitalWrite(ledPin0, HIGH);
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, HIGH);
        break;

      case 7:

        digitalWrite(ledPin0, LOW);
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, HIGH);
        break;

      case 8:

        digitalWrite(ledPin0, HIGH);
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, LOW);
        break;

      case 9:

        digitalWrite(ledPin0, LOW);
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, LOW);
        break;

      case 10:

        digitalWrite(ledPin0, HIGH);
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, LOW);
        break;

      case 11:

        digitalWrite(ledPin0, LOW);
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, LOW);
        break;

      case 12:

        digitalWrite(ledPin0, HIGH);
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, LOW);
        break;

      case 13:

        digitalWrite(ledPin0, LOW);
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, LOW);
        break;

      case 14:

        digitalWrite(ledPin0, HIGH);
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, LOW);
        break;

      case 15:

        digitalWrite(ledPin0, LOW);
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, LOW);
        break;
    }
  } else {
    //
    // button is released
    //
    buttonState = false;
  }
}
}
  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
   switch (buttonPushCounter) {

Where exactly is this modulo of which you speak?

Yes learn how to use arrays

http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

A shorter version that counts without restrictions and shadows the lowest 4 bits to the leds like the original.

const byte buttonPin = A1;
const byte ledPins[] = { 13, 12, 11, 10 };

int buttonPushCounter = 0;
bool buttonState = 0;
bool lastButtonState = 1;

void setup() {
  pinMode(buttonPin, INPUT);
  for (auto x : ledPins) {
    pinMode(x, OUTPUT);
  }
  Serial.begin(250000);
}
void loop() {
  // read the pushbutton input pin:
  buttonState = !digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);

    lastButtonState = buttonState;
    for (byte i = 0; i < sizeof(ledPins); i++) {
      digitalWrite(ledPins[i], (buttonPushCounter & (1 << i)) == 0);
    }
  } else {
    // button is released
    buttonState = false;
  }
}

Grumpy_Mike:
Yes learn how to use arrays

Arrays

Thanks alot but if I look at the serial monitor it only gives ???? how can I fix this?

f I look at the serial monitor it only gives ???? how can I fix this?

Try again I don't know under what conditions you get that.

If it is new or modified code then post it. You are probably not doing the code correctly.

  Serial.begin(250000);

I changed the baudrate to be less sluggish.

In my current state of ignorance I'd attempt direct port manipulation and get it done in one swell foop. Something on the order of:

portn = (portn & 0xf0) | (~buttonPushCounter & 0xf)

would eliminate the need for the switch or looping.

I think.

dougp:
In my current state of ignorance I'd attempt direct port manipulation and get it done in one swell foop. Something on the order of:

portn = (portn & 0xf0) | (~buttonPushCounter & 0xf)

would eliminate the need for the switch or looping.

I think.

Yes, but it forces you to use only the pins that are assigned to adjacent bits in the port.

aarg:
Yes, but it forces you to use only the pins that are assigned to adjacent bits in the port.

No, it wouldn't work for the general case.

If the pins are not adjacent however, could one not do almost as well with:

digitalWrite(ledPin0, !(value & 0x1);
digitalWrite(ledPin1, !(value & 0x2);
digitalWrite(ledPin2, !(value & 0x4);
digitalWrite(ledPin3, !(value & 0x8);

More memory consumed but less obscure.

What's wrong with my answer from the previous thread you opened on the exact same topic?

It's really not going to be any shorter than that!