I am making a keypad and when I try to save the buttons that were pushed they reset back to zero.
int tmp = 1;
int digit = 1;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int code = 0;
void setup() {
Serial.begin(9600);
pinMode(7, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
}
void loop() {
if (digit > 4) {
digit = 1;
}
for (int i = 2; i <= 7; i++) {
int tmp = digitalRead(i);
if (tmp == HIGH) {
if (digit == 1) {
int a = i - 1;
}
if (digit == 2) {
int b = i - 1;
}
if (digit == 3) {
int c = i - 1;
}
if (digit == 4) {
int d = i - 1;
Serial.print(a);
Serial.print(b);
Serial.print(c);
Serial.print(d);
digitalWrite(8, HIGH);
}
digitalWrite(9, HIGH);
delay(100);
digitalWrite(9, LOW);
delay(100);
Serial.print(i);
digit++;
}
}
}
asked Sep 15, 2017 at 1:14
1 Answer 1
Do you know what int a;
does?
For example you have a global variable a
and then you have local a
. Local one hides global one.
In all cases you're changing local variables. Then you're printing global a
,b
,c
and local d
.
answered Sep 15, 2017 at 6:15
lang-cpp
int total = 0;
) and just add to it with each keypress:total = (total * 10) + i;
- wherei
is the most recent digit input by the user.