Go Down

Topic: Super-simple programming question... (Read 529 times) previous topic - next topic

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:

Code: [Select]

// 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!

AWOL

You could look at "bitWrite" in the Reference.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

robtillaart

#2
Aug 12, 2011, 01:24 pm Last Edit: Aug 12, 2011, 01:29 pm by robtillaart Reason: 1
Quote
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?

Quote
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


Code: [Select]

 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)

Code: [Select]

 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;
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy