Skip to content

Instantly share code, notes, and snippets.

@weldtype
Created January 28, 2017 17:14
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 weldtype/54943c732e6ab033b9f5ab8355571423 to your computer and use it in GitHub Desktop.
Save weldtype/54943c732e6ab033b9f5ab8355571423 to your computer and use it in GitHub Desktop.
// MLX90614の実験
// 2017/01/28 edy
// 2017/01/29 8x2行LCDを追加
//http://mag.switch-science.com/2014/11/05/i2c_lcd_breakout_int/
/***************************************************
This is a library example for the MLX90614 Temp Sensor
Designed specifically to work with the MLX90614 sensors in the
adafruit shop
----> https://www.adafruit.com/products/1748
----> https://www.adafruit.com/products/1749
These sensors use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include<Wire.h>
#include <Adafruit_MLX90614.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
#define LCDAdr 0x3e
byte contrast = 30; // コントラスト(0~63)
void setup()
{
Wire.begin();
mlx.begin();
Serial.begin(9600);
lcd_cmd(0b00111000); // function set
lcd_cmd(0b00111001); // function set
lcd_cmd(0b00000100); // EntryModeSet
lcd_cmd(0b00010100); // interval osc
lcd_cmd(0b01110000 | (contrast & 0xF)); // contrast Low
lcd_cmd(0b01011100 | ((contrast >> 4) & 0x3)); // contast High/icon/power
lcd_cmd(0b01101100); // follower control
delay(200);
lcd_cmd(0b00111000); // function set
lcd_cmd(0b00001100); // Display On
lcd_cmd(0b00000001); // Clear Display
//delay(100);
}
void loop()
{
float objTemp = mlx.readObjectTempC();
float dieTemp = mlx.readAmbientTempC();
Serial.print(objTemp);
Serial.print(",");
Serial.println(dieTemp);
lcd_clear();
lcd_setCursor(0, 0);
lcd_printFloat(objTemp, 5, 1);
lcd_setCursor(0, 1);
lcd_printFloat(dieTemp, 5, 1);
delay(500);
}
void lcd_cmd(byte x) {
Wire.beginTransmission(LCDAdr);
Wire.write(0b00000000); // CO = 0,RS = 0
Wire.write(x);
Wire.endTransmission();
}
void lcd_clear()
{
lcd_cmd(0b00000001);
}
void lcd_contdata(byte x) {
Wire.write(0b11000000); // CO = 1, RS = 1
Wire.write(x);
}
void lcd_lastdata(byte x) {
Wire.write(0b01000000); // CO = 0, RS = 1
Wire.write(x);
}
// 文字の表示
void lcd_printStr(const char *s) {
Wire.beginTransmission(LCDAdr);
while (*s) {
if (*(s + 1)) {
lcd_contdata(*s);
} else {
lcd_lastdata(*s);
}
s++;
}
Wire.endTransmission();
}
// 表示位置の指定
void lcd_setCursor(byte x, byte y) {
lcd_cmd(0x80 | (y * 0x40 + x));
}
void lcd_printInt(int num)
{
char int2str[10];
sprintf(int2str, "%d", num);
lcd_printStr(int2str);
}
//浮動小数点を文字列に変換して表示
//http://garretlab.web.fc2.com/arduino/reverse_lookup/index.html#string-float_to_string
void lcd_printFloat(float num, int width, int prec)
{
char float2str[10];
dtostrf(num, width, prec, float2str);
lcd_printStr(float2str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment