simple program that i can't seem to make

sorry, i’m new to this, but i am about to put my head through a wall here.

i have a bunch of 7 segment display stuff already working. the number that shows up on the display is just a variable (temp)

the only thing i want to do is have two buttons: one to increase the number, one to decrease it. code to make sure it doesn’t go below 1.

how do you do this? everything i try just breaks, i can’t set the var in the loop or the whole thing is pointless, but it has to exist to start with. dammit :-/

void loop(int temp){

  if ( digitalRead(upbutton) == HIGH ) { temp = (temp + 1); }
  if ( digitalRead(downbutton) == HIGH  ) { temp = (temp - 1); }
  else { 
     if(temp >= 1 && temp <= 128) { temp = temp; } 
     else { temp = 1; }
  }

Okay a few things:
is the loop shown the main loop in the arduino program, because I believe it cannot have parameters. If you need to use a variable inside the loop, make it global, just initialize it outside the setup and loop functions altogether.

Second, your code, once it senses a button press, will increment or decrement the variable As fast as possible, way faster than you can react. You need some type of state change detection, I believe there’s an example on the arduino website.

Third, there is a function called constrain().
The parameters are
constrain(x, a, b)
where x is the input value
a is the lower bound
b is the upper bound.
It will return x if a<x<b
a if x<=a
b if x>=b
That will simplify your code a bit.

i actually don't care if it goes as fast as possible, if it just did anything but give me compiler errors at this point. i can figure out the delays later.

basically i'm trying to make an arduino implementation of the Midi Mouse to try to learn the arduino (surprise, i'm already regretting it, i don't have enough brains for this shit).

basically it's just a box that sends a midi number when press the center button, and you change the number it sends via two up/down buttons. theoretically easy... but in practice turning out to be far more difficult than i expected

snerk,

Please post your whole program. Most likely someone will point out why it won't compile.

For future reference, you might want to say "It won't compile". Based on your statement that "everything I try just breaks" we were assuming that your program wasn't running correctly.

Hang in there. It can be hard to start up at first, but once you get going, it's a lot of fun.

Regards,

-Mike

P.S. Good job of posting using the code button. A lot of beginners don't manage that, and we have to 'splain it to 'em.

well, it does what it needs to now i think, even without really debouncing, but the problem lies in the display more than anything.

i should have 3 individual 7 segment digits each with their own 595 but, instead i have one 595 and i have to cycle through 3 cathodes on PWM pins to get a 3 digit number… so as the number is getting ++'ed it barely shows up as a blink in the display.

the one thing that i can’t figure out is that as the loop progresses, i want the speed of the increments to accellerate up to a certain point… any hints? (i’ve tried doing speed =+ but it just makes everything stop working)

void loop(){
  int speed = 90;

  if( digitalRead(upbutton) == HIGH) { 
    while ( digitalRead(upbutton) == HIGH ) {
      if(fuckface > 127 ) { } else { fuckface++; }
       
      setDisp(fuckface); 
      delay(speed);

      if ( digitalRead(upbutton) == LOW ) { break; }
    } 
  }


  if( digitalRead(downbutton) == HIGH) { 
    while ( digitalRead(downbutton) == HIGH ) {
      if(fuckface < 2) { } else { fuckface--; }
      setTemp(fuckface); 
      delay(speed);

      if ( digitalRead(downbutton) == LOW ) { break; }
    } 
  }


  setDisp(fuckface);
}





 
void setDisp(int temp){

  setDigit(digit3Pin, temp % 10);
  temp /= 10;
  if (temp >= 1){
    setDigit(digit2Pin, temp % 10);
    temp /= 10;
    if (temp >= 1){
      setDigit(digit1Pin, temp % 100);
    }
  }
  
  
}

What do you expect this:

if(crappity smackface > 127

to do?

The "if" here is redundant:

if( digitalRead(upbutton) == HIGH) {
    while ( digitalRead(upbutton) == HIGH ) {

Why don't you post your sketch?

    setDigit(digit1Pin, temp % 100);

for the most significant digit, send temp as it is without % 100

sketch, then:

int latchPin = 8;  //Pin connected to ST_CP of 74HC595 (aka RCLK)
int clockPin = 12;  //Pin connected to SH_CP of 74HC595 (aka SRCLK)
int dataPin = 11;  //Pin connected to DS of 74HC595 (aka SER)

int digit1Pin = 3; 
int digit2Pin = 5;
int digit3Pin = 4;

int upbutton = 6;
int upstate = 0;
int lastupstate = LOW;

int downbutton = 7;
int downstate = 0;
int lastdownstate = LOW;

int pchange = 1;

byte data;
byte dataArray[11];

void setup(){
  pinMode(digit1Pin, OUTPUT);
  pinMode(digit2Pin, OUTPUT);
  pinMode(digit3Pin, OUTPUT);
  pinMode(upbutton, INPUT);
  pinMode(downbutton, INPUT); 
//  pinMode(downbutton, INPUT);

  pinMode(latchPin, OUTPUT);

  Serial.begin(9600);

  //  In binary representation, right most digit is A
  dataArray[0] = B01111011;
  dataArray[1] = B00001001;
  dataArray[2] = B01110101;
  dataArray[3] = B01011101;
  dataArray[4] = B00001111;
  dataArray[5] = B01011110;
  dataArray[6] = B01111110;
  dataArray[7] = B01001001;
  dataArray[8] = B01111111;
  dataArray[9] = B01011111;
  dataArray[10] = B00000000;

// SET PINS HIGH DAMMIT OR EVERYTHING BREAKS

digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);

}

void loop(){
  int speed =290;

  if( digitalRead(upbutton) == HIGH) { 
    while ( digitalRead(upbutton) == HIGH ) {
      if(pchange > 127 ) { } else { pchange++; }
       
      setDisp(pchange); 
      delay(speed);

      if ( digitalRead(upbutton) == LOW ) { break; }
    } 
  }


  if( digitalRead(downbutton) == HIGH) { 
    while ( digitalRead(downbutton) == HIGH ) {
      if(pchange < 2) { } else { pchange--; }
      setDisp(pchange); 
      delay(speed);

      if ( digitalRead(downbutton) == LOW ) { break; }
    } 
  }


  setDisp(pchange);
}





 
void setDisp(int temp){

  setDigit(digit3Pin, temp % 10);
  temp /= 10;
  if (temp >= 1){
    setDigit(digit2Pin, temp % 10);
    temp /= 10;
    if (temp >= 1){
      setDigit(digit1Pin, temp);
    }
  }
  
  
} 


void setDigit(int digitPin, int value){
  
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, dataArray[value]);  
    digitalWrite(latchPin, 1);
    
    digitalWrite(digitPin, LOW);
    delay(1);
    digitalWrite(digitPin, HIGH);  
}



void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first,
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);


  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);


    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 1);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}

hm good point on the if > while thing. this is why forums are helpful =P

i think using the while loop method is an ok way to do it without worrying about debouncing switches and all… now on to the midi part, which hopefully is less disastrous

if( digitalRead(upbutton) == HIGH) {
    while ( digitalRead(upbutton) == HIGH ) {
      if(pchange > 127 ) { } else { pchange++; }

      setDisp(pchange);
      delay(speed);

      [glow]if ( digitalRead(upbutton) == LOW ) { break; }[/glow]
    }
  }

Also redundant.

digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);

You’ve given names to these pins - why not use them?

const byte dataArray[] = {B01111011, B00001001, B01110101,
                          B01011101, B00001111, B01011110,
                          B01111110, B01001001, B01111111,
                          B01011111, B00000000};

Your code simplified and reworked a little -

/* =============================================================================
 * Sketch - Simple.pde
 * -----------------------------------------------------------------------------
 * <http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1276136260>
 */

/* =============================================================================
 * -----------------------------------------------------------------------------
 * Header Files
 */


/* =============================================================================
 * -----------------------------------------------------------------------------
 * Global Constant Data
 */

const int k_pinLatch        =  8;  // connected to ST_CP of 74HC595 (aka RCLK)
const int k_pinClock        = 12;  // connected to SH_CP of 74HC595 (aka SRCLK)
const int k_pinData         = 11;  // connected to DS of 74HC595 (aka SER)

const int k_pinDigit1       =  3;
const int k_pinDigit2       =  5;
const int k_pinDigit3       =  4;

const int k_pinButtonUp     =  6;
const int k_pinButtonDown   =  7;

const byte aData[11] = 
{
      B01111011
    , B00001001
    , B01110101
    , B01011101
    , B00001111
    , B01011110
    , B01111110
    , B01001001
    , B01111111
    , B01011111
    , B00000000
};


/* =============================================================================
 * -----------------------------------------------------------------------------
 * Global Variables
 */

int g_number                    =  1;


/* =============================================================================
 * -----------------------------------------------------------------------------
 * This shifts 8 bits out MSB first, on the rising edge of the clock,
 * clock idles low
 */

void shiftOut(int pinData, int pinClock, unsigned char data)
{
    pinMode(pinClock, OUTPUT);
    pinMode(pinData, OUTPUT);
    
    // clear just in case to prepare shift register for bit shifting
    digitalWrite(pinData, 0);
    digitalWrite(pinClock, 0);
    
    int bit_mask    = 0x80;
    do
    {
        digitalWrite(pinClock, LOW);
        
        // Sets the pin according to bit state of 'data' bit at 'bit_mask'
        digitalWrite(pinData, (( data & bit_mask ) ? 1 : 0));
        
        // Register shifts bits on upstroke of clock pin
        digitalWrite(pinClock, 1);
        
        // Zero the data pin after shift to prevent bleed through
        digitalWrite(pinData, HIGH);
    } while ( bit_mask >>= 1 );
    
    // stop shifting
    digitalWrite(pinClock, LOW);
}  


/* =============================================================================
 * -----------------------------------------------------------------------------
 */

void setDigit(int pinDigit, int value)
{
    digitalWrite(k_pinLatch, LOW);

        shiftOut(k_pinData, k_pinClock, aData[value]);

    digitalWrite(k_pinLatch, HIGH);


    digitalWrite(pinDigit, LOW);

    delay(1);

    digitalWrite(pinDigit, HIGH);
}


/* =============================================================================
 * -----------------------------------------------------------------------------
 */

void setDisp(int value)
{
    setDigit(k_pinDigit3, value % 10);
    value /= 10;

    if ( value )
    {
        setDigit(k_pinDigit2, value % 10);
        value /= 10;

        if ( value )
        {
            setDigit(k_pinDigit1, value);
        }
    }
}


/* =============================================================================
 * -----------------------------------------------------------------------------
 */

void loop()
{
    const int k_dtDelay = 290;
    
    while ( HIGH == digitalRead(k_pinButtonUp) )
    {
        if ( g_number < 128 )
        {
            g_number++;
        }
        
        setDisp(g_number);
        delay(k_dtDelay);
    }
    
    
    while ( HIGH == digitalRead(k_pinButtonDown) )
    {
        if ( g_number > 1 )
        {
            g_number--;
        }

        setDisp(g_number);
        delay(k_dtDelay);
    }

    setDisp(g_number);
}


/* =============================================================================
 * -----------------------------------------------------------------------------
 */

void setup()
{
    pinMode(k_pinDigit1, OUTPUT);
    pinMode(k_pinDigit2, OUTPUT);
    pinMode(k_pinDigit3, OUTPUT);
    
    pinMode(k_pinButtonUp, INPUT);
    pinMode(k_pinButtonDown, INPUT);
    
    pinMode(k_pinLatch, OUTPUT);
    
    Serial.begin(9600);
    
    digitalWrite(k_pinDigit1, HIGH);
    digitalWrite(k_pinDigit2, HIGH);
    digitalWrite(k_pinDigit3, HIGH);
}

Even simpler would be to give "g_number" local static scope.

dtDelay might be better as a "const" too.

Even simpler would be to give "g_number" local static scope.

I thought about that but beginning programmers find it confusing so I thought I'd leave it as was.

dtDelay might be better as a "const" too.

I thought I had done that but apparently lost it!

Thanks, changed above!