This project is one of those thermal flashlight/camera/photography derivatives that uses a mlx90614 BCI datasheet: Error - Melexis to detect temperature and a 16x2 LCD datasheet: https://www.sparkfun.com/datasheets/LCD/ADM1602K-NSW-FBS-3.3v.pdf (acutally I think it's 5V but that's irrelevant) to display it. Along with a lightboard to display color changing light based on the temperature. This is on an Arduino UNO.
The problem I'm having is I want to use a push button as an enable so I can shut off the lights, LCD, and sensor when the button is not depressed while keeping the micro powered, there is a separate on/off switch for that.
My attempt (attached) used the pushbutton on pin 6 and an IF statement to only run the included code when depressed (high) I didn't actually try the push button but when I loaded with no input on pin six all the lights/display/etc functioned so I figured I had done something wrong.
If anyone can give me a pointer on how to use a pushbutton input to stop the lights and display from coming on that would be great.
#include <LiquidCrystal.h>
#include <i2cmaster.h>
#include "Wire.h"
const float lowReading = 60;
const float highReading = 120;
const unsigned char separatorCharacter = 255;
// initialize the library with the numbers of the interface pins
//12=RS,13=E,5=D4,4=D5,3=D6,2=D7
LiquidCrystal lcd(12, 13, 5, 4, 3, 2);
byte degreesymbol[8] = { //shape of degree symbol using binary
B11100,
B10100,
B11100,
B00000,
B00000,
B00000,
B00000,
B00000
};
const int buttonPin = 6;
int buttonState = 0;
void setup(){
pinMode (buttonPin, INPUT);
pinMode(9,OUTPUT); //red
pinMode(10,OUTPUT); //green
pinMode(11,OUTPUT); //blue
i2c_init(); //Initialise the i2c bus
PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups analog pins 4and5
lcd.begin(16,2); // 16x2 LCD datasheet https://www.sparkfun.com/datasheets/LCD/ADM1602K-NSW-FBS-3.3v.pdf
lcd.createChar(1, degreesymbol);// create character from binary above use lcd.write(1) to call
}
float normf(float x, float low, float high) {
float y = (x - low) * 255.f / (high - low);
if(y > 255) {
y = 255;
}
if(y < 0) {
y = 0;
}
return y;
}
void loop(){
buttonState = digitalRead(buttonPin);// read state of pb value
if (buttonState == HIGH);
{ int dev = 0x5A<<1;//address of sensor
int data_low = 0;
int data_high = 0;
int pec = 0; //packet error code
i2c_start_wait(dev+I2C_WRITE);
i2c_write(0x07); //read RAM
// read
i2c_rep_start(dev+I2C_READ); //repeat start
data_low = i2c_readAck(); //Read 1 byte and then send ack 16 bits of data divided into two halves
data_high = i2c_readAck(); //Read 1 byte and then send ack
pec = i2c_readNak();
i2c_stop(); // sends to sensor done until void loop begins
//This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps
double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
double tempData = 0x0000; // zero out the data
int frac; // data past the decimal point
// This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
tempData = (double)(((data_high & 0x007F) << 8) + data_low);
tempData = (tempData * tempFactor)-0.01;
float celcius = tempData - 273.15;
float fahrenheit = (celcius*1.8) + 32;
float state = normf(fahrenheit, lowReading, highReading);
// Regular ol' RGB LED:
int hue = map(state,0,255,359,(359*0.5)); // not the whole color wheel
setLedColorHSV(hue,1,1); //We are using Saturation and Value constant at 1
// LCD display
lcd.setCursor(0,0);
lcd.print(fahrenheit);
lcd.write("Fahrenheit");
lcd.setCursor(0,1);
lcd.print(celcius);
lcd.write("Celcius");
}
}
//Convert a given HSV (Hue Saturation Value) to RGB(Red Green Blue) and set the led to the color
// h is hue value, integer between 0 and 360
// s is saturation value, double between 0 and 1
// v is value, double between 0 and 1
//http://splinter.com.au/blog/?p=29
void setLedColorHSV(int h, double s, double v) {
//this is the algorithm to convert from RGB to HSV
double r=0;
double g=0;
double b=0;
double hf=h/60.0;
int i=(int)floor(h/60.0);
double f = h/60.0 - i;
double pv = v * (1 - s);
double qv = v * (1 - s*f);
double tv = v * (1 - s * (1 - f));
switch (i)
{
case 0: //red
r = v;
g = tv;
b = pv;
break;
case 1: //green
r = qv;
g = v;
b = pv;
break;
case 2: //green
r = pv;
g = v;
b = tv;
break;
case 3: //blue
r = pv;
g = qv;
b = v;
break; //blue
case 4:
r = tv;
g = pv;
b = v;
break;
case 5: //red
r = v;
g = pv;
b = qv;
break;
}
//set each component to a integer value between 0 and 255
int red=constrain(255-(int)255*r,0,255);
int green=constrain(255-(int)255*g,0,255);
int blue=constrain(255-(int)255*b,0,255);
setLedColor(red,green,blue);
}
//Sets the current color for the RGB LED
void setLedColor(int red, int green, int blue) {
analogWrite(9,red); //Red pin attached to 9
analogWrite(10,green); //green pin attached to 10
analogWrite(11,blue); //blue pin attached to 11
}
Thanks all. You are always so helpful!