Skip to content

Instantly share code, notes, and snippets.

@mathieubolla
Last active December 15, 2015 10:38
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 mathieubolla/5246609 to your computer and use it in GitHub Desktop.
Save mathieubolla/5246609 to your computer and use it in GitHub Desktop.
// Reads temperature from TMP36 sensor (from SparkFun electronics) on Arduino Pro Mini 3.3V 8MHz
// Reports the readings to serial port
// Plug Vcc of sensor to digital 12
// Plug Gnd of sensor to gnd of the board
// Plug Vout of sensor to analog 0
// Monitoring diode (digital 13) blinks while sensor is being read
const int power = 12;
const int monitor = 13;
const int analog = A0;
const int serialRate = 9600 * 4;
void setup() {
// Beware: This is for Arduino Pro Mini 8MHz
// most boards do not need this division
Serial.begin(serialRate / 2);
pinMode(power, OUTPUT);
}
void loop() {
// Power up the thermo sensor
digitalWrite(power, HIGH);
// Power up monitor led
digitalWrite(monitor, HIGH);
// Wait for consistent ADC reading
delay(150);
// Read sensor value
int value = analogRead(analog);
digitalWrite(power, LOW);
digitalWrite(monitor, LOW);
// Print values to serial
// Formula is 10mV per degree celsius for this sensor
// Arduino reference is 3.3 on this board
// ADC range is 0 to 1023 (10 bits)
Serial.println(100 * value * (3.3 / 1023));
// Flush serial to get more consistent decoding at the other end of serial link
Serial.flush();
// Wait for next loop
delay(2000);
}
// Processing script (http://processing.org) to read thermal reports on serial port
// from Arduino code above. Displays temps as a visual thermometer. Fast, and ugly...
import processing.serial.*;
Serial port;
void setup() {
size(120, 600);
port = new Serial(this, "/dev/tty.usbserial-A601EQIH", 9600*4);
}
void draw() {
if (port.available() > 0) {
String input = port.readString();
if (input != null) {
if (input.length() == 7) {
float temperature = float(input.substring(0, 6));
println(temperature+"oC");
if (temperature > 30.0) {
temperature = 30.0;
}
if (temperature < 10.0) {
temperature = 10.0;
}
float hauteur = (temperature - 10) * 600 / 20;
line(21.0, "21°C");
line(19.0, "19°C");
line(23.0, "23°C");
fill(255, 255, 255);
rect(120 / 2 - 15, 10, 30, 590, 15, 15, 0, 0);
fill(255, 0, 0);
rect(120 / 2 - 15, 600 - hauteur, 30, hauteur, 15, 15, 0, 0);
}
}
}
}
void line(float reference, String texte) {
float hauteur = (reference - 10) * 600 / 20;
line(120 / 2 + 15, 600 - hauteur, 120 / 2 + 30, 600 - hauteur);
fill(0, 0, 0);
text(texte, 120 / 2 + 25, 600 - hauteur - 10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment