Tuesday, August 30, 2011

Project 2 - Arduino Motor Project - Serial input speed & direction control

Here's the Fritzing diagram, showing pins 9 & 10 used since they have the Pulse Width Modulation (PWM) option enabled on them.


/*
 * Project:      MotorSpin_621project02_1
 * Author:      Jane-Maree Howard
 * Date:         Tuesday 11/10/2011
 * Platform:   Arduino 22
 * Purpose:    To demonstrate Serial input motor control
 * Operation:  Description: The ET-MINI DC-MOTOR board has an H-bridge & a DC motor
 -                          The board's terminals are:
 -                          EN - Enable, must be set at logic "1" for operation.
 -                          IN1- With EN @ "1", IN1 @ "1" rotates the motor to the Right.
 -                          IN2- With EN @ "1", IN2 @ "1" rotates the motor to the Left
 -            
Arduino Libraries: LIB04_KeypressInput; LIB06_PWMmotorSpeed_ETMINI;  
 -            
Setup(): Serial @ 9600 baud; Print heading;            
 -            
Loop():  Via Serial Monitpr, input a PAIR of characters, e.g. r7, ss, L9;
 -                      Press 'Enter' or 'Send';
 -                      Motor direction & speed range are outputted &..
 -                      ..PWMmotorControl(bRight, bLeft, bEnable, bMotor, bSpeed) operates
 -                      the motor according to instruction-pair;

 */
int  iKeyPress = 0;      // for incoming serial data
char chInput;              // for 'char' parameters
// digital control pins for ET-MINI DC-MOTOR
byte bRight    = 10;    // IN1 is pin 10 - rotate Right
byte bLeft     = 9;       // IN2 is pin 9  - rotate Left
byte bEnable   = 13;  // EN  is pin 13 - Enable rotation, must = '1'
byte bMotor    = 0;    // in LIB06_PWMmotorControl(), '0'=STOP, '1'=RIGHT, '11'=LEFT
byte bSpeed    = 0;    // in ditto, PWM duty cycle parameter - initialised to "stop"

void setup()  
{
  Serial.begin(9600);                      //SM @ 9600 baud
  pinMode(bEnable, OUTPUT);   // EN - ENable pin on motorboard
  pinMode(bRight, OUTPUT);     // IN1 - 'RIGHT' pin on motorboard
  pinMode(bLeft, OUTPUT);       // IN2 - 'LEFT' pin on motorboard
  Serial.println("\nEnter your control characters in pairs e.g.: R5, ss");
}//end setup()

void loop()                    
{
  // first input Motor Direction, STOP, RIGHT, or LEFT
  iKeyPress = KeypressInput();
  if (iKeyPress !=0)
  {
    Serial.print("\nInput direction\t");
    chInput   = (char)iKeyPress;
    Serial.println(chInput, BYTE);
    switch (chInput)
    {
      case ('s'):
        bMotor    = 0;
        break;
      case ('S'):
        bMotor    = 0;         
        break;
      case ('r'):
        bMotor    = 1;
        break;
      case ('R'):
        bMotor    = 1;
        break;
      case ('l'):
        bMotor    = 11;
        break;
      case ('L'):
        bMotor    = 11;
        break;     
      default: bMotor    = 0;    
    }//switch()

    // now input Motor Speed on a scale of 1-9
    iKeyPress = KeypressInput();
    Serial.print("Input speed\t");
    chInput   = (char)iKeyPress;
    Serial.println(chInput, BYTE);
    switch (chInput)
    {
      case ('1'):
        bSpeed    = 50;
        break;
      case ('2'):
        bSpeed    = 75;
        break;
      case ('3'):
        bSpeed    = 100;
        break;  
      case ('4'):
        bSpeed    = 125;
        break;
      case ('5'):
        bSpeed    = 150;
        break;
      case ('6'):
        bSpeed    = 175;
        break;    
      case ('7'):
        bSpeed    = 200;
        break; 
      case ('8'):
        bSpeed    = 225;
        break;        
      case ('9'):
        bSpeed    = 255;
        break;     
      default: bMotor    = 0;    
    }//switch()
  }//if()
  /* now that control characters have been entered e.g.L7,
     call PWMmotorControl with all parameters present
  */

  PWMmotorControl(bRight, bLeft, bEnable, bMotor, bSpeed);
}//end loop()
//END


The control characters must be entered as a pair,
otherwise it will not function.

The direction can be upper or lower case.

The duty cycle (input speed 1-9) can in theory range from 0-255, but in practice, anything < 50 won't fire the motor up.
255 represents 100%, or full-speed.







/*
 * Project:    LIB06_PWMmotorSpeed_ETMINI
 * Author:       Jane-Maree Howard
 * Date:          Tuesday 11/10/2011
 * Platform:     Arduino 22
 * Purpose:     To use PWM speed-control for an ET-MINI DC-MOTOR board
 * Operation:  The ET-MINI DC-MOTOR board has an H-bridge & a DC motor
 -                          The board's terminals are:
 -                          EN - Enable, must be set at logic "1" for operation.
 -                          IN1- With EN @ "1", IN1 @ "1" rotates the motor to the Right.
 -                          IN2- With EN @ "1", IN2 @ "1" rotates the motor to the Left  
 -             Declare:     NONE - MUST BE DECLARED IN CONJOINING SKETCH(ES)
 -             Setup():     NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
 -                          (used  only in testing)
 -             Procedure(): void PWMmotorControl(byte,byte,byte,byte,byte);     
 -             Loop():      NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
 -                          (used  only in testing)   
         
 */
//   RotateMotor - CALLED IN MAIN SKETCH
void PWMmotorControl(byte bR, byte bL, byte bEN, byte bMO, byte bSPEED)
{
  /* 
  Rotate ET-MINI DC-MOTOR: bR,bL,bEN are Pins; bMO en/dis-ables;
 
  bSPEED takes values 0-255 as part of the PWM duty cycle;
  analogWrite() is a PWM operation on a Digital pin,
  .            
& has nothing to do with Analog pins or AnalogRead()
  */
  // pause motor to allow change of direction

  delay(20);                        // delay 20 milliseconds
  digitalWrite(bEN,1);      // write '1' to motorboard pin EN - enable motor
  switch (bMO)
  {
    case 0:                          // STOP  - disable motor
      digitalWrite(bEN,0);  // write '0' to motorboard pin EN
      break;
    case 1:                          // rotate RIGHT @
      digitalWrite(bL,0);    // write '0' to motorboard pin IN2 - no rotation left
      analogWrite(bR,bSPEED);   // write to motorboard pin IN1
      break;
    case 11:                       // rotate LEFT @ 
      digitalWrite(bR,0);   // write '0' to motorboard pin IN1 - no rotation right
      analogWrite(bL,bSPEED);   // write to motorboard pin IN2   
      break;
    default:                           // STOP - disable motor
      digitalWrite(bEN,0);    // write '0' to motorboard pin EN - disable motor
  }//switch()case
}//PWMmotorControl()
//END


Go to the following link for the operating software
/*
 * 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

.

Project 1 - Arduino Software Investigation - the EEPROM library


 
EEPROM Library


The microcontroller on the Arduino boards has its own EEPROM:


Electrically Erasable Programmable Read Only Memory.


memory whose values are kept when the board is turned off (like a tiny hard drive).

The microcontrollers on the various Arduino boards have different amounts of EEPROM:


1024 bytes on the ATmega328

512 bytes on the ATmega168 and ATmega8, 

4 KB (4096 bytes) on the ATmega1280 and ATmega2560.


This library enables you to read, read(), and write, write(), those bytes.
 

byte EEPROM.read(address)

Description: Reads a byte from the EEPROM.
Locations that have never been written to have the value of 255.
Parameters: address: the location to read from, from 0 to 511 (int)
Returns: the value stored in that location (byte)

EEPROM.write(address, value)

Description: Writes a byte to the EEPROM.
Parameters:
address
: the location to write to, from 0 to 511 (int)
value
: the value to write, from 0 to 255 (byte)
Returns: none



The Atmega 168 datasheet says EEPROM memory has a specified life of 100000 write/erase cycles,
so there is a limit to how many times you can write information to that memory space.

Bear this in mind for long-lived projects or fast-moving data.
The datasheet also specifies that a write cycle takes 3.3 ms to complete.
Other EEPROM write and read requests will fail if executed in this time period.
This delay appears to be built into the EEPROM library,
as a casual test shows each cycle taking 3.33 ms to execute.
Hence, you do not specifically need to add a delay to an EEPROM write;
just be aware of the built-in time delay.

Below (pp 2,3) is some example code, demonstrating both Read & Write functions.



References
Arduino EEPROM Library. Retrieved from http://arduino.cc/en/Reference/EEPROM

Example

/*
 * Project:    EEPROM_ReadWrite_621project01
 * Author:     Jane-Maree Howard
 * Date:       Friday 0/08/2011
 * Platform:   Arduino 22
 * Purpose:    To demonstrate EEPROM Read & Write functions via the Serial Monitor (SM)
 * Operation:  Description:
               Include: EEPROM.h library
               Declare: EEPROM memory size; value read from EEPROM
               Functions: PrintSM(int,int) ReadEEPROM(int); EEPROM.write(int,int); 
               Setup(): SM @ 9600baud;  write to EEPROM            
               Loop():  read from EEPROM 
 * Comment:    an integer written to EEPROM will return its LSByte!
 */
#include  

//int  iMemSize  = 512; // ATmega168
int  iMemSize  = 1024;  // ATmega328
int iValue;             // value read from EEPROM

void setup()   
{
  Serial.begin(9600);  //SM @ 9600baud
  //now write to EEPROM
  for (int j=0; j
  {
    Serial.print("Writing to\t");
    PrintSM(j,j);
    WriteEEPROM(j,j);
  }//for   
}//end setup()

void loop()                     
{
  //now read EEPROM  
  for (int j=0; j
  {
    Serial.print("Reading from\t");
    iValue  = ReadEEPROM(j);
    PrintSM(j,iValue);
    delay(250);
  }//for
}//end loop()

/* PrintSM() - prints parameters to SM */
void PrintSM(int iAddr,int iVal)
{
  Serial.print(iAddr);
  Serial.print("\t");
  Serial.print(iVal);
  Serial.println();
}//printSM()

// below are the two feature functions, READ & WRITE

/* ReadEEPROM() - returns a byte from the parameter address */
int ReadEEPROM(int iAddr)
{
  // this is the READ function
  int iVal  = EEPROM.read(iAddr);
  // don't go beyond memory size
  if (iAddr >= iMemSize)
    iAddr  = 0;
  return iVal;
}//ReadEEPROM()


/* WriteEEPROM() - writes a parameter byte to the parameter address */
void WriteEEPROM(int iAddr,int iVal)
{
  // this is the WRITE function
  EEPROM.write(iAddr,iVal); 
  // don't go beyond memory size
  if (iAddr >= iMemSize)
    iAddr  = 0; 
}//WriteEEPROM()

//END


Task 19 - Arduino motor mini-board spinning both directions

The Hardware & Software for this task are practically identical.

Only the software features a minor difference, namely, in the line: 

 rotateMotor(0,1,1,3);

This rotates the motor Left for 3 seconds. 

Task 18 - Arduino motor mini-board spinning one direction

Above is a Fritzing diagram for controlling the ET-MINI DC-MOTOR board.

EN must be at logic "1" to enable the motor function.
IN1 at logic "1" (with IN2 at logic "0") rotates the motor to the Right.
IN2 at logic "1" (with IN1 at logic "0") rotates the motor to the Left.

To rotate Right, set EN & IN1 @ logic "1" & IN2 @ "0".
On either side (near the top, see board) are indicator LEDs.

 Below are some pictures of the actual board:


Terminal block showing IN1, EN, & IN2




ET-MINI DC-MOTOR
  










Also shown are +Vcc, GND (obscured)
& two other terminals to be used later.

The software is posted separately.

Task 17 - soft-serial input, Rx, subroutine

Task 16 - Inter-Arduino soft-serial communication

Thursday, August 25, 2011

Task 15 - Byte output using RS232 soft-serial protocol

Arduino RS232 soft-serial protocol

Task 14 - Arbitrary byte serial monitor output bit-by-bit

/*
 * Project:    bitwiseByteOutput_task14
 * Author:     Jane-Maree Howard
 * Date:         Wednesday 24/08/2011; modified Sunday 09/10/2011
 * Platform:   Arduino 22
 * Purpose:    To output the bits, one by one, to the
  =                  serial monitor(SM) screen from an arbitrary byte.
 * Operation:  Inputs an arbitrary byte, which is right-shifted 0 to 7 places
 -             successively, bit-wise ANDed with value B01, & each resultant bit is
 -             separately outputted to the SM
 -             Arduino Libraries: LIB04_KeypressInput; LIB05_BitSelect;
 -             Declare: Function bitwiseByte(bByte,iShifts); int iKeyPress; byte bInbyte;
 -             Setup(): Serial @ 9600 baud; headings             
 -             Loop():  Input keypress as arbitrary byte; output byte as BINary;
 -                      output each bit separately
 */
int     iKeyPress = 0;       // for incoming serial data 
byte    bInbyte;
void setup()  
{
  Serial.begin(9600);        // Serial @ 9600 baud
  // print headings
  Serial.println("KEY-PRESS\tBIN\tLSB..................MSB");  
}//end setup()

void loop()                    

  // send data only when you receive data
  iKeyPress = KeypressInput();       // LIB04_KeypressInput
  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
    for (int j=0; j<8; j++)                 // now select each bit separately
    {
      bInbyte  = iKeyPress;
      bInbyte  = BitSelect(bInbyte, j);     // LIB05_BitSelect
      Serial.print(bInbyte, BIN);             // ..print each bit..
      Serial.print("  ");                           // ..& 2 spaces
    }//for()
    Serial.println(); 
  }//if()  
}//end loop()
//END


As can be seen, the BIN column entries
are 'mirror-imaged' by the block labelled LSB............MSB since we start with the Least Significant Bit.  Leading zeroes omitted in the BIN column give the output a slightly unbalanced look.

 LIB04_KeypressInput
 see Task 13

 /*
 * Project:    LIB05_BitSelect
 * Author:     Jane-Maree Howard
 * Date:         Sunday 09/10/2011
 * Platform:   Arduino 22
 * Purpose:    To make a library    function for selecting a given bit         from an arbitrary byte
 * Operation:  Bitwise ANDs arbitrary byte with value B01
 -                    after being right-shifted a given number of places
 -             Declare:     NONE - MUST BE DECLARED IN CONJOINING SKETCH(ES)
 -             Setup():     NONE
 -             Procedure(): byte BitSelect(byte, byte);     
 -             Loop():      NONE          
 */
byte BitSelect(byte bByte, byte bShift)
{
  /*  bitwise ANDs parameter bByte with value B01
      after being right-shifted bShift places
      & returns a separate Byte
      CALLED IN MAIN SKETCH  */
  bByte      = bByte >> bShift;
  bByte      = bByte & B01;
  return bByte;
}//BitSelect()
//END

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

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

Task 11 - bit-ANDing function

/*
 * Project:    BinaryANDing_621task11
 * Author:     Jane-Maree Howard
 * Date:         Wednesday 21/09/2011; modified Friday 07/10/2011
 * Platform:   Arduino 22
 * Purpose:    To demonstrate a function to bitwise AND a byte with B01
 -                   and output the result to the screen.
 -                   Tests in the main loop().
 * Operation: Inputs a byte via Serial comm & 

 -                   function taking a byte as parameter bitwise-ANDs it
 -                   with value B01
 -         Arduino Library: LIB03_BinaryAND - function BinaryAND(byte,byte)

 -                                    LIB04_KeypressInput  
 -             Declare:          byte bInbyte; int iKeyPress;
 -             Setup():          Serial comms @ 9600 baud; print heading              
 -             Loop():           Input a byte via Serial comm & output to SM as BIN;

 -                                   bitwise-AND byte with value B01 & output result to SM.                
 */
int     iKeyPress = 0;     // for incoming serial data 
byte    bInbyte;

void setup()  
{
  Serial.begin(9600);      //Serial @ 9600baud
  // print headings
  Serial.println("KEY-PRESS  BIN\t    AND b01"); 
}//end setup()

void loop()                    

  // send data only when you receive data
  iKeyPress = KeypressInput();       // LIB04_KeypressInput
  if (iKeyPress !=0)
  {
    Serial.print(iKeyPress, BYTE);   // print as ASCII character
    Serial.print("\t   ");           // print tabs
    bInbyte  = iKeyPress;            //
    Serial.print(bInbyte, BIN);      // print resultant byte as binary
    Serial.print("  ");              // print tab
    bInbyte  = BinaryAND(bInbyte, B01);   // LIB03_BinaryAND
    Serial.println(bInbyte, BIN);         // ..print resultant byte as binary
   }//if()  
}//end loop()
//END


At right is the Serial Monitor output showing what happens to a byte that is bitwise-ANDed with the value B01.

Only the LSB appears, as leading zeroes are ignored.



/*
 * 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
 * Operation: bitwise ANDs 2 parameter bytes & returns a third byte
 -              Declare:     NONE - MUST BE DECLARED IN CONJOINING SKETCH(ES)
 -              Setup():     NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
 -                              (used  only in testing)
 -              Procedure(): byte BinaryAND(byte, byte);     
 -              Loop():      NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
 -                              (used  only in testing)            
 */
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
 * Operation:  inputs a key-press & returns an Integer
 -             Declare:     NONE - MUST BE DECLARED IN CONJOINING SKETCH(ES)
 -             Setup():     NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
 -                               (used  only in testing)
 -             Procedure(): int KeypressInput();  no parameters   
 -             Loop():      NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
 -                              (used  only in testing)            
 */
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
 

Task 10 - Binary Adding function

/*
 * Project:    BinaryAdding_621task10
 * Author:     Jane-Maree Howard
 * Date:       Tuesday 30/08/2011
 * Platform:   Arduino 22
 * Purpose:    To demonstrate a function to add B0111 to a byte passed to it.
 -             Called 256 times in loop() using 0-255 as the parameters.
 * Operation:  Description: A function taking a byte as parameter adds a specific
 -                          binary number to it & returns a byte
 -             Declare: Function binaryAdd(bByte); bThisByte
 -             Setup(): Serial port @ 9600 baud; print bThisByte.
 -             Loop():  Adds byte B0111 to given byte 256 times                 
 */
byte  bThisByte  = B01011010;
byte  bStoreByte;
void setup()  
{
  Serial.begin(9600);  //SM @ 9600baud
  Serial.print("\nByte = \t");
  Serial.println(bThisByte, BIN);
  Serial.println();
}//end setup()

void loop()                    
{
  bStoreByte   = bThisByte;    // stores byte being operated on..
  for (int j=0; j<256; j++)
  {
    bThisByte  =+ BinaryAdd(bThisByte);
    Serial.print("Byte = \t");
    Serial.println(bThisByte, BIN);
    delay(100);
  }//for()
  Serial.print("\nStored: ");
  Serial.print(bStoreByte, BIN);
  Serial.println(",\t+ the 256 additions.\n");
  delay(2000);
}//end loop()

// BinaryAdd() - adds B00000111 to parameter
byte BinaryAdd(byte bByte)
{
  bByte = bByte + B00000111;
  return bByte;
}//BinaryAdd()
//END



The screen-shot on the right
shows the start of the program.


Byte = 1011010



On the left, you can see the byte
just above the stored byte - it's
exactly the same value..


..& again, the same value, although the program has gone through several more loop() cycles, as you
can see by the position of the
cursor at the right of the Serial
Monitor.


The value remains 1011010.

Arduino Bit-operations AND, OR, XOR


Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^)


The bitwise operators perform their calculations at the bit level of variables.
They help solve a wide range of common programming problems.
Much of the material below is from an excellent tutorial on bitwise math wihch may be found here.

Description and Syntax


Below are descriptions and syntax for all of the operators.
Further details may be found in the referenced tutorial.

Bitwise AND (&)


The bitwise AND operator in C++ is a single ampersand, &,
    used between two other integer expressions.
Bitwise AND operates on each bit position of the surrounding expressions independently,
    according to this rule:
    if both input bits are 1, the resulting output is 1, otherwise the output is 0.
Another way of expressing this is:

    0  0  1  1    operand1
    0  1  0  1    operand2
    ----------
    0  0  0  1    (operand1 & operand2) - returned result

In Arduino, the type int is a 16-bit value,
so using & between two int expressions causes 16 simultaneous AND operations to occur.
In a code fragment like:

    int a =  92;          // in binary: 0000000001011100
    int b = 101;         // in binary: 0000000001100101
    int c = a & b;      // result:    0000000001000100, or 68 in decimal.


Each of the 16 bits in a and b are processed by using the bitwise AND,
and all 16 resulting bits are stored in c, resulting in the value 01000100 in binary,
which is 68 in decimal.

One of the most common uses of bitwise AND is
    to select a particular bit (or bits) from an integer value, often called masking.
See below for an example



Bitwise OR (|)

The bitwise OR operator in C++ is the vertical bar symbol, |.
Like the & operator, | operates independently
   each bit in its two surrounding integer expressions,
   but what it does is different (of course). 


The bitwise OR of two bits is 1 if either or both of the input bits is 1, otherwise it is 0.
In other words:

    0  0  1  1    operand1
    0  1  0  1    operand2
    ----------
    0  1  1  1    (operand1 | operand2) - returned result

Here is an example of the bitwise OR used in a snippet of C++ code:

    int a =  92;         // in binary: 0000000001011100
    int b = 101;        // in binary: 0000000001100101
    int c = a | b;       // result:    0000000001111101, or 125 in decimal.




Example Program

A common job for the bitwise AND and OR operators
     is what programmers call Read-Modify-Write on a port.
On microcontrollers, a port is an 8 bit number
     that represents something about the condition of the pins.
Writing to a port controls all of the pins at once.

PORTD is a built-in constant that refers to the output states of digital pins 0,1,2,3,4,5,6,7.
If there is 1 in an bit position, then that pin is HIGH.
    (The pins already need to be set to outputs with the pinMode() command.)
So if we write PORTD = B00110001; we have made pins 2,3 & 7 HIGH.
One slight hitch here is that we may also have changeed the state of Pins 0 & 1,
    which are used by the Arduino for serial communications
    so we may have interfered with serial communication.

     Our algorithm for the program is:

    * Get PORTD and clear out only the bits corresponding to
    -      the pins we wish to control (with bitwise AND).
    * Combine the modified PORTD value with
    -      the new value for the pins under control (with biwise OR).

int i;             // counter variable
int j;

void setup()
{
    // set direction bits for pins 2 to 7,
    // leave 0 and 1 untouched   (xx | 00 == xx)
    DDRD = DDRD | B11111100;
    // same as pinMode(pin, OUTPUT) for pins 2 to 7
    Serial.begin(9600);

}//setup()


void loop()
{
     for (i=0; i<64; i++)
    {
          // clear out bits 2 - 7, leave pins 0 and 1 untouched (xx & 11 == xx)
         PORTD = PORTD & B00000011; 
         // shift variable up to pins 2 - 7 - to avoid pins 0 and 1
         j = (i << 2);     
        // combine the port information with the new information for LED pins        
        PORTD = PORTD | j;  
        // debug to show masking    
        Serial.println(PORTD, BIN);
        delay(100);
     }//for

}//loop()



Bitwise XOR (^)

There is a somewhat unusual operator in C++ called
    bitwise EXCLUSIVE OR, also known as bitwise XOR.
(In English this is usually pronounced "eks-or".)
The bitwise XOR operator is written using the caret symbol ^.

This operator is very similar to the bitwise OR operator |,
    only it evaluates to 0 for a given bit position when
    both of the input bits for that position are 1:

    0  0  1  1    operand1
    0  1  0  1    operand2
    ----------
    0  1  1  0    (operand1 ^ operand2) - returned result

Another way to look at bitwise XOR is that
    each bit in the result is a 1 if the input bits are different,
    or 0 if they are the same.

Here is a simple code example:

    int x = 12;             // binary: 1100
    int y = 10;             // binary: 1010
    int z = x ^ y;         // binary: 0110, or decimal 6


The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0)
    some of the bits in an integer expression.

In a bitwise OR operation if there is a 1 in the mask bit, that bit is inverted;
    if there is a 0, the bit is not inverted and stays the same.

Below is a program to blink digital pin 5.

// Blink_Pin_5
// demo for Exclusive OR

void setup()
{
      // set digital pin five as OUTPUT
     DDRD = DDRD | B00100000;
     Serial.begin(9600);

}//setup()


void loop()
{
      // invert bit 5 (digital pin 5), leave others untouched
      PORTD = PORTD ^ B00100000; 
      delay(100);

}//loop()


Reference:   http://arduino.cc/en/Reference/BitwiseAnd

Task 9 - function DoubleInt()

/*
 * Project:     DoubleInt_621Task09
 * Author:     Jane-Maree Howard
 * Date:          Tuesday 30/08/2011
 * Platform:   Arduino 22
 * Purpose:    To demonstrate a simple function taking a parameter,
                           with result outputted to Serial Monitor (SM)
 * Operation:  Description: calls a function DoubleInt(), taking an
                             integer as parameter, which returns a number double
                             the value of the parameter.                  
                            Declare: integer myInt; DoubleInt(int iInt)
                            Setup(): SM@9600baud;
                                              call DoubleInt() with an integer & a variable            
                            Loop(): empty                  
 */

void setup()  
{
  Serial.begin(9600);  //SM @ 9600baud
  int myInt = 23;
  Serial.println(DoubleInt(365));
  Serial.println(DoubleInt(myInt));
}//end setup()

void loop()                    
{  //empty
}//end loop()

// DoubleInt() - doubles parameter
int DoubleInt(int iInt)
{
  int j = iInt*2;
  return j;
}//DoubleInt()
//END














Arduino Functions

TASKS 15-17 -- Automation&Robotics: schedule 3

 


Tasks
Please respond to these tasks in your blog using the same reference numbers.
    • 15.  Write a subroutine to output a byte using the RS232
             soft-serial protocol using 8,N,2 at 9600.
      ~
    • 16.  Input bytes into a colleague's Arduino using the soft-serial
             from your pin to the Rx of your colleague's Arduino.
      Write up your code and take a photo of the set-up.
      ~
    • 17.  Write a soft-serial input, Rx, subroutine that takes bytes
             from a normal 5v RS232 Tx output at 8,N,2 at 9600.

    Tuesday, August 23, 2011

    Task 8 - "Hello World" program


    /*
     * Project:     HelloWorld_621Task08
     * Author:     Jane-Maree Howard
     * Date:            Tuesday 30/08/2011
     * Platform:     Arduino 22
     * Purpose:      To demonstrate a simple function call via the Serial Monitor (SM)            
     * Operation:  Description: uses a function in the main loop
                                    to output text 10 times to the SM                
                                   Declare: function called Hello()
                                    Setup(): SM @ 9600 baud            
                                    Loop(): call function Hello() 10 times, & print a blank line.
     */
    void setup()  
    {               
      Serial.begin(9600);  // Serial Monitor @ 9600 baud
    }//end setup()

    void loop()                    
    {
      for (int j=0; j<10; j++)
      {
        Hello();
      }//for j
      Serial.println();
    }//end loop()

    // 'Hello()' print routine
    void Hello()
    {
      Serial.println("Hello, world.");
    }//Hello()
    //END


    Task7 -2 Arduinos connected Tx -++ Rx pins

    Wednesday, August 10, 2011

    TASKS 7-14 -- Automation&Robotics: schedule 2


    Tasks

    Please respond to these tasks in your blog using the same reference numbers.

    • 7.  Connect up your RS232 Tx to the Rx of another Arduino.
           Output an "A" every second so that the input screen of the other Arduino writes this when it checks and prints the input serial screen.
      Take a photo of your set-up and publish it in your blog along with the code for Rx and Tx Arduinos.
      Reverse your roles with your colleague's Arduino. That is, swap the Rx/Tx duties.
      ~
    • 8.  Write a function called Hello()
            to output "Hello, world." when it's invoked.
      Call this function 10 times in your main loop().
      ~
    • 9.  Write a function DoubleInt to double an integer.
           Call it like this: Serial.println(DoubleInt(myInt)).
      Call it twice; once with a number and once with a variable.
      ~
    • 10.  Write a function to add B0111 to a byte passed to it.
             Call it 256 times in loop() using 0-255 as the parameters.
      ~
    • 11.  Write a function to AND a byte with B01
              and output the result to the screen.
      Test this in the main loop().
      ~
    • 12.  Write a function to say "yes" if the LSB (least significant bit)
             is a 1 or "no" otherwise.
      Test in the main loop.
      ~
    • 13.  Write a function to left shift a byte in the parameter position
             two places to the left then output the original and the final byte.
      ~
    • 14.  Write a program to output the bits, one by one,
             to the serial monitor screen from an arbitrary byte.

    Task 6 - Key-Press program: other serial monitor output

    /*
     * Project:    keyPressBrayOutput_621task06
     * Author:     Jane-Maree Howard
     * Date:       Wednesday 21/09/2011
     * Platform:   Arduino 22
     * Purpose:    To input a key-press from computer & output it to a Bray++ serial monitor
     * Operation:  Description:
     *             Declare: iKeyPress (int)
     *             Setup(): Bray++ serial monitor (BSM);  headings
     *             Arduino Library: LIB04_KeypressInput()            
     *             Loop(): reads input; prints character & ASCII codes                                             
     */


    The program is exactly the same as in 621task06, except that the Bray++ Terminal monitor is used instead.

    The COM port is selected on the PC running the Arduino to be the same as that selected on the opened Bray Terminal.

    The line
       Serial.begin(9600); 
                                           is still needed to allow the connection to be made but the Arduino Serial Monitor is NOT opened.


     This is the Bray Terminal monitor.
    Unfortunately, it presented me with only 2 COM ports, COM1 & COM3 - neither would work on the Arduino, which would upload only on COM4.

    So this'll have to wait until i get home (where i hope i can make it work).

    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.