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;
This is about robots & automation. It's a companion blog to JaneMareesTech, which is about the Arduino microcontroller system & related electronics
Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts
Friday, October 7, 2011
Sunday, September 25, 2011
Project 3 -- Minor Project: schematic
this is the best i could do - i don't know how to make circuit jumper in Fritzing but it should be obvious that that's what they are, not connections..
The 3 'wriggly' things are Peltier elements (Thermistors)
The 3 'wriggly' things are Peltier elements (Thermistors)
Labels:
EEPROM,
fritzing,
functions,
hardware,
interrupts,
LED,
MINOR PROJECT,
photocell,
PROJECTS,
schematic,
sensors,
serial monitor,
thermistor
Project 3 -- Minor Project: software
/*
* Project: Minor_Project_621
* Author: Jane-Maree Howard
* Date: Wednesday 21/09/2011
* Platform: Arduino 22
* Purpose: To demonstate a fridge-control robot
* Operation: Several thermistors are mounted inside the fridge.
* Their values are read in sequence at 1-minute intervals,
* their average is calculated, & the values & their average stored in EEPROM
* A LDR is also mounted inside the fridge, to detect when the door is opened.
* Change in the LDR resistance trigers an interrupt, & this is also recorded.
Declare:
Setup():
Loop():
*/
#include // EEPROM library
int iAnalogPin[] = {0,1,2,3}; // analog pin numbers
int iLEDpin = 13; // door-open LED
int iDarkLevel = 100; // pre-set photocell reading triggering interrupt
int iGetData = 0; // get signal to retrieve data
int iHeading = 0; // signal to print heading
int iSecond = 1000; // 1 second delay &..
int iMinute = 60000; // ..1 minute delay
int iTemperature[3]; // temperature readings from A0 - A2
int iAvgTemp; // Average of the 3 temperatures
volatile byte iAddressCount = 0; // EEPROM address counter
volatile byte bLEDstate = 0; // LED pin ON or OFF
/* all operations are performed in the setup() section of the program */
void setup()
{
delay(iSecond*5); // up to 10 minute delay
pinMode(iLEDpin, OUTPUT); // LED-pin is an output..
digitalWrite(iLEDpin,LOW); // ..initially turned off
Serial.begin(9600); // SM @ 9600baud
// attach interrupt 0, to digital pin 2
attachInterrupt(0, DoorOpen, CHANGE); // ..on CHANGE
iGetData = KeypressInput(); // check for receiving data
while (iGetData == 0) // if no signal,start reading..
{
// stop interrupts while reading
noInterrupts(); // stop interrupts while reading
iAvgTemp = 0; // zero average Temperature vble
/* First read the 3 values into iTemperature[] .. */
for (byte j=0; j<3; j++)
{
// read value on 3 thermistor pins i.e. iAnalogPin[j]
iTemperature[j] = analogRead(iAnalogPin[j]);
// now we have to map 0-1023 to 0-255..
// ..since we want to store the values as bytes..
iTemperature[j] = map(iTemperature[j], 0, 1023, 0, 255);
// ..& write them to EEPROM
EEPROM.write(iAddressCount, iTemperature[j]);
// increment the address count..
iAddressCount++ ;
iAvgTemp += iTemperature[j]; // add to average total
// delay 10 milliseconds
delay(10);
}//for(j)
iAvgTemp = (int)(iAvgTemp/3);
// write Average to EEPROM also..
EEPROM.write(iAddressCount, iAvgTemp);
// ..& increment the address count.
iAddressCount++ ;
interrupts(); // re-enable interrupts
// debugging
Serial.print("\nrecorded data\t");
for (int j=0;j<3;j++)
Serial.print(iTemperature[j]);
Serial.print(iAvgTemp);
delay(iMinute); // delay 1 minute..
iGetData = KeypressInput(); // ..then check for receiving data
}// while(==0) */
while (iGetData != 0) // ..otherwise, retrieve stored data & send to SM
{
if (iHeading%20 == 0)
Heading();
iHeading++;
//diagnostic stuff & logic testing
if (iHeading > 100)
{
iGetData = 0; // check for receiving data
iHeading = 0;
}//if()
}// while(!=0)
}// end setup()
void loop()
{/*nothing done in here*/}//end loop()
/* prints heading to Serial Monitor at pre-set intervals */
void Heading()
{
Serial.println("\nTemperatures\tT0\tT1\tT2\tT3\tAverage\tDoor\n");
}//Heading()
//END
LIBRARY FUNCTIONS:
/*
* 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
- 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
/*
* Project: LIB09_photocell_interrupt
* Author: Jane-Maree Howard
* Date: Thursday 24/11/2011
* Platform: Arduino 22
* Purpose: To make a Photocell Switch for an interrupt pin (digital 2 or 3).
* Operation: The circuit arrangement is the same as the ladyada
* LED dimmer, but here the software has a trigger threshold
* for turning the interrupt pin on.
* The output of the Analog pin is measured as before,
* but instead of analogWrite() to the interrupt pin,
* digitalWrite is used to switch the pin on or off.
* We don't need to map the Analog pin output,
* since it's merely a trigger.
* Original description:
* "Connect one end of the photocell to 5V, the other end to Analog 0.
* Then connect one end of a 4.7K resistor from Analog 0 to ground
* Connect LED from pin 9 through a resistor to ground"
* Declare, Setup(), Loop():
* NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
- Procedure(): void PhotocellTrigger(int,int,int);
- void DoorOpen();
*/
/* the photocell pin value is tracked by the interrupt */
void PhotocellTrigger(int iInterruptPin, int iPhotoPin, int iDark)
{
int iPhotoReading = analogRead(iPhotoPin); //i.e. analog 0
//we now use our 'darkLevel' variable to trigger the LED ON.
if (iPhotoReading > iDark)
digitalWrite(iInterruptPin, HIGH); // turn InterruptPin on, else..
else
digitalWrite(iInterruptPin, LOW); // ..turn InterruptPin off
}//PhotocellTrigger()
/* called when the Interrupt is triggered - records door state */
void DoorOpen()
{
digitalWrite(iLEDpin, bLEDstate); // Turn door-open LED ON or OFF..
EEPROM.write(iAddressCount, bLEDstate);// ..record the incident..
iAddressCount++; // .. increment address count..
bLEDstate = !bLEDstate; // ..& change LED-state ON to OFF or vice versa
}//DoorOpen()
//END
* Project: Minor_Project_621
* Author: Jane-Maree Howard
* Date: Wednesday 21/09/2011
* Platform: Arduino 22
* Purpose: To demonstate a fridge-control robot
* Operation: Several thermistors are mounted inside the fridge.
* Their values are read in sequence at 1-minute intervals,
* their average is calculated, & the values & their average stored in EEPROM
* A LDR is also mounted inside the fridge, to detect when the door is opened.
* Change in the LDR resistance trigers an interrupt, & this is also recorded.
Declare:
Setup():
Loop():
*/
#include
int iAnalogPin[] = {0,1,2,3}; // analog pin numbers
int iLEDpin = 13; // door-open LED
int iDarkLevel = 100; // pre-set photocell reading triggering interrupt
int iGetData = 0; // get signal to retrieve data
int iHeading = 0; // signal to print heading
int iSecond = 1000; // 1 second delay &..
int iMinute = 60000; // ..1 minute delay
int iTemperature[3]; // temperature readings from A0 - A2
int iAvgTemp; // Average of the 3 temperatures
volatile byte iAddressCount = 0; // EEPROM address counter
volatile byte bLEDstate = 0; // LED pin ON or OFF
/* all operations are performed in the setup() section of the program */
void setup()
{
delay(iSecond*5); // up to 10 minute delay
pinMode(iLEDpin, OUTPUT); // LED-pin is an output..
digitalWrite(iLEDpin,LOW); // ..initially turned off
Serial.begin(9600); // SM @ 9600baud
// attach interrupt 0, to digital pin 2
attachInterrupt(0, DoorOpen, CHANGE); // ..on CHANGE
iGetData = KeypressInput(); // check for receiving data
while (iGetData == 0) // if no signal,start reading..
{
// stop interrupts while reading
noInterrupts(); // stop interrupts while reading
iAvgTemp = 0; // zero average Temperature vble
/* First read the 3 values into iTemperature[] .. */
for (byte j=0; j<3; j++)
{
// read value on 3 thermistor pins i.e. iAnalogPin[j]
iTemperature[j] = analogRead(iAnalogPin[j]);
// now we have to map 0-1023 to 0-255..
// ..since we want to store the values as bytes..
iTemperature[j] = map(iTemperature[j], 0, 1023, 0, 255);
// ..& write them to EEPROM
EEPROM.write(iAddressCount, iTemperature[j]);
// increment the address count..
iAddressCount++ ;
iAvgTemp += iTemperature[j]; // add to average total
// delay 10 milliseconds
delay(10);
}//for(j)
iAvgTemp = (int)(iAvgTemp/3);
// write Average to EEPROM also..
EEPROM.write(iAddressCount, iAvgTemp);
// ..& increment the address count.
iAddressCount++ ;
interrupts(); // re-enable interrupts
// debugging
Serial.print("\nrecorded data\t");
for (int j=0;j<3;j++)
Serial.print(iTemperature[j]);
Serial.print(iAvgTemp);
delay(iMinute); // delay 1 minute..
iGetData = KeypressInput(); // ..then check for receiving data
}// while(==0) */
while (iGetData != 0) // ..otherwise, retrieve stored data & send to SM
{
if (iHeading%20 == 0)
Heading();
iHeading++;
//diagnostic stuff & logic testing
if (iHeading > 100)
{
iGetData = 0; // check for receiving data
iHeading = 0;
}//if()
}// while(!=0)
}// end setup()
void loop()
{/*nothing done in here*/}//end loop()
/* prints heading to Serial Monitor at pre-set intervals */
void Heading()
{
Serial.println("\nTemperatures\tT0\tT1\tT2\tT3\tAverage\tDoor\n");
}//Heading()
//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: Description: 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
* Project: LIB09_photocell_interrupt
* Author: Jane-Maree Howard
* Date: Thursday 24/11/2011
* Platform: Arduino 22
* Purpose: To make a Photocell Switch for an interrupt pin (digital 2 or 3).
* Operation: The circuit arrangement is the same as the ladyada
* LED dimmer, but here the software has a trigger threshold
* for turning the interrupt pin on.
* The output of the Analog pin is measured as before,
* but instead of analogWrite() to the interrupt pin,
* digitalWrite is used to switch the pin on or off.
* We don't need to map the Analog pin output,
* since it's merely a trigger.
* Original description:
* "Connect one end of the photocell to 5V, the other end to Analog 0.
* Then connect one end of a 4.7K resistor from Analog 0 to ground
* Connect LED from pin 9 through a resistor to ground"
* Declare, Setup(), Loop():
* NONE - MUST BE USED ONLY IN CONJOINING SKETCH(ES)
- Procedure(): void PhotocellTrigger(int,int,int);
- void DoorOpen();
*/
/* the photocell pin value is tracked by the interrupt */
void PhotocellTrigger(int iInterruptPin, int iPhotoPin, int iDark)
{
int iPhotoReading = analogRead(iPhotoPin); //i.e. analog 0
//we now use our 'darkLevel' variable to trigger the LED ON.
if (iPhotoReading > iDark)
digitalWrite(iInterruptPin, HIGH); // turn InterruptPin on, else..
else
digitalWrite(iInterruptPin, LOW); // ..turn InterruptPin off
}//PhotocellTrigger()
/* called when the Interrupt is triggered - records door state */
void DoorOpen()
{
digitalWrite(iLEDpin, bLEDstate); // Turn door-open LED ON or OFF..
EEPROM.write(iAddressCount, bLEDstate);// ..record the incident..
iAddressCount++; // .. increment address count..
bLEDstate = !bLEDstate; // ..& change LED-state ON to OFF or vice versa
}//DoorOpen()
//END
Labels:
Arduino,
EEPROM,
functions,
interrupts,
LED,
libraries,
MINOR PROJECT,
photocell,
PROJECTS,
sensors,
serial monitor,
software,
thermistor
Tuesday, September 20, 2011
Task 30 -- the two Arduino hardware interrupts, & how to use them
Thursday, August 25, 2011
Task 15 - Byte output using 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
* 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
* 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
* 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
* 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.
* 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.
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
* 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
Subscribe to:
Posts (Atom)






