multiple buttons on analog input

Hello,

I am a newbie here, I receivecd my Arduino Leonardo short time ago. I started on my first project - multiple buttons on analog input while using analogRead. Now I would like to code Keyboard.press regarding value from analogRead. This is how should it work…if “myvalue” is read, Keyboard.press will occur and will be send to PC. Simply keyboard press is not problem, problem is that my "if paramter is still in loop and I want it run only once and when value comes back to zero (lets say less than 20) I would like the same key to be pressed. And finally I would like to make time delay for keyboard press - it should work like I press the button, it was pressed only for short period of time (20ms), then loop runs and after the time loop the same keyboard key is pressed. Those would be different keyboard keys. Hope I wrote it understandable.

Heres my sketch for now:

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
 
 Keyboard.begin();
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
 
 
  if ( (sensorValue > 318) && (sensorValue < 323) ) { //BUTTON FOR REPEATING KEYBOARD PRESS IF VALUE  0 - NOT WORKING NOW :/
  Keyboard.write(81);
  delay(200);
  Keyboard.release(81);
  delay (200);
  
  }  
  if ( (sensorValue > 693) && (sensorValue < 700) ) { //BUTTON FOR PRESSED KEY, WORKS GREAT :)
  Keyboard.press(119);
  delay(50);
  Keyboard.release(119);
  delay(1);
  
   }
 
 }

Thanks anyone for helping in forward :slight_smile:

You need to keep track of the value you read last time. If the value you read this time is the same as the value you read last time, do nothing. If not, do something.

Thank you for your answer, but I would really appreciate example of code...thank you again

Look at the StateChangeDetection example sketch.

PeterH thank you for your answer, but unfortunately I don´t think this will help me. I understand how to have on/off button od digital input but I have 4 buttons on analog input (one is easy, works as I said before) but the other ones....I got into problems basicly because I am able to write program where in serial window it writes me keyboard letters, but it writes constatly (thats the problem of analog input I suppose) I tried to google all day, but no exact answer, any further help would be appreciated. Anyway again thank you

it was pressed only for short period of time (20ms),

That's humanly impossible.

raschemmel:

it was pressed only for short period of time (20ms),

That's humanly impossible.

I won´t say I ment to write 200ms, but thats not not point.....I am more interested in making analogRead to analoginput with keyboardpress output when pressed key are not repeating....that is my point :wink:

Perhaps I found smething, but today I am out of energy, I will try it tomorrow...it is still digital, but maybe I can get it to work via analog...we will see

http://www.arduino.cc/en/Tutorial/Switch

How about putting some comments in your code to identify the keyboard keys your are pressing ?

This is not that hard. The hard part is understanding what you are trying to say you want to do. If you presented it in a more organized fashion it would be easier to understand. Try formatting the information like a TO DO LIST.
TO DO

  1. monitor analog input
  2. look for button press
    etc.etc. etc.

If you do that and step back and look at it , it should be obvious what you need to do, but because you are not organized you can't figure out what to do. There are plenty of examples on the web of how to do similar things. If you are expecting us to write the code for you then you are in for a surprise. We help you get back on track when you get derailed but you are the locomotive and
you are responsible for the forward motion of the train. Don't expect us to write your code for you.

I don't know how clearly you have this idea in your own mind, but you are not presenting it clearly to us. For example

making analogRead to analoginput with keyboardpress output

probably contains 10 or 20 separate concepts within it. And I can't help feeling that if you take the time to describe what you want in much greater detail there is a strong possibility that you will see a solution yourself. It will certainly help us to help you.

Remember a computer is utterly stupid. You are its brains.

...R

Robin2 and rashemmel: Thank you guys for your support, I really appreciate it!
I read my posts few times and while I was googling all day now I know that I did not present my idea clearly. I apologize for that and now I will try to fix it. So here we go…

TO DO LIST:
monitor analog input (done)

values:
keyboard.press(“l”) - 826 - 831
keyboard.press(“p”) - 947-950
keyboard.press(“b”)- 693 - 700
keyboard.press(“d”) - 318 - 323

button press in code (done)

if ( (sensorValue > 693) && (sensorValue < 700) ) { //BUTTON FOR PRESSED KEY, WORKS GREAT :)
  Keyboard.press(119);
  delay(50);
  Keyboard.release(119);
  delay(1);

make keyboard.press only ONCE (not done)
I do not know how to change “if” parameter when key is pressed how to stop the loop until the value gets to 0 and on that value it will be the key pressed again
Here if you please give me a hand, because I found a lot of digitalRead tutorials for turining led on/off, but no for analogRead, I am really lost now :confused:

Perhaps Keyboard.releaseAll may help?

if ( (sensorValue > 318) && (sensorValue < 323) ) { //BUTTON FOR REPEATING KEYBOARD PRESS IF VALUE  0 - NOT WORKING NOW :/
  Keyboard.write(81);
  delay(200);
  Keyboard.release(81);
  delay (200);
  else
  Keyboard.releaseAll;
}

And finally I will need the time delay between pressing the key and again pressing the same key, but I assume I will may use the code from “making keyboard press only once” ut with delay (please do not focus on this one, I haven´t put a lot of thinking into this, its just an idea for later :slight_smile:

Even if for all of you it may seem easy, for me, who started to coding yesterday all of this makes me crazy and any hand of help will be great…maybe I can revenge all of you if you will need help with hardware faults :wink:

Thank you for reading to this point, I tried to be as clear as I possible

I still want to see comments that tell me what the ascii code represents. I don't want to have to remember what "119" means.

You are only showing small pieces of code so we can't grasp the overall picture. If the whole sketch is very long please write a short sketch (< 30 lines ?) that illustrates the problem you are trying to solve.

I'm probably thick, but I have no idea what "Keyboard.press(119);" is supposed to do.

...R

I'm probably thick, but I have no idea what "Keyboard.press(119);" is supposed to do.

values:
keyboard.press("l") - 826 - 831
keyboard.press("p") - 947-950
keyboard.press("b")- 693 - 700
keyboard.press("d") - 318 - 323

ASCII code for "q"=81
ASCII code for "w"=119

I know what it means only because I googled but I still want the OP to put those comments in because I shouldn't have to remember what "119" means.

I still want to see comments that tell me what the ascii code represents. I don't want to have to remember what "119" means.

I think the problem you want help with is that you want to work out which button was pressed, when you have multiple buttons connected to an analog input via a resistor circuit.

Some circuits enable you to handle the case where multiple buttons are pressed simultaneously. Some don't. From you comment about analogRead ranges, I guess you aren't trying to do that. In that case all you need to do is a sequence of range checks on the value to see which range it falls in, and identify the corresponding button. Relating that button to a character code is a very simple, but completely separate, problem. I suspect you also want some logic to let you work out when the key press occurs. You can do that by recording the previously-pressed button and test whether the currently-pressed button is the same.

What the OP is trying to do is mimic the attached keypad shield code and use that to trigger Keyboard library functions that send characters to the serial port.

The sparkfun link has no useful info but,
If you recognize it as the Linksprite Button Shield (different name , same product)
You will find this link,

http://store.linksprite.com/linksprite-16x2-lcd-keypad-shield-for-arduino-version-a/
If you look closely you'll see a little "Download" link which takes you here,

http://linksprite.com/wiki/index.php5?title=16_X_2_LCD_Keypad_Shield_for_Arduino
Which has a detailed description and code.
(below)

 #include <LiquidCrystal.h>
#include <LCDKeypad.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
char msgs[5][16] = {"Right Key OK ",
                    "Up Key OK    ",               
                    "Down Key OK  ",
                    "Left Key OK  ",
                    "Select Key OK" };
int adc_key_val[5] ={50, 200, 400, 600, 800 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
 
void setup()
{
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("     helle! ");
  lcd.print("      welcome!");
  lcd.setCursor(0,1);
  lcd.print("   LinkSprite");
  lcd.print("    LCD Shield");
  delay(1000);
 
  lcd.setCursor(0,0);
  for (char k=0;k<26;k++)
  {
    lcd.scrollDisplayLeft();
    delay(400);
  }
  lcd.clear();
  lcd.setCursor(0,0); 
  lcd.print("ADC key testing"); 
}
void loop()
{
   adc_key_in = analogRead(0);    // read the value from the sensor 
   key = get_key(adc_key_in);  // convert into key press
   if (key != oldkey)   // if keypress is detected
   {
     delay(50);  // wait for debounce time
     adc_key_in = analogRead(0);    // read the value from the sensor 
     key = get_key(adc_key_in);    // convert into key press
     if (key != oldkey)    
     {   
       lcd.setCursor(0, 1);
       oldkey = key;
       if (key >=0)
       {
           lcd.print(msgs[key]);              
       }
     }
   }
   delay(100);
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
    int k;
    for (k = 0; k < NUM_KEYS; k++)
    {
      if (input < adc_key_val[k])
      {
        return k;
      }
    }   
    if (k >= NUM_KEYS)k = -1;  // No valid key pressed
    return k;
}

Since the OP is a Newbie, he doesn't know how to write the above code which is why he isn't using an array . His code is basically a barebones minimum function version that doesn't drive an lcd. I have no idea what he wants the keyboard characters for.
I am guessing it is some attempt at a menu (which he has not disclosed yet ...)([WITH-HOLDING INFORMATION WILL NOT BE TOLERATED]

lcd_1602_keypad_shield.ino (1.61 KB)

raschemmel:
ASCII code for "q"=81
ASCII code for "w"=119

I know what it means only because I googled but I still want the OP to put those comments in because I shouldn't have to remember what "119" means.

Can avoid the comment or confusion by simply putting the character in.

Keyboard.press( 'w' );

Remember the single quotes though.

Thank you ! (my point exactly)

raschemmel:
Keyboard library functions that send characters to the serial port.

Now I'm even more confused. Why does anyone need a library to send characters to the serial port. What's wrong with Serial.print('w'); ?????

...R

Robin2:
Why does anyone need a library to send characters to the serial port.

I think that the sketch is intended to send simulated keyboard events to the host PC. And as far as I can figure out from this thread, that part is irrelevant to the problem, which is simply to detect key presses on a keypad connected to an analog input via a resistor circuit, and doesn't seem to need anything more complicated than a sequence of range checks on the value returned by analogRead() to detect the currently-pressed key, followed by a comparison with the previously-pressed key.