Categories

Archive

Disclaimer

De meningen ge-uit door medewerkers en studenten van de TU Delft en de commentaren die zijn gegeven reflecteren niet perse de mening(en) van de TU Delft. De TU Delft is dan ook niet verantwoordelijk voor de inhoud van hetgeen op de TU Delft weblogs zichtbaar is. Wel vindt de TU Delft het belangrijk - en ook waarde toevoegend - dat medewerkers en studenten op deze, door de TU Delft gefaciliteerde, omgeving hun mening kunnen geven.

More efficient code to get LEDs working according to temperature

The previous code was not very neat. For efficiency and modularity it is important to work less with constant values in the main code, but with variables and arrays to adapt and tweak the code more easily later. This will also be easier to test the final prototype in the end. Code is in black and comments are in grey. For the code see below.

 

/* code created by Simone Koch on 10/11/2011
This program uses a temperature sensor to measure a person’s warmth and the change in warmth.
It takes into account large changes be redefining the default temperature (which gets constant after a while)
and reacts according to this by illuminating a certain amount of LEDs.
Please take into account that this program is under development. */

// constants
const int tempAnalogInPin = A0; // input of the temperature sensor
const int maxN = 4; // # of measurements for defValue
const int nul = -5; // margin used for deltaX
const int low = 4; // margin used for deltaX
const int mid = 15; // margin used for deltaX
const int high = 25; // margin used for deltaX
const int bound = high; // boundary of deltaX before a new defValue will be calculated

//variables
int n = 0; // # of Values measured for calculating defValue
int defValue = 0; // default value measured
int newDefault = 0; // temporary value while calculating defValue
int tempeArray[maxN]; // temperature measurements
int x = 0; // current temperature value
int deltaX = 0; // the difference measured to defValue (NOT absolute)

void setup() {
   Serial.begin(9600);
   pinMode(13, OUTPUT);
   pinMode(12, OUTPUT);
   pinMode(11, OUTPUT);
}

void loop() {
   x = analogRead(tempAnalogInPin);
   Serial.println(x); 
   if ((x > (defValue + bound)) | (x < (defValue – bound))) {  // if current value differs too much from defValue
     adjustDefault();
   }
   else {
     calcValues();
   }
   delay(1000);  // delay 1 second
}

void adjustDefault() {
   lampjes(0,0,0);
   Serial.println("Adjust Default" );
   n=0;
   while (n < maxN) {  // while amount of measurements < max measurements
     tempeArray[n] = x;  // store on place n the current value
     n++;
     delay(1000);
   }
   newDefault = 0;
   for (int i = 0; i <= maxN; i++) {  // for i = 0, until maxN, do i++
     newDefault = newDefault + tempeArray[i]; // add the current values
   }
   defValue = newDefault / (maxN + 1);  
   Serial.println(defValue);
}

void lampjes(int a, int b, int c) { //  amount of LEDs on (green, orange, red)
   switch (a) {  
     case 0: digitalWrite(13, LOW);  // green = low
     break;
     case 1: digitalWrite(13, HIGH);  // green = high
     break;
   }
   switch (b) {
     case 0: digitalWrite(12, LOW);  // orange = low
     break;
     case 1: digitalWrite(12, HIGH);  // orange = high
     break;
   }
   switch (c) {
     case 0: digitalWrite(11, LOW);  // red = low
     break;
     case 1: digitalWrite(11, HIGH);  // red = high
     break;
   }
}

void calcValues() { // give output (show LEDs on/of)
   deltaX = x – defValue; // current value – default value
   Serial.println(" Delta Values:" );  
   Serial.println(deltaX);
   if (deltaX <= nul) { // if deltaX <= lowest margin, put off all LEDs
     lampjes(0,0,0);
   }
   if (deltaX > nul) { // if deltaX > lowest margin, put only on green LED
     lampjes(1,0,0);
   }
   if (deltaX > low) { // if deltaX > low margin, put on only green and orange LEDs
     lampjes(1,1,0);
   }
   if (deltaX > mid) { // if deltaX > middle margin, put on green, orange and red LEDs
     lampjes(1,1,1);
   }

Be Sociable, Share!

Leave a Reply

© 2011 TU Delft