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 :-/
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
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)
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