Capacitive touch code help

My other post in another forum area never got any replies, so I will try it here.

I am working on a project where I am using a Leonardo for detecting touch as a keyboard input in order to trigger a video on my computer.

How can this code be modified so that when I touch a piece of metal, a letter is registered on my computer?

I am referencing this code for the leonardo.

Thank you for your help!

How can this code be modified so that when I touch a piece of metal, a letter is registered on my computer?

What is the problem? Reading the sensor? Generating a random number to define the letter to send? Using Keyboard.press() and Keyboard.release()?

What have YOU tried?

I currently have the Demo sketch (not the threshold sketch ) running on my Leonardo connected to a copper sheet with a 1m ohm resistor.
http://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense

I can see a big jump in the serial monitor outputs over 10k when i touch copper and when i dont touch it it reads between 200 - 300.

I am a big time beginner at this.

I need a key press sent to my computer as if acting like a keyboard when I touch the copper. I guess it would be the same if the serial monitor is over 10k then print F? I'm not sure if that is the correct phrasing.

Thank you for the reply!

chuco61:
I guess it would be the same if the serial monitor is over 10k then print F?

A common mistake.

What you need is to send an F when the input changes from low to high (or high to low, depending on when you want the keypress to register). To do this, you need to "remember" what the input was last time you looked at it.

Have a look here Gammon Forum : Electronics : Microprocessors : Switches tutorial at the section starting "detecting transitions".

Thanks, i'll have a look.

Is it similar to this code that I have used for another project? Here i used the Leonardo and arcade buttons. This I can read and understand for the most part. I am just having difficulty merging the capacitive sensor and the keyboard. :confused:

void setup() {
  // make pin 2 an input and turn on the
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  //if the button is pressed
  if(digitalRead(2)==LOW){
    //Send an ASCII '1',
    Keyboard.press('1');
    delay(125);
    Keyboard.release('1');
    delay(100);
  }  
  //if the button is pressed
  if(digitalRead(4)==LOW){
    //Send an ASCII '3',
    Keyboard.press('3');
    delay(125);
    Keyboard.release('3');
    delay(100);
  }
  //if the button is pressed
  if(digitalRead(6)==LOW){
    //Send an ASCII '4',
    Keyboard.press('4');
    delay(125);
    Keyboard.release('4');
    delay(100);
  }
  //if the button is pressed
  if(digitalRead(8)==LOW){
    //Send an ASCII '6',
    Keyboard.press('6');
    delay(125);
    Keyboard.release('6');
    delay(100);
  }
  //if the button is pressed
  if(digitalRead(10)==LOW){
    //Send an ASCII '7',
    Keyboard.press('7');
    delay(125);
    Keyboard.release('7');
    delay(100);
  }
  //if the button is pressed
  if(digitalRead(12)==LOW){
    //Send an ASCII '9',
    Keyboard.press('9');
    delay(125);
    Keyboard.release('9');
    delay(100);
  }
}

I am just having difficulty merging the capacitive sensor and the keyboard.

Because you are not trying. You've made NO attempt that we can see.

You have a current value. You need to have a previous value. That is dirt simple - two lines of code - one to define the variable and one to set it to the current value at the end of loop().

You need to compare the current value to to the previous value, to see if the change is significant. That is dirt simple - one line of code.

You need to take some action if the change is significant. That's two more lines for the curly braces after the if statement and up to three lines in the braces to press, delay, and release the key.