Thursday, August 25, 2011

Task 12 - byte LSB test function

/*
 * Project:    ByteLSBtest_621task12
 * Author:     Jane-Maree Howard
 * Date:       Wednesday 21/09/2011; modified Saturday 08/10/2011
 * Platform:   Arduino 22
 * Purpose:    Function to say "yes" if the LSB (least significant bit) is a 1
 -              or "no" otherwise. Tested in the main loop.
 * Operation:  Description:  inputs keypress via Serial comm link;
 -                           A function taking the keypress integer as parameter
 -                           logically ANDs it with binary number B01,
 -                           producing a number which is equal to
 -                           either '1' or '0', & prints "yes" or "no" accordingly
 -             Arduino Library: LIB04_KeypressInput; LIB03_BinaryAND;
 -             Declare: Function LSBtest(bByte); (int)iKeyPress; (byte) bInbyte;
 -             Setup(): Serial comms @ 9600 baud; headings;           
 -             Loop():  input keypress; test LSB & print 'yes'/'no' result                 
 */
int     iKeyPress = 0;       // for incoming serial data 
byte    bInbyte;           // function parameter

void setup()  
{
  Serial.begin(9600);      // Serial comms @ 9600 baud
  // print headings
  Serial.println("KEY-PRESS\tBIN\tYES/NO");    
}//end setup()

void loop()                    

  // send data only when you receive data
  iKeyPress = KeypressInput();      // LIB04_KeypressInput - see below
  if (iKeyPress !=0)
  {
    Serial.print(iKeyPress, BYTE);   // print as ASCII character
    Serial.print("\t\t");                    // print tab      
    Serial.print(iKeyPress, BIN);     // print as BINary
    Serial.print("\t");                      // print tab
    bInbyte  = iKeyPress;
    LSBtest(bInbyte);                      // test LSB 
  }//if()
}//end loop()

// tests parameter's LSB for '1'/'0' & outputs "yes"/"no"
void LSBtest(byte bByte)
{
  // test LSB
  bByte  = BinaryAND(bByte, B01);  //
LIB03_BinaryAND - see below
  // there are only 2 possible values
  if (bByte == 0)
    Serial.println("NO");
  else
    Serial.println("YES");
}//LSBtest()
//END




self-explanatory..

(bit fuzzy tho')















/*
 * Project:    LIB03_BinaryAND
 * Author:     Jane-Maree Howard
 * Date:         Friday 07/10/2011
 * Platform:   Arduino 22
 * Purpose:    To make a library function for bitwise ANDing 2 bytes
 -             Declare:     NONE - MUST BE DECLARED IN CONJOINING SKETCH(ES)
 -             Setup():     NONE     
 -             Loop():      NONE       
 */
byte BinaryAND(byte bB1, byte bB2)
{
  /*  bitwise ANDs 2 Bytes together & returns a separate Byte
  -   CALLED IN MAIN SKETCH  */
  byte bOutByte  = bB1 & bB2;
  return bOutByte;
}//BinaryAdd()
//END

/*
 * Project:    LIB04_KeypressInput
 * Author:     Jane-Maree Howard
 * Date:         Saturday 08/10/2011
 * Platform:   Arduino 22
 * Purpose:    To make a library function for inputting a key-press via Serial comm link  

 -             Declare:     NONE - MUST BE DECLARED IN CONJOINING SKETCH(ES)
 -             Setup():     NONE     
 -             Loop():      NONE    
         
 */
int KeypressInput()
{
  /*  inputs a keypress via the Serial comms link
  -    & returns a non-zero Integer only if there is an input
  -    CALLED IN MAIN SKETCH  */
  int iKey = 0;
  // send data only when you receive data
  if (Serial.available() > 0)
    iKey = Serial.read();
  return iKey;
}//KeypressInput()
//END

No comments:

Post a Comment