Skip to content

Instantly share code, notes, and snippets.

@zach-c-d
Last active February 3, 2016 18:50
Show Gist options
  • Save zach-c-d/21b5f64d2ae5334b565f to your computer and use it in GitHub Desktop.
Save zach-c-d/21b5f64d2ae5334b565f to your computer and use it in GitHub Desktop.
Temp Sensor 3
//Take analogue readings from four temperature sensors (LM35). When temperature increases sufficiently (hand touches
//sensor), light up one LED. Each sensor has it's own LED.
//cycle count
int cycle = 0;
//variables for storing the reading, then converting reading to celsius
int reading0;
int reading1;
int reading2;
int reading3;
float tempC0;
float tempC1;
float tempC2;
float tempC3;
//variables for the delta_tempData values
float old_tempC0;
float old_tempC1;
float old_tempC2;
float old_tempC3;
//compares the difference between current tempData and old_tempData
float delta_tempC0;
float delta_tempC1;
float delta_tempC2;
float delta_tempC3;
//store the previous delta value for use in average
float old_delta0;
float old_delta1;
float old_delta2;
float old_delta3;
//averages the last 2 delta values
float av_delta0;
float av_delta1;
float av_delta2;
float av_delta3;
//sum of last 2 delta values
float sum_delta0;
float sum_delta1;
float sum_delta2;
float sum_delta3;
//i counts the cycles until the 4th cycle. Then resests and returns av_delta(values) in the Serial port
int i = 0;
//i0 is the value of brightness of LED0, and so on with i1, i2, i3
int i0 = 0;
int i1 = 0;
int i2 = 0;
int i3 = 0;
//initialize the tempSensor Pins
int tempPin0 = 0;
int tempPin1 = 1;
int tempPin2 = 2;
int tempPin3 = 3;
//assign the LED pins
int LED0 = 13;
int LED1 = 12;
int LED2 = 8;
int LED3 = 7;
void setup()
{
analogReference(INTERNAL);
Serial.begin(9600);
//initialize LED pins
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop()
{
cycle ++;
//store tempData as old data for delta values
old_tempC0 = tempC0;
old_tempC1 = tempC1;
old_tempC2 = tempC2;
old_tempC3 = tempC3;
//store old delta values for delta average
old_delta0 = delta_tempC0;
old_delta0 = delta_tempC1;
old_delta0 = delta_tempC2;
old_delta0 = delta_tempC3;
//read data for pin0
reading0 = analogRead(tempPin0);
tempC0 = reading0 / 9.31; //convert reading to celsius by x/9.31
//read data for pin1
reading1 = analogRead(tempPin1);
tempC1 = reading1 / 9.31;
//read data for pin2
reading2 = analogRead(tempPin2);
tempC2 = reading2 / 9.31;
//read data for pin3
reading3 = analogRead(tempPin3);
tempC3 = reading3 / 9.31;
if (cycle > 4) {
//delta values of tempData and old_tempData
delta_tempC0 = tempC0 - old_tempC0;
delta_tempC1 = tempC1 - old_tempC1;
delta_tempC2 = tempC2 - old_tempC2;
delta_tempC3 = tempC3 - old_tempC3;
//if temperature is rising, fade on assigned LED. Else if temperature is dropping, fade off LED
if ((delta_tempC0) >= 0.18) {
i0 += 10;
digitalWrite(LED0, HIGH);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
} else if (-0.12 >= (delta_tempC0)) { //
i0 -= 10;
digitalWrite(LED0, LOW);
}
if ((delta_tempC1) >= 0.18) {
i1 += 10;
digitalWrite(LED0, LOW);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
} else if (-0.12 >= (delta_tempC1)) { //
i1 -= 10;
digitalWrite(LED1, LOW);
}
if ((delta_tempC2) >= 0.18) {
i2 += 10;
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
} else if (-0.12 >= (delta_tempC1)) { //
i2 -= 10;
digitalWrite(LED2, LOW);
}
if ((delta_tempC3) >= 0.18) {
i3 += 10;
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
} else if (-0.12 >= (delta_tempC3)) { //
i3 -= 10;
digitalWrite(LED3, LOW);
}
i = i + 1;
if (i / 4 == 1) {
i = 0;
sum_delta0 = delta_tempC0 + old_delta0;
/*
//only print av_delta every four cycles
//print all delta averages
Serial.println("averages");
Serial.println(av_delta0);
Serial.println(av_delta1);
Serial.println(av_delta2);
Serial.println(av_delta3);
Serial.println("");*/
}
}
//average the current delta and the previous delta
av_delta0 = (delta_tempC0 + old_delta0) / 2;
av_delta1 = (delta_tempC1 + old_delta1) / 2;
av_delta2 = (delta_tempC2 + old_delta2) / 2;
av_delta3 = (delta_tempC3 + old_delta3) / 2;
//print all results
Serial.println("results");
Serial.println(tempC0);
Serial.println(tempC1);
Serial.println(tempC2);
Serial.println(tempC3);
Serial.println(""); //empty line to neatin the print
//print all changes (delta values)
Serial.println("changes");
Serial.println(delta_tempC0);
Serial.println(delta_tempC1);
Serial.println(delta_tempC2);
Serial.println(delta_tempC3);
Serial.println("");
//delay to make serial communication easier to read
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment