Pages: [1]   Go Down
Author Topic: Super-simple programming question...  (Read 459 times)
0 Members and 1 Guest are viewing this topic.
Tempe, AZ
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26638
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could look at "bitWrite" in the Reference.
Logged

"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.

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 228
Posts: 14055
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 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:
 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;
« Last Edit: August 12, 2011, 06:29:51 am by robtillaart » Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: