initiating IF statement with a keystroke to control an LED


I am very new to Arduino but am trying to build a simple LED based fluorometer. As far as hardware goes, i have a simple photodiode (to be replaced with a more sophisticated one once i figure out what i am doing) and a high powered Cree LED controlled through a BuckPuck driver. I am collecting data and writing it to the serial port just about as fast as it can be done. What i would like to do is make an if/else statement that would initiate when i press the (S) key on my key board. This if statement would turn the LED on for a set amount of time and then promptly turn it back off. Does this seem possible? What kind of code structure or syntax do i need? I attempted to use the 'keyboard.begin' and '' functions but i got an error message saying they were only available on a different Arduino board (mine is the standard Arduino UNO). I would very much appreciate any and all help i can get. Thanks in advance for your time.


Kenneth D. Hoadley Student at the University of Delaware

Arduino can't directly communicate with your keyboard. It can communicate via the serial port. You can use the Serial Monitor app in the IDE to send an 'S'. here is how I would look for the S and control LED

void setup() {
  Serial.begin(9600);  //Make sure serial monitor baud rate same
void loop() {
  if(Serial.avalable() > 0) {
    char isItS =;
    if(isItS == 'S') {
       //turn on LED
      // delay a bit  //I use delay here cause its simple,  blink without delay way better 
     //turn off LED

Here's (an extract from) some code I use to drive motors based on keypresses....

char incomingByte;   // for incoming serial data

void setup() 

void loop() 
   if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte =;

                // say what you got:
                Serial.print("I received: ");       
                Serial.println(incomingByte);    // just to prove it's being received
         if (incomingByte == 'S')
          //  do stuff;

I you decide you need other actions on other keys, then just add else ifs:

else if (incomingByte == 'X')
          // do other stuff

You need to use Serial communication to communicate with the Arduino through your computer. The easiest way to do this is via your Serial Monitor but, if you want to avoid pressing the send button (and/or hitting the 'Enter' key) every time you want to send something to the Arduino; try using Processing (available at to automatically detect your key stroke and send it to the Arduino.

Then, in your Arduino code you can use the code snippets others have suggested as replies to this post; to do various things.

If you are new to Serial Communication via processing check out a tutorial at my blog at :

You can also use any terminal emulator program. I use PuTTY where you can set it to be connected to the COM port. Instant response character by character each way.

Otherwise the good ting about Processing is that you can make (realtivly) easy a graphic window where pressing keys, moving the mouse etc sends specific characters to the Arduino and likewise make any graphic display (echo characters, display a shape, change colours) in reaction on receiving characters from the Arduino. The minus is that Processing is Java-based, Arduino is C+±based and the two are maddningly similar on most syntax and widly different in some points - one gets a bit confused at times :~

Thanks for the quick response! Looks like i have a few options to try out. Thanks again!