spatula:
Guess what the ASCII value of '9' is? 57. Insum=firstKey+secondKey;
you are adding the characters ('0'..'9'), not the numbers (0..9).
You are right.
I managed to do the code for addition.
#include <Keypad.h>
int ledPin[12]={13,12,11,10,9,8,7,6,5,4,3,2};
int number1=0;
int number2=0;
int keyPressed=0;
int counter=0;
int sum=0;
char firstKey;
char secondKey;
long previousMillis = 0;
long interval = 1000;
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {31, 33, 35, 37}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {39, 41, 43}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
for(counter=0; counter<12; counter++)
{
pinMode(ledPin[counter], OUTPUT); // sets the digital pins as output
}
}
void loop(){
char key = customKeypad.getKey();
/*if (key) {
Serial.println(key);
}*/
if (key) { // blocking from anything not needed
keyPressed= keyPressed+1;
if (key != '*' && key !='#'){
// number =key - '0' ; // convert key to its value
} // end of sort
if (keyPressed ==1){
number1= key - '0';
firstKey=key;
}
else if ( keyPressed==2){
number2= key - '0';
secondKey=key;
sum=(number1 *10)+number2;
Serial.println(sum);
//Serial.println(number);
if (sum){
char key = customKeypad.getKey();
// I'm not sure about this loop
if (key == '#') // increment binary counter
{
Serial.println("i"+firstKey);
//digitalWrite(13, HIGH);
//delay (1000);
for(counter=0; counter< sum; counter++){
//digitalWrite(ledPin[counter-1], HIGH);
digitalWrite(ledPin[counter], HIGH);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
} // end of millis
} // end of increment counter
} // end of '#'
else if (key == '*') // decrement binary counter
{
for(counter; counter < sum; counter-- ){
if (counter < 0) counter = 0; // checks to see if
// counter does past zero then set it back to zero
digitalWrite(ledPin[counter], LOW);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
} // end of millis
} // end of decrement counter
} // end of '*'
} // end of sum
keyPressed=0;
} // end of keycount
} //end of if(key)
} // end of loop
Now, when I press two numbers I get the right sum on the serial monitor.
However, if I press '*' I get minus 6. Similarly, for '#' I get minus 13.
How do I convert the third key to either increase the decrease the counting?
At the moment my LEDs don't do anything.
I also have to convert the input to binary counting.
How do I do it?
Regards