Super-simple programming question...

Ok, I have a working code that is far more complicated than the question I am about to ask, so I apologize for my utter noobness...

I have 4 push buttons. One to increment my counter up by 10, one to increment up by 1, one for down 1 and one for down 10. This code displays the counter value great on the LCD:

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 2, 3, 4, 5);
const int upTenButtonPin = 9;
const int upOneButtonPin = 10;
const int downOneButtonPin = 11;
const int downTenButtonPin = 12;
int buttonPushCounter = 0;
int buttonState = 0;
int lastUpTenButtonState = 0;
int lastUpOneButtonState = 0;
int lastDownOneButtonState = 0;
int lastDownTenButtonState = 0;

void setup() {

  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Button Pushes:");


}

void loop() {

  pinMode(upTenButtonPin, INPUT);
  pinMode (upOneButtonPin, INPUT);
  pinMode(downOneButtonPin, INPUT);
  pinMode(downTenButtonPin, INPUT);
  
  int upTenButtonState = digitalRead(upTenButtonPin);
  if (upTenButtonState != lastUpTenButtonState) {
    if (upTenButtonState == HIGH) {
      buttonPushCounter += 10;
    }
   }
    lastUpTenButtonState = upTenButtonState;
  
  
  int upOneButtonState = digitalRead(upOneButtonPin);
  if (upOneButtonState != lastUpOneButtonState) {
    if (upOneButtonState == HIGH) {
      buttonPushCounter ++;
    }
   }
    lastUpOneButtonState = upOneButtonState; 
     
  
  int downOneButtonState = digitalRead(downOneButtonPin);
  if (downOneButtonState != lastDownOneButtonState) {
    if (downOneButtonState == HIGH) {
      buttonPushCounter --;
    }
   }
    lastDownOneButtonState = downOneButtonState;
  
    
  int downTenButtonState = digitalRead(downTenButtonPin);
  if (downTenButtonState != lastDownTenButtonState) {
    if (downTenButtonState == HIGH) {
      buttonPushCounter -= 10;
    }
   }
    lastDownTenButtonState = downTenButtonState;

  delay(100);
 
  pinMode(upTenButtonPin, OUTPUT);
  pinMode(upOneButtonPin, OUTPUT);
  pinMode(downOneButtonPin, OUTPUT);
  pinMode(downTenButtonPin, OUTPUT);
    
  lcd.setCursor(0, 1);
  lcd.print(buttonPushCounter, DEC);
}

However, I now would like to take this counter value and output it to pins D2 - D9 as an 8-bit binary number for an ADC (R-2R ladder setup). What do I do in order to "print" the counter value to the 8 pins?

Any help is appreciated!

You could look at "bitWrite" in the Reference.

However, I now would like to take this counter value and output it to pins D2 - D9

Are the D2-D9 pins not allready used in your sketch? LCD etc?

What do I do in order to "print" the counter value to the 8 pins?

Consider a shiftOut and a 74HC595 , uses less pins - http://www.arduino.cc/en/Tutorial/ShiftOut -

Furthermore you should add this line at the end:
buttonPushCounter = constrain(buttonPushCounter, 0, 255); // adjust numbers if needed
to keep its value within defined range for the ADC

  int upTenButtonState = digitalRead(upTenButtonPin);
  if (upTenButtonState != lastUpTenButtonState) {
    if (upTenButtonState == HIGH) {
      buttonPushCounter += 10;
    }
   }
    lastUpTenButtonState = upTenButtonState;
  
  int upOneButtonState = digitalRead(upOneButtonPin);
  if (upOneButtonState != lastUpOneButtonState) {
    if (upOneButtonState == HIGH) {
      buttonPushCounter ++;
    }
   }
    lastUpOneButtonState = upOneButtonState; 
     
  int downOneButtonState = digitalRead(downOneButtonPin);
  if (downOneButtonState != lastDownOneButtonState) {
    if (downOneButtonState == HIGH) {
      buttonPushCounter --;
    }
   }
    lastDownOneButtonState = downOneButtonState;
  
    
  int downTenButtonState = digitalRead(downTenButtonPin);
  if (downTenButtonState != lastDownTenButtonState) {
    if (downTenButtonState == HIGH) {
      buttonPushCounter -= 10;
    }
   }
    lastDownTenButtonState = downTenButtonState;

can be shortened

  • reuse the var to hold the read value
  • that var can be an uint8_t (one byte as it only holds 0 or 1)
  • merge the if statements with AND &&
  • make the compares easier (compare with a constant, esp LOW==0 is easier)
  uint8_t button = digitalRead(upTenButtonPin);
  if (button == HIGH && lastUpTenButtonState == LOW) buttonPushCounter += 10;
  lastUpTenButtonState = button ;

  button = digitalRead(upOneButtonPin);
  if (button == HIGH && lastUpOneButtonState == LOW ) buttonPushCounter ++;
  lastUpOneButtonState = button; 

  button = digitalRead(downTenButtonPin);
  if (button == HIGH && lastDownTenButtonState == LOW) buttonPushCounter -= 10;
  lastUpTenButtonState = button ;

  button = digitalRead(downOneButtonPin);
  if (button == HIGH && lastDownOneButtonState == LOW ) buttonPushCounter--;
  lastUpOneButtonState = button;