Wednesday, October 26, 2011

Task 40 - Timer0 Polling routine - toggle LED on/off

/*
 * Project:      Timer_0_Polling_Experiment
 * Author:      Peter Brook tidied by Jane-Maree Howard on..
 * Date:          ..Wednesday 26/10/2011
 * Platform:    Arduino 22
 * Purpose:     To visually demonstrate Timer0 polling
 * Operation:  Polls Timer0 prescaled by 1024; toggles LED on/off every second
 *      Declare: toggle variable; counter vble; ToggleLED() routine 
 *      Setup():  Serial port @9600baud; Prints opening statement; delays 5 seconds
 *                      set TImerMaSK0 to zero - turn of Timer0 interrupt;
 *                      set Timer/CounterControlRegister0B to DEC5; prescaler /s by 1024;
 *      Loop():    idle until TimerInterruptFlagRegister0 == 1;
 *                      reset TIFR0; count 1 'tick'(from 0 to 60, 61 'ticks');
 *                      @ 60'ticks', reset 'ticks' & toggle LED on/off
 */
int     state = 1;        // LED toggle vble - init. OFF!      
byte  ticks = 0;        // counts up towards 60, the LED toggle value

void setup()
{
  Serial.begin(9600);  // Serial port @ 9600 baud
  Serial.println("\n\tInitial state is OFF\n\t");
  delay(5000);            // wait 5 seconds 
  // initialize digital pin 13 as an output.
  // Pin 13 has an LED connected on most Arduino boards:

  pinMode(13, OUTPUT);
  //after this, can't use 'delay() any more
  TIMSK0 = 0;          // turn off Interrupt for Timer0
  TCCR0B = 5;         // Prescaler divides by 1024;  BIN101 = DEC5
}//setup()

void loop()
{
   while(byte(TIFR0 &0x01) ==0) // loop till flag goes up
   {/* do nothing */}                       
   TIFR0 = 1;                // clear flag
   ticks++;                     // count a tick
 
   if (ticks == 60)          // preset value
   {
      ticks=0;                  // reset 'ticks'
      Serial.print("\tTT ");
      ToggleLED();        // toggle LED
   }//if()
}// loop()

void ToggleLED()
{
  if (state==1)
  {
    digitalWrite(13,LOW);                    // LED turns OFF
    state = 0;                                          // toggle 'state' vble
    Serial.print("I've toggled OFF");   // Serial Monitor o/p
  }//if()
  else
  {
    digitalWrite(13,HIGH);                    // LED turns ON
    state=1;                                             // toggle 'state' vble
    Serial.println("I've toggled ON "); // Serial Monitor o/p
  }//else()
}//ToggleLED() 



In the code, the initial state of the on-board LED (pin 13) is OFF.


The SM output records the first toggle ("TT") & the LED
(of course ) stays OFF.

From then on, the LED's recorded toggled state matches that of the on-board LED (pin 13)

Tasks 40-41 -- Automation&Robotics: schedule 8

  • 40.. Copy the Timer0 Polling routine
             given to you in a previous post,
    and run it with some tidying up.
    Show your tidy code.
    ~
  • 41.. Repeat the program in 40,
             but this time make it work a one second LED flash
    using an Interrupt Service Routine.
    ~


Thursday, October 20, 2011

Task 38a - using the I2C bus with an EEPROM 24LC64 addressed differently

I've added a line to the earlier code coz i want to make it easier to change the device address.


 In this particular case:
  A0 is LOW;
  A1 is HIGH;
  A2 is LOW.


This device needs to be addressed as 0x52
(see code changes below)


#include {Wire.h}           // I2C library - usual brackets warning

byte  LCaddress  = 0x52;    // 24LC64 device address
:
:
// now write the char-string to the EEPROM - NOTE 0x52:address pin A1 is HIGH
i2c_eeprom_write_page(LCaddress, 0, (byte *)somedata, sizeof(somedata));
:
:
// now access the first address (0) from the memory
 byte b = i2c_eeprom_read_byte(LCaddress, 0);   //NOTE 0x52:address pin A1 is HIGH
:
:
 addr++;                //..next address &..
//..access next address from the memory.
b = i2c_eeprom_read_byte(LCaddress, addr);      //NOTE 0x52:address pin A1 is HIGH
:
:
etcetera..

& i've changed the input data just to make sure the program's doing what it should be doing..
 
char somedata[] = "Jane-Maree's EEPROM data";  // - 24 characters..



And this is what it does..










..& when i've written..

byte  LCaddress  = 0x51;    // 24LC64 device address


 ..it's because.. 

  A0 is HIGH;
  A1 is LOW;
  A2 is LOW.


..& this device needs to be addressed as 0x51..



..producing this  ==} 




i've got pikkies of the breadboard arrangement clearly (i hope) showing the connections to A0, A1, & A2.


Everything else is unchanged..







Wednesday, October 19, 2011

Task 38.1 - using the I2C bus with an EEPROM 24LC64: software edit

/*
 * Project:      eeprom_fromWeb
 * Author:      hkhijhe - tidied even more by Jane-Maree Howard
 * Date:         01/10/2010
 * Platform:   Arduino 22
 * Purpose:    To demonstrate use of the I2C bus with an EEPROM 24LC64
 -             "You were issued with an eeprom 24LC64 IC.
 -              This is a TWI memory that you connect to SCL and SDA on the Arduino.
 -              Run the program from the blog.
 -              Show your code changes and the serial monitor output in your blog".
 * Operation:  Because this chip is I2C,
 -               it only uses Arduino analog pins 4 & 5 (SDA and SCL),
 -               and of course the power (5V) and GND.
 -             Connect as follows:
 -                Arduino pin 4 to EEPROM pin 5
 -                Arduino pin 5 to EEPROM pin 6
 -                Arduino 5V to EEPROM pin 8
 -                Arduino GND to EEPROM pin 1,2,3,4
 -             Be sure to leave pin 7 of the EEPROM open or tie it to GND,
 -               otherwise the EEPROM will be write protected.
 -             Include: Wire.h - the I2C library
 -             Declare: various i2c_eeprom_read.. i2c_eeprom_write.. procedures
 -             Setup(): Opens the Wire connection, the Serial connection,
 -                      writes a char-string to the EEPROM, & prints heading
 -                      NOTE: the char-string has length = 28
 -             Loop():  Reads from the EEPROM, & prints, one byte at a time, & repeats                
 */

#include {Wire.h}               //I2C library-usual brackets warning

  void setup()
  {
    // a char-string of data to write to the EEPROM - 28 characters long!!
    char someData[] = "this is data from the eeprom";
   /*
    char somedata[] = "this is Jane-Maree's eeprom data"; - 32 characters..
     .. & it bombs.  Gibberish - I've tried it!  See WARNING below..
   */
    Wire.begin();                 // Wire connection initialised
    Serial.begin(9600);       // Serial connection @ 9600 baud
    // now write the char-string to the EEPROM
    i2c_eeprom_write_page(0x50, 0, (byte *)someData, sizeof(someData));
    delay(10);                     //add a small delay..
    // ..& print a heading
    Serial.println("\nMemory written");
    Serial.println("\nNow read memory & print to Serial port");
  } //setup()

  void loop()
  {
    int addr=0;               // address parameter, starts @ 0
    // now access the first address (0) from the memory
    byte b = i2c_eeprom_read_byte(0x50, 0);
    // while there are non-zero bytes..
    while (b!=0)
    {
      Serial.print((char)b); //..print content as char to serial port..
      addr++;                      //..next address &..
      //..access next address from the memory.
      b = i2c_eeprom_read_byte(0x50, addr);
    }//while ()
    Serial.println(" ");     // new line..
    delay(2000);               // ..2 second delay, & start again!
  } //loop()


   /*
    WARNING: address is a page address, 6-bit end will wrap around
    also, data can be maximum of about 30 bytes,
    because the Wire library has a buffer of 32 bytes
  */
  void i2c_eeprom_write_page( int deviceAddress, unsigned int eeAddressPage, byte* data, byte length )
  {
    Wire.beginTransmission(deviceAddress);
    Wire.send((int)(eeAddressPage >> 8));       // MSB
    Wire.send((int)(eeAddressPage & 0xFF));  // LSB
    byte c;
    for ( c = 0; c < length; c++)
      Wire.send(data[c]);
    Wire.endTransmission();
  } //i2c_eeprom_write_page()


  byte i2c_eeprom_read_byte( int deviceAddress, unsigned int eeAddress )
  {
    byte rdata = 0xFF;
  
    //ask device by address for data
    Wire.beginTransmission(deviceAddress);
    Wire.send((int)(eeAddress >> 8));             // MSB
    Wire.send((int)(eeAddress & 0xFF));           // LSB
    Wire.endTransmission();
  
    Wire.requestFrom(deviceAddress,1);
    if (Wire.available())
      rdata = Wire.receive();
    return rdata;
  } //i2c_eeprom_read_byte()

// END

Tuesday, October 18, 2011

Task 38 - using the I2C bus with an EEPROM 24LC64

(The post following this one goes into different device addresses)
The address given in the original code - below, more or less - has the 24LC64 connected as follows:

Pin A0 is LOW;
Pin A1 is LOW;
Pin A2 is LOW.

This requires the device address to be 0x50.

Later, we will see devices connected so that they are to be addressed as  0x51  &  0x52, merely by varying  the LOW/HIGH status of A0, A1, & A2.


This is how several devices can be connected to the same I2C bus - they are addressed differently.
/*
 * Project:      eeprom_fromWeb
 * Author:      hkhijhe - tidied by Jane-Maree Howard
 * Date:         01/10/2010
 * Platform:   Arduino 22
 * Purpose:    To demonstrate use of the I2C bus with an EEPROM 24LC64
 -             "You were issued with an eeprom 24LC64 IC.
 -              This is a TWI memory that you connect to SCL and SDA on the Arduino.
 -              Run the program from the blog.
 -              Show your code changes and the serial monitor output in your blog
". 
 * Operation:  Because this chip is I2C,
 -               it only uses Arduino analog pins 4 & 5 (SDA and SCL),
 -               and of course the power (5V) and GND.
 -             Connect as follows:
 -                Arduino pin 4 to EEPROM pin 5
 -                Arduino pin 5 to EEPROM pin 6
 -                Arduino 5V to EEPROM pin 8
 -                Arduino GND to EEPROM pin 1,2,3,4
 -             Be sure to leave pin 7 of the EEPROM open or tie it to GND,
 -               otherwise the EEPROM will be write protected.
 -             Include: Wire.h - the I2C library
 -             Declare: various i2c_eeprom_read.. i2c_eeprom_write.. procedures
 -             Setup(): Opens the Wire connection, the Serial connection,
 -                      writes a char-string to the EEPROM, & prints heading
 -                      NOTE: the char-string has length = 28
 -             Loop():  Reads from the EEPROM, & prints, one byte at a time, & repeats                 
 */

#include {Wire.h}               //I2C library-usual brackets warning
 
  void setup()
  {
    // a char-string of data to write to the EEPROM - 28 characters long!!
    char somedata[] = "this is data from the eeprom";

   /*
    char somedata[] = "this is Jane-Maree's eeprom data"; - 32 characters..
     .. & it bombs.  Gibberish - I've tried it!  See WARNING below..

   */
    Wire.begin();                 // Wire connection initialised
    Serial.begin(9600);       // Serial connection @ 9600 baud
    // now write the char-string to the EEPROM
    i2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata));
    delay(10);                     //add a small delay..
    // ..& print a heading
    Serial.println("\nMemory written");
    Serial.println("\nNow read memory & print to Serial port");
  } //setup()

  void loop()
  {
    int addr=0;               // address parameter, starts @ 0
    // now access the first address (0) from the memory
    byte b = i2c_eeprom_read_byte(0x50, 0);
    // while there are non-zero bytes..
    while (b!=0)
    {
      Serial.print((char)b); //..print content to serial port..
      addr++;                      //..next address &..
      //..access next address from the memory.
      b = i2c_eeprom_read_byte(0x50, addr);
    }//while ()
    Serial.println(" ");     // new line..
    delay(2000);               // ..2 second delay, & start again!
  } //loop()
 

  void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data )
  {
    int rdata = data;
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8));             // MSB
    Wire.send((int)(eeaddress & 0xFF));        // LSB
    Wire.send(rdata);
    Wire.endTransmission();
  } //i2c_eeprom_write_byte()

  /*
    WARNING: address is a page address, 6-bit end will wrap around
    also, data can be maximum of about 30 bytes,
    because the Wire library has a buffer of 32 bytes
  */
  void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length )
  {
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddresspage >> 8));       // MSB
    Wire.send((int)(eeaddresspage & 0xFF));  // LSB
    byte c;
    for ( c = 0; c < length; c++)
      Wire.send(data[c]);
    Wire.endTransmission();
  } //i2c_eeprom_write_page()

  byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress )
  {
    byte rdata = 0xFF;
   
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8));             // MSB
    Wire.send((int)(eeaddress & 0xFF));        // LSB
    Wire.endTransmission();
   
    Wire.requestFrom(deviceaddress,1);
    if (Wire.available())
      rdata = Wire.receive();
    return rdata;
  } //i2c_eeprom_read_byte()

  // maybe let's not read more than 30 or 32 bytes at a time! See NOTE!
  void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length )
  {
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8));          // MSB
    Wire.send((int)(eeaddress & 0xFF));     // LSB
    Wire.endTransmission();
   
    Wire.requestFrom(deviceaddress,length);
    int c = 0;
    for ( c = 0; c < length; c++ )
      if (Wire.available())
        buffer[c] = Wire.receive();          
  } //i2c_eeprom_read_buffer()



You can see the gibberish along the top, caused by a buffer over-run.

i think this was what happened earlier, when i was 'experimenting'.

When all else fails, 
   Read The Instructions.

Now i need to figure out how to get around that buffer problem - i've not yet understood how to add a C-header file (.h) & C-file (.cpp)..

Task 38 - a diversion "..long time the manxome foe (s)he sought.."

..well it wasn't THAT long - I'm back in D208 &..

"Listen.. Listen.. It's a goal!!"
 (with apologies to the legendary Winston McCarthy).

well, yes!  Now it's working perfectly.  And so, for a diversion,
from Lewis Carroll..
-                                                                              The Jabberwock
-                                                                                         as illustrated by John Tenniel
 

Jabberwocky

'Twas brillig, and the slithy toves

Did gyre and gimble in the wabe;
All mimsy were the borogoves,
And the mome raths outgrabe.

"Beware the Jabberwock, my son!

The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
The frumious Bandersnatch!"

He took his vorpal sword in hand:

Long time the manxome foe he sought--
So rested he by the Tumtum tree,
And stood awhile in thought.

And as in uffish thought he stood,

The Jabberwock, with eyes of flame,
Came whiffling through the tulgey                              
wood,                                                                    
And burbled as it came!

One, two! One, two! and through and through

The vorpal blade went snicker-snack!
He left it dead, and with its head
He went galumphing back.

"And hast thou slain the Jabberwock?

Come to my arms, my beamish boy!
O frabjous day! Callooh! Callay!"
He chortled in his joy.

'Twas brillig, and the slithy toves

Did gyre and gimble in the wabe;
All mimsy were the borogoves,
And the mome raths outgrabe. 

                                                            "
from Through the Looking-Glass, and What Alice Found There (1872)

I've always liked this, so I'm pleased to have an excuse to include it.
And isn't Shakespeare marvellous:

"The graves stood tenantless, and the sheeted dead
  Did squeak and gibber in the Roman streets"

Hamlet: Act I, Scene i.
 

Task 37 - Integrate the new eeprom library from the link

Integrating the new eeprom library from the link here

"To use the library, make a folder in your SKETCHBOOKPATH\libaries 
  with the name I2C_EEPROM and put the .h and .cpp there. 
  Optionally make a examples subdirectory to place the sample app."

Reference: http://arduino.cc/playground/Main/LibraryForI2CEEPROM


Having tried Task 38 in room D208, I'm now wondering if my current change of room (D209) is causing a problem.  The website refers to a 'boundary problem', & I have a feeling that I don't really know what I'm doing (yet)..








Hmmm.. When in a quandary, have a laugh - "thanks" to Mike Keefe of the 'Denver Post'




















And this guy, Andrew Toos, at CartoonStock

 I like the 'robot' on the right..  these are just too good (hehe)  :-D

TASKS 37-39 -- Automation&Robotics: schedule 7


     37.  Integrate the new eeprom library from the link here.
            You should be able to #include this new library this.

38. You were issued with an eeprom 24LC64 IC.
      This is a TWI memory that you connect to SCL and SDA on the Arduino.
      Run the program from the blog.
     Show your code changes and the serial monitor output in your blog.

39.  The problem with the program above is that
       it can only handle about 30 bytes at a time.
     Overcome this problem so that any amount of data can be sent
     up to the capacity of the chip.   
    Write up your findings.

Sunday, October 16, 2011

How to look after your CMOS chips - with ordinary kitchen or garage stuff

Your CMOS chips are delicate little beasts - in particular, they're very sensitive to static electricity!  You really don't want this happening..

The trouble is, those nano-sized tracks inside them are easily damaged by even quite small currents sparking between them, & since the static from you, your clothes, the environment generally, is of the order of several thousand Volts, it doesn't take much to cause a spark between tracks, which are burnt out by the discharge & so that/those track(s) is/are useless & irreparable.

My little CMOS EEPROM chip is one of those, & the way to safeguard them is to:
   short-circuit all the pins to each other! 
That's why you see them "plugged" into tinfoil, conductive foam & the like.  I spent the princely sum of (ta-DA!)..

$2 - one spectacles case;
$2 - roll of heavy duty kitchen aluminium foil;
$2 - bath sponge;
---
..$6 at the local '2N5' shop in Great King St (near Countdown)..

..& here's a pikkie of the finished product!

Inside the lid you can see a pair of plastic tweezers for handling CMOS chips, & a little chip safe & sound in its protective CMOS-motel.

You've prob'ly got stuff like this @ home: kitchen foil (i folded it 2-3 times), old bath sponge (you can snip a lump off your existing one), & any small container  - doesn't really matter what it is..  & you have a (free!) CMOS chip-carrier. 

They'll be perfectly safe in there (unless you put your electrically grubby paws on them & kark them before they get their chance to Shine!) until it's time for them to Do Their Stuff!


On the right you can see another device for safeguarding your CMOS chips.

You can buy these things, with fancy conductive foam wristbands, but i made this one, coz i happened to have this peice of wire with a crocodile clip on it, so i stripped the other end & connected that to the EARTH terminal - the silvery BOTTOM one in the photo - don't do this unless you KNOW WHAT YOU'RE DOING!

230Volts AC up your arm won't leave you feeling on top of the world (unless that's how high you jump)!!!

The crocodile clip attaches to my (sterling silver - good conductor) bangle.  
Failing that, a stainless steel pot-scrub (another kitchen item) would make a useful 'hand-wipe';   very dry hands aren't good.

Friday, October 7, 2011

Tasks 18 & 19 - Arduino motor mini-board spinning both directions - software revised

/*
 * Project:    MotorSpin_621task18
 * Author:     Jane-Maree Howard
 * Date:       Saturday 03/09/2011; modified Friday 07/10/2011
 * Platform:   Arduino 22
 * Purpose:    To demonstrate control of a DC motor, using my ARDUINO Library function(s)
 * Operation:  Description: The motor is a ET-MINI DC-MOTOR board.
 -                                            See LIB01_MotorSpin_ETMINI for more details
 -             Declare:     Pin variables (byte), delay multiplier (int)
 -             Setup():     Serial.begin = 9600 baud; initialise digital control pins
 -             Procedure(): RotateMotor(params) takes 3 bytes (for the motor)       
 -             Loop():      Set RotateMotor() parameters; delay for 5 seconds             
 */
// digital control pins for ET-MINI DC-MOTOR
byte bRight    = 10;     // IN1 is pin 10 - rotate Right
byte bLeft     = 8;      // IN2 is pin 8  - rotate Left
byte bEnable   = 13;     // EN  is pin 13 - Enable rotation, must = '1'
// delay control in seconds
int iDelay     = 1000;   // delay variable     

void setup()  
{
  Serial.begin(9600);    //SM @ 9600baud
  //initialise digital control pins
  MotorPinMode(bRight,bLeft,bEnable);
}//end setup()

void loop()
//
{
   RotateMotor(bRight,bLeft,bEnable,10);   // rotate motor Right..
   delay(iDelay*5);                        // ..for 5 seconds, then..
   RotateMotor(bRight,bLeft,bEnable,0);    // ..stop..
   delay(iDelay*5);                        // ..for 5 seconds, then..
}//end loop()
//END
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/*
 * Project:     MotorSpin_621task19_1
 * Author:     Jane-Maree Howard
 * Date:         Saturday 03/09/2011; modified Friday 07/10/2011
 * Platform:   Arduino 22
 * Purpose:    To demonstrate control of a DC motor, using my ARDUINO Library function(s)
 * Operation:  Description: The motor is a ET-MINI DC-MOTOR board.
 -                      Declare:     Pin variables (byte), delay multiplier (int)
 -                      Setup():      Serial.begin = 9600 baud;

 -                                          initialise digital control pins as outputs
 -                      Procedure(): RotateMotor(params) takes 3 bytes (for the motor)         

 -                       Loop():      Set RotateMotor() parameters; 
 -                               delay for 5 seconds then reverse            
 */
byte bRight    = 10;     // IN1 is pin 10 - rotate Right
byte bLeft     = 8;        // IN2 is pin 8  - rotate Left
byte bEnable   = 13;   // EN  is pin 13 - Enable rotation, must = '1'
int iDelay     = 1000;   // delay variable     

void setup()  
{
   Serial.begin(9600);    //Serial connection @ 9600baud
   //initialise digital control pins as outputs
   MotorPinMode(bRight,bLeft,bEnable);      //LIB01_MotorSpin_ETMINI
}//end setup()

void loop()                    
{
   RotateMotor(bRight,bLeft,bEnable,10);      // LIB01_MotorSpin_ETMINI: rotate motor Right..
   delay(iDelay*5);                                            // ..for 5 seconds, then..
   RotateMotor(bRight,bLeft,bEnable,0);       // ..stop briefly..
   delay(150);
   RotateMotor(bRight,bLeft,bEnable,11);      // ..rotate motor Left..
   delay(iDelay*5);                                            // ..for 5 seconds
   RotateMotor(bRight,bLeft,bEnable,0);       // ..stop briefly..
   delay(150);
}//end loop()
//END

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

to see how the following is included with the above sketches, click on this link

/*
 * Project:    LIB01_MotorSpin_ETMINI
 * Author:     Jane-Maree Howard
 * Date:       Friday 07/10/2011
 * Platform:   Arduino 22
 * Purpose:    To make a library function for an ET-MINI DC-MOTOR board
 * Operation:  Description: The motor is an ET-MINI DC-MOTOR board.
  -                           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 MotorPinMode(byte, byte, byte);  

 -                                            void RotateMotor(byte,byte,byte,byte);     
  -                     Loop():   NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
  -                                    (used  only in testing)            
 */

void MotorPinMode(byte bR, byte bL, byte bEN)
{
  /*  initialise digital control pins as outputs.
      CALLED IN MAIN SKETCH  */
  pinMode(bR, OUTPUT);        // IN1
  pinMode(bL,  OUTPUT);       // IN2
  pinMode(bEN, OUTPUT);       // EN 
}//MotorPinMode()

// RotateMotor- CALLED IN MAIN SKETCH
void RotateMotor(byte bR, byte bL, byte bEN, byte bMO)
{
  /*  Rotate ET-MINI DC-MOTOR: bR,bL,bEN are Pins; bM en/dis-ables */
  switch (bMO)
  {
    case 01:               // PAUSE
      digitalWrite(bR,0);  // write '0' to motorboard pin IN1 - no rotation right
      digitalWrite(bL,0);  // write '0' to motorboard pin IN2 - no rotation left
      digitalWrite(bEN,1); // write '1' to motorboard pin EN - enables motor
      break;
    case 10:               // rotate RIGHT
      digitalWrite(bR,1);  // write '1' to motorboard pin IN1
      digitalWrite(bL,0);  // write '0' to motorboard pin IN2 - no rotation left
      digitalWrite(bEN,1); // write '1' to motorboard pin EN - enable motor
      break;
    case 11:               // rotate LEFT 
      digitalWrite(bR,0);  // write '0' to motorboard pin IN1 - no rotation right
      digitalWrite(bL,1);  // write '1' to motorboard pin IN2
      digitalWrite(bEN,1); // write '1' to motorboard pin EN - enable motor
      break;
    default:               // STOP - disable motor
      digitalWrite(bR,0);  // write '0' to motorboard pin IN1 - no rotation right
      digitalWrite(bL,0);  // write '0' to motorboard pin IN2 - no rotation left
      digitalWrite(bEN,0); // write '0' to motorboard pin EN - disable motor
  }//switch()case
}//RotateMotor()
//END

tasks 18 & 19: revised software showing library use

First of all i re-wrote the software for tasks 18 & 19, as you will see in the next post

i then rewrote the functions in a test LIB sketch, & when it compiled & ran ok, i deleted the setup() & loop() parts & saved it in a separate folder which i have  called Arduino Libraries.

These should NOT BE CONFUSED with the C-libraries, for which you must write the line, at the head of your code:
 #include Clib1.h
(which normally has those pointy brackets enclosing the .h file - but which means something entirely different in this blog's HTML so i can't put them in).



In the Arduino task sketch, click on 'Sketch' as shown on the right.

Click on 'Add File...'

 You get the dialog box as shown on the left.

You have to know where your required file is, & open it..

Click on the 'Open' tab.

You now have TWO sketches open, & they can be combined to operate as one.

And when you save the original sketch, the LIBrary sketch is saved with it, so that every time you subsequently open the main sketch you get the LIB sketch opening with it.

i should add a line to the LIB sketch, describing the required parameters.

in theory (hehe - famous last words!) i should be able:
  to write anything that has the right parameters for the right job;
  to plug in a different motor provided the parameters fit;
  change the motor type used with only minor code alterations.

this has been a bit of a holdup, & i think is worth the extra effort i've made for it.

ps: the same probably applies to Processing sketches i'd imagine..

Here's some example code - Keypress input via the Serial Monitor:


 /*
 * 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()
.
 //END

Used in the following posts (click to return to them if necessary):
  Project 02 - Motor Project;
 Task 13;
 Task 14;