chebe: (South Park)
[personal profile] chebe
Someone asked me for this and I realised I'd never posted it. (I had dealt with reading the temp here but not the rest of it.) It's not fully done. The timing and lights and such could still do with some tweaking, but I guess that comes down to personalisation. If you'd like to try this project the physical construction is here, and what follows is my code. If you do give it a go I'd love to see how it turns out!


//Temperature sensor & LED scarf

//MCP9700, thermistor type temp sensor
//output: 0.5V @ 0C, 0.75V @ 25C
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures

//Vout = Temp(coefficient) * Temp(ambient) + V@0C
//Temp(ambient) = (Vout - V@0C)/Temp(coefficient)
//Vout = sensorReading * (3.3/1024)

//Temp(coefficient) = 10mV, reduce all to mV
//Temp(ambient) = ((sensorReading *(3300/1024)) - 500)/10

int tempSenPin = 0; //analog input 0/digital 14
int leds1 = 9; // line 1 of LEDs
int leds2 = 11; // line 2 of LEDs
float previousReading = 0;
float ambientTemp = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(tempSenPin, INPUT);
  pinMode(leds1, OUTPUT);
  pinMode(leds2, OUTPUT);

  // calibrate during the first five seconds
  // => get ambient temp
  float readings = 0;
  int count = 0;
  while (millis() < 5000) {
     readings = readings + tempC(analogRead(tempSenPin));
     count++;
     delay(100);
  }
  ambientTemp = readings/count;
}

void loop()
{
  float temp = tempC(analogRead(tempSenPin));
  //Serial.print(temp);
  //Serial.print(" : ");
  //Serial.println(ambientTemp);
   
  if(temp > ambientTemp){
    
    if(temp > previousReading){
      analogWrite(leds2, 0);
      
      int value = 0;
      for(value=0; value < 125; value++){
        analogWrite(leds1, value);
        delay(10);
      }
      for(value=125; value > 0; value--){
        analogWrite(leds1, value);
        delay(10);
      }
    }else{
      analogWrite(leds1, 0);
      int value = 0;
      for(value=0; value < 125; value++){
        analogWrite(leds2, value);
        delay(10);
      }
      for(value=125; value > 0; value--){
        analogWrite(leds2, value);
        delay(10);
      }
    }
  }
  
  previousReading = temp;
}

// convert analogRead value into degrees Celsius
float tempC(int reading){
  float voltage = reading * (3300 / 1024); 
  float tempC = (voltage - 500)/10;
  return tempC;
}
Page generated 2017-Oct-18, Wednesday 10:06 pm
Powered by Dreamwidth Studios