Well done Carl, your skills are improving, however (there allways is) readkeyboard() reads the keyboard again and by doing so it could read again a value below 25 or so. So instead I propose a function that will interpret the keyboardValue read
Note: Code not compiled or tested
void loop()
{
keyboardValue = analogRead(keyboardPin); // read the keyboard value (0 - 1023)
while (keyboardValue < 25)
{
//do nothing until a key is pressed
keyboardValue = analogRead(keyboardPin);
delay(50);
} //end of do nothing till a key is pressed
int k = kbValue(keyboardValue); // maps the value of the key being pressed "keypressed" i.e. 0-9
Serial.println(k); // print the value back to the Serial view window on your PC
delay(1000);
}
// interpret the keyboard routine
int kbValue(int kbv)
{
keypressed = 1;
if (keyboardValue >=68) keypressed++;
if (keyboardValue >=108) keypressed++;
if (keyboardValue >=153) keypressed++;
if ((keyboardValue >=186) keypressed++;
if ((keyboardValue >=254) keypressed++;
if ((keyboardValue >=361) keypressed++;
if ((keyboardValue >=457) keypressed++;
if ((keyboardValue >=525) keypressed++;
if ((keyboardValue >=621) keypressed++;
if ((keyboardValue >=738) keypressed++;
if ((keyboardValue >=812) keypressed++;
if ((keyboardValue >=854) keypressed++;
if ((keyboardValue >=898) keypressed++;
if ((keyboardValue >=945) keypressed++;
if (keyboardValue >=970) keypressed++;
do
{
delay (100);
keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
} while (keyboardValue > 68); //wait until key no longer being pressed before continuing
return keypressed;
}
Another point of attention is that in readkeyboard you test keyboard value even if keypressed has gotten a value. Replace all the if's with multiple nested if else .. Here a snippet of code how that could be done.
keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
if (keyboardValue < 68){keypressed = 1;}
else if (keyboardValue < 108)) {keypressed = 2;}
else if (keyboardValue < 153)) {keypressed = 3;}
else if (keyboardValue < 186)) {keypressed = 4;}
else if .... etc
The tests are simpler and the number of tests are less. This is caused by the fact that we use earlier tested information. If a value is not smaller than 68 it must be greater or equal so we do not need to test that.