Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

I will be maintaining my libraries here:
https://bit.ly/pATDBi

I am the lead developer for libraries 
that ship with the Wiring distribution. 
As per version 1.0 -
Wiring will support Arduino boards.
You are welcome to check it out!
https://wiring.org.co/download/
Constrain Utility for Arduino
Author:  Alexander Brevig
Contact: alexanderbrevig@gmail.com


Navigation


Description

I made a simple struct that has constraints that will persist.

The other method is to call constraint every time you need the variable (to be constrained).


Download, install and import

Download here: Attach:Constrain.zip
Put the Constrain folder in "hardware\libraries\".
In the Arduino IDE, create a new sketch (or open one) and select from the menubar "Sketch->Import Library->Constrain".
Once the library is imported, an "#include <Constrain.h>" line will appear at the top of your Sketch.


Declaration

Constrained<datatype> variableName( initialValue , minimumValue , maximumValue );


Creation and usage

Constrained<byte> index( 2 , 0 , 6 ); //start at index 2, do not get an index below 0 or above 6.

index += 4; //will result in index being 6
index = 10; //will result in index being 6
index = 0;  //will result in index being 0
index--;    //will result in index being 0


Example

SimpleConstrain

  1. /*
  2. ||
  3. || @author Alexander Brevig
  4. || @versin 1.0 Beta
  5. ||
  6. */
  7.  
  8. #include <Constrain.h>
  9.  
  10. Constrained<float> constrainedFloat( 0.2 , 0.0 , 1.0 );
  11. Constrained<byte> constrainedByte( 2 , 0 , 10 );
  12.  
  13. void setup(){
  14.   Serial.begin(9800);
  15.   Serial.println("We have two constrained variables. \n\tThe first is a float, constrained to be within [0,1].\n\tThe other is a byte that is constrained to be within [0,10]");
  16.   Serial.println("Setting the variables to 0");
  17.   //set/reset the variable
  18.   constrainedByte = 0;
  19.   constrainedFloat = 0;
  20.   Serial.println("Trying to make the variables exceed the constraint");
  21.   for(byte i=0; i<12; i++){
  22.     Serial.print(constrainedFloat.value); //access value
  23.     Serial.print(" ");
  24.     Serial.println(constrainedByte.value,DEC);
  25.     constrainedFloat += 0.1;
  26.     constrainedByte++;
  27.   }
  28.   Serial.println("Trying to make the variables go below the constraint");
  29.   for(byte i=0; i<12; i++){
  30.     Serial.print(constrainedFloat.value);
  31.     Serial.print(" ");
  32.     Serial.println(constrainedByte.value,DEC);
  33.     constrainedFloat -= 0.1;
  34.     constrainedByte--;
  35.   }
  36. }
  37.  
  38. void loop(){}

SimpleConstrain Serial Output

 
We have two constrained variables. 
	The first is a float, constrained to be within [0,1].
	The other is a byte that is constrained to be within [0,10]
Setting the variables to 0
Trying to make the variables exceed the constraint
0.00 0
0.10 1
0.20 2
0.30 3
0.40 4
0.50 5
0.60 6
0.70 7
0.80 8
0.90 9
1.00 10
1.00 10
Trying to make the variables go below the constraint
1.00 10
0.90 9
0.80 8
0.70 7
0.60 6
0.50 5
0.40 4
0.30 3
0.20 2
0.10 1
0.00 0
0.00 0


FAQ

Is a Constrained variable the same as a regular variable (byte,int etc) ?

No. A Constrained variable is actually a struct, but will behave like a regular variable in most ways.


Links


Information about this page

Part of AlphaBeta Utilities.
Last Modified: July 27, 2011, at 12:06 PM
By: AlphaBeta