Skip to content

Instantly share code, notes, and snippets.

@Samathy
Created April 2, 2017 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Samathy/c00436887e83cfa55103c21d1b6ea3ff to your computer and use it in GitHub Desktop.
Save Samathy/c00436887e83cfa55103c21d1b6ea3ff to your computer and use it in GitHub Desktop.
#include <CapacitiveSensor.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define CAPSENSORPIN 3
#define CAPCPIN 2
#define THERMRESISTORVAL 10000
#define THERMISTORPIN A4
#define NEOPIN 5
#define NEONUMPIXELS 4
#define NEOBASELINE 30 //Baseline brightness
#define TEMPNOMINAL 19
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NEONUMPIXELS, NEOPIN, NEO_GRB + NEO_KHZ800);
CapacitiveSensor cs_5_2 = CapacitiveSensor(CAPCPIN,CAPSENSORPIN); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
int currentRed;
int currentGreen;
int currentBlue;
int neofactor;
int readTemp() //Read the temperature from the thermistor. Returns temp in degrees C
{
float reading;
reading = analogRead(THERMISTORPIN);
// convert the value to resistance
reading = (1023 / reading) - 1;
reading = THERMRESISTORVAL / reading;
Serial.print("Thermistor resistance ");
Serial.println(reading);
float steinhart;
steinhart = reading / THERMRESISTORVAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= 3950; // 1/B * ln(R/Ro)
steinhart += 1.0 / (25 + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
return steinhart;
}
long readCapacitance() //Read the capacitance from the proximity sensor. Return value.
{
long prox = cs_5_2.capacitiveSensor(30);
return prox;
}
void changeAllPixels(int r, int g, int b)
{
for (int i=0; i<NEONUMPIXELS;i++)
{
pixels.setPixelColor(i, pixels.Color(r,g,b-20));
}
pixels.show();
delay(10);
return;
}
float percent(int val, int perc)
{
return val * perc / 100;
}
void setup()
{
Serial.begin(9600);
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
pixels.begin(); // This initializes the NeoPixel library.
neofactor = 10;
}
void loop()
{
int temperature = readTemp();
int proximity = readCapacitance();
if (proximity >= (neofactor+20))
{
if (NEOBASELINE+neofactor+20 <= 210)
{
neofactor = neofactor+20;
}
}
else if(proximity < (neofactor - 20))
{
neofactor = neofactor -20;
}
Serial.print("neofactor is:");
Serial.println(neofactor);
Serial.println(NEOBASELINE+neofactor+currentRed);
Serial.println("Values are:");
Serial.print("Temperature: ");
Serial.println(temperature, DEC);
Serial.print("Capacantance");
Serial.println(proximity);
if (temperature <= TEMPNOMINAL-1)
{
currentRed = TEMPNOMINAL-temperature;
changeAllPixels(NEOBASELINE+neofactor+currentRed,0,NEOBASELINE+currentBlue);
}
else if (temperature >= TEMPNOMINAL+1)
{
currentBlue = TEMPNOMINAL+temperature;
changeAllPixels(NEOBASELINE+currentRed,0,NEOBASELINE+neofactor+currentBlue);
}
else
{
changeAllPixels(NEOBASELINE+neofactor+(TEMPNOMINAL - 10),0,NEOBASELINE+neofactor+(TEMPNOMINAL- 10));
}
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment