Monday, February 8, 2016

TCC test

Hello

Thursday, December 12, 2013

Arduino Template

Arduino Template
I'll do not wish to alarm you , but I got a new toy and like to share some sketchy ( pun intended) thoughts about it.
So here is the first installment of last of the series of sketches I patterned after Arduino IDE example named rightfully Blink.
The idea was to build a working Arduino application using just that _ Arduino Uno.

A curious user should be able to follow thru, dodging comments and finally realize how the template grew from simple to more complex but maintained ( I hope) the processing flow to just blink one LED.

Constructive commentaries are appreciated, comments from peanut gallery regrading the formatting, usage of delay function etc are cheerfully applauded too.

/*
Project
Basic Template version 7
Turn the L LED ( Uno ) on and off
Objective:
Demonstrate "Uno is alive" using build in L LED.
Identify / fix typos using compiler
Use Serial ( COM) as troubleshooting tool
Use preprocessor as troubleshooting tool
Use function with parameter checking and simple return values.
Introducing:
Symbolic names for variables
Digital I/O
delay function
Serial(COM)
Preprocessor
Development - comments
Function - using passed parameters
Using controls - "if"
Function - using simple true / false return value for error tracking
Code block - lines of code can be enclosed by { } curly brackets indicating a logical scope / relations and also allowing
to declare variables local to such scope.

Function - using numerical code returned to identify incorrect pin passed as parameter
Suggested exercise:
Change one occurrence of L_LED to LLED, compile and "fix" the error using IDE
Replace "fixed" delay of 1000 ms with symbol
- use IDE "find and replace"
- use meaningful name for symbol
Remove quotation mark from string "L LED off" - "L LED off" , compile and "fix" the error using IDE
Remove quotation mark from string "L LED off" - "L LED off , compile and "fix" the error using IDE
Note the compiler is helpful, but cannot tell the difference between originating and terminating character.
Remove DEBUG in #ifdef DEBUG line - #idfef , compile and "fix" the error using IDE
Replace turning the LED off with fLED function, comment tout unused code block.
Using controls - "if(statement)"
The "statement " is evaluated for true or false. If true next line / block is processed.
If false next code line / block is skipped.

Function - checking for return value using simple return value for error tracking
Code block - uncomment code block test, compile and "fix" the error
Function - using numerical code returned to identify incorrect pin passed as parameter
Pass incorrect pin value to the function, observe COM output.
Fix the line formatting.

*/
// project variables
// hardware Pin 13 has an L LED connected on most Arduino boards.
int L_LED = 13; // Pin 13 has an L LED connected on most Arduino boards.

// project definitions
#define DEBUG 0 // 0 skip compiling marked code 1 compile all code


// the setup routine runs once
void setup() {
// initialize the digital pin as an output.
pinMode(L_LED, OUTPUT); // unnecessary - all pins are output on power -up , but keeps orderly code
// initialize build-in serial (COM) port
Serial.begin(9600);
// indicate debugging mode
#if DEBUG
Serial.println("Debugging mode on...");
#endif
}

// the loop routine runs over and over again forever:

void loop() {
// replace this code block with a function
/*
digitalWrite(L_LED, HIGH); // software - set the L LED pin to HIGH
// hardware - turns the L LED on
#ifdef DEBUG // check for debugging mode
Serial.println("L LED on"); // output line to COM port
#endif
delay(1000); // processor does nothing for 1000 ms - wait
*/
// replacing simple function call with checking function return code
// fLED(L_LED,1,1000); // parameters int iPin, bool bOnOff, int iDelay)

if(fLED(L_LED,1,1000)) // function returns only 0 or 1
Serial.println("Success L LED is on "); // function returned 1 - true
else
Serial.println("Failure L LED is still off "); // function returned 0 - error code false
// replace this code block with a function capable of indicating incorrect pin parameter passed
/*
digitalWrite(L_LED, LOW); // software - set the L LED pin to LOW
// hardware - turns the L LED off
#if DEBUG
Serial.println("L LED off"); // output line to COM port
#endif
delay(1000); // processor does nothing for 1000 ms - wait
*/


{
int iError; // local variable - no need to initialize - set by function
// iError = fLED_pin(L_LED,0,1000); // comment out for test
iError = fLED_pin(0,0,1000); // 0 is invalid pin - serial port
if(iError == -1 ) // checking return value
Serial.println("Success L LED is off"); // function returned -1 OK
else
{ // returned error value code block
Serial.println("Failure L LED is still on ");
Serial.print("Incorrect pin number passed ");
Serial.print(iError);
}
}
}


// function to operate LED
// input parameters
// int iPin I/O pin
// bool bOnOff 0 turn LEDN off 1 turn LED on
// int iDelay keep LED state lenght
// output
// bool 0 function completed sucessfully
// bool 1 function failed

bool fLED(int iPin, bool bOnOff, int iDelay)
{
bool bError = true; // simple true / false error check true / 1 / high
// check input parameters for validity
if((iPin <2 ipin=""> 15)) // pin should be in range 2 thru 14 - for Uno
return !bError; // failed with complement of iError - false - 0 / low - indicating invalid pin
// returns without processing the rest of the code
// of success continue processing
//{ // code block test
int TEST = 100;
#if DEBUG
Serial.println("Wait time");
Serial.println(iDelay); // output wait time to COM - keep track of progress
#endif
digitalWrite(iPin, bOnOff); // software - set the L LED pin to HIGH
// hardware - turns the L LED on
#if DEBUG // check for debugging mode
Serial.println("output to L LED"); // output line to COM port
#endif
delay(iDelay); // processor does nothing for 1000 ms - wait
// { // code block test
TEST++;
return bError;
}
// ehnanced function to operate LED
// input parameters
// int iPin I/O pin
// bool bOnOff 0 turn LEDN off 1 turn LED on
// int iDelay keep LED state lenght
// output / returns
// int
//-1 function completed successfully
// != -1 ( not equal -1) function failed - indicate incorrect pin number , including pin 0 (serial port )

int fLED_pin(int iPin, bool bOnOff, int iDelay)
{
int iError = -1; // no error , success
// check input parameters for validity
if((iPin <2 ipin=""> 15)) // pin should be in range 2 thru 14 - for Uno
{
iError = iPin;
return iPin; // failed returning pin number to caller - what will happen if the line is
// commented out ?
}
// continue processing
//{ // code block test
int TEST = 100;
#if DEBUG
Serial.println("Wait time");
Serial.println(iDelay); // output wait time to COM - keep track of progress
#endif
digitalWrite(iPin, bOnOff); // software - set the L LED pin to HIGH
// hardware - turns the L LED on
#if DEBUG // check for debugging mode
Serial.println("output to L LED"); // output line to COM port
#endif
delay(iDelay); // processor does nothing for 1000 ms - wait
// { // code block test
TEST++;

return iError;
}

Saturday, May 30, 2009