Pages: [1]   Go Down
Author Topic: Constrain Utility. Making constrained variables.  (Read 998 times)
0 Members and 1 Guest are viewing this topic.
Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've made a new utility called Constrain. It is inspired by this post.

This will help creating variables that are constrained throughout the program. (At least throughout the scope of the variable.)

[size=14]Example Sketch:[/size]
Quote

 
/*
||
|| @author Alexander Brevig
|| @versin 1.0 Beta
||
*/

#include <Constrain.h>

Constrained<float> constrainedFloat( 0.2 , 0.0 , 1.0 );
Constrained<byte> constrainedByte( 2 , 0 , 10 );

void setup(){
  Serial.begin(9800);
  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]");
  Serial.println("Setting the variables to 0");
  //set/reset the variable
  constrainedByte = 0;
  constrainedFloat = 0;
  Serial.println("Trying to make the variables exceed the constraint");
  for(byte i=0; i<12; i++){
    Serial.print(constrainedFloat.value); //access value
    Serial.print(" ");
    Serial.println(constrainedByte.value,DEC);
    constrainedFloat += 0.1;
    constrainedByte++;
  }
  Serial.println("Trying to make the variables go below the constraint");
  for(byte i=0; i<12; i++){
    Serial.print(constrainedFloat.value);
    Serial.print(" ");
    Serial.println(constrainedByte.value,DEC);
    constrainedFloat -= 0.1;
    constrainedByte--;
  }
}

void loop(){}
 


[size=14]Example Serial Output:[/size]
Quote
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
« Last Edit: April 21, 2009, 07:17:32 pm by AlphaBeta » Logged

Pages: [1]   Go Up
Jump to: