Wednesday, August 10, 2011

Task5 - Key Press program: Serial Monitor output - software

/*
 * Project:    keyPressOutput_621task05
 * Author:     Jane-Maree Howard
 * Date:       Wednesday 10/08/2011
 * Platform:   Arduino 22
 * Purpose:    To take a key press
 -                    and output the key and its codes
 -                    in binary, decimal & in hex.
 * Operation:  Description:
 -              Declare:   iKeyPress (int)
 -              Setup():   Serial Port (SP); headings            
 -              Loop():    reads input; prints character & ASCII codes              
 */

int     iKeyPress = 0;      // for incoming serial data

void setup()  
{
  Serial.begin(9600);     // SP @ 9600baud

  // print headings
  Serial.print("KEY-PRESS\t");   
  Serial.print("BIN\t");
  Serial.print("DEC\t"); 
  Serial.println("HEX");    
}//end setup()

void loop()                    
{
  // send data only when you receive data
  if (Serial.available() > 0)
  {
    // read incoming key-press
    iKeyPress = Serial.read();
    // output key-press & its codes
    Serial.print(iKeyPress, BYTE);   // print as ASCII character
    Serial.print("\t\t");                       // print tab   
   
    Serial.print(iKeyPress, BIN);    // print as ASCII-encoded binary
    Serial.print("\t");                        // print tab
   
    Serial.print(iKeyPress, DEC);    // print as ASCII-encoded decimal
    Serial.print("\t");                        // print tab

    Serial.println(iKeyPress, HEX);  // print as ASCII-encoded hexadecimal
  }//if()
 
}//end loop()
//END


This is the Serial Monitor
output - you can see the keys
that have already been outputted,
& the word "Yay!" which was about
to be entered by clicking the
'Send' button on the top right.

No comments:

Post a Comment