Go Down

Topic: Arduino Template (Read 3548 times) previous topic - next topic

Vaclav

Dec 12, 2013, 06:28 pm Last Edit: Dec 13, 2013, 08:42 am by AWOL Reason: 1
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;
}
Deleted
Moderator edit: Deleted code restored.

el_supremo

Surely you know by now that you should put your code in a code block.
It would also make your code more readable if you first used the IDE menu Tools|Auto Format to indent your code for you.

Pete
Don't send me technical questions via Private Message.

nickgammon

@Vaclav: You've complained before that when someone posts a question, the first thing people do is ask them to use code tags.

You still persist in not doing so, even though you have now made 188 posts.

I can only assume you are now trying to bait us, and as such are likely to be banned.

Please use code tags.

Read this before posting a programming question

How to use this forum
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

AWOL

Have you really been programming for thirty plus years?
So many common basic C conventions ignored.
Debug code that has the potential for introducing more bugs than it helps find is doubly useless.

Code: [Select]
  // unnecessary - all pins are output on power -up
Really?

Code: [Select]
if((iPin <2 ) | (iPin > 15))
Bitwise OR.


Oh, and my scrolling finger hurts.

nickgammon

Code: [Select]

  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


Let me get this straight. The variable bError is true when there is no error but false when there is an error is that it?

Joe: "Is it raining?"
Vaclav: "True"
Joe: "I'll get my umbrella then."
Vaclav: "No, no, when I say 'raining == true' I mean 'it is not raining' ".
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

AWOL

A final warning for Vaclav:
You have repeatedly been asked to show a simple courtesy to other forum members, and you have chosen to ignore those requests.
From now on, posts which do not adhere to forum guidelines will simply be deleted.
If I have to do this to more than twice, your account goes too.

I hope that this is perfectly clear.

Go Up