Thursday, August 25, 2011

Task 13 - byte left-shifting function

/*
 * Project:      ByteLeftShift_621task13
 * Author:     Jane-Maree Howard
 * Date:         Wednesday 21/09/2011; modified Saturday 08/10/2011
 * Platform:   Arduino 22
 * Purpose:    To left-shift a byte in the parameter postion two places to the left
 -                   then output the original and the final byte.
 * Operation:  Description: Prints headings; inputs character;
 -             Arduino Libraries: LIB04_KeypressInput (see below);
 -             Declare: Function leftShiftByte(bByte,iShifts); int iKeyPress; byte bInbyte;
 -             Setup():             
 -             Loop():                   
 */
int     iKeyPress = 0;       // for incoming serial data 
byte    bInbyte;
void setup()  
{
  Serial.begin(9600);       // Serial @ 9600 baud
  // print headings
  Serial.println("KEY-PRESS\tBIN\tSHIFTED");  
}//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 tabs
    bInbyte  = iKeyPress;      //
    Serial.print(bInbyte, BIN);          // print resultant byte as binary
    Serial.print("\t");                        // print tab
    bInbyte  = leftShiftByte(bInbyte,2);  // left-shift 2 places &..
    Serial.println(bInbyte, BIN);             // ..print resultant byte as binary
  }//if()  
}//end loop()

// leftShiftByte() - left-shifts parameter byte iShift places
byte leftShiftByte(byte bByte,int iShift)
{
  bByte  = bByte << iShift;
  return bByte;
}//leftShiftByte()
//END



 The left-shifted byte is clear from the screen-print  
 on the right. 


Leading zeros omitted (in the BIN column & the SHIFTED column) .


Below is my Arduino Library function for Serial input.


 /*
 * 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
 * Operation:  Description: inputs a key-press & returns an Integer
 -                    Procedure(): int KeypressInput();  no parameters             
 */
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()


Task-14

1 comment:

  1. my Arduino Library functions are a big help in allowing me to concentrate on the main problem.

    ReplyDelete