/*
HC-SR05 pin Arduino pin Relay Board
vcc 5V X
Trig pin 9 X
Echo pin 8 X
Gnd Gnd Gnd
X pin 12 signal pin
X Vin +V
*/
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
// OLED display TWI address
// OLED display TWI address
#include <Buzzer.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <stdint.h>
#include <Adafruit_MLX90614.h>
#define OLED_RESET 4
Adafruit_SSD1306 display1(OLED_RESET);
Adafruit_SSD1306 display2(OLED_RESET);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
Adafruit_SSD1306 display(-1);
int laserPin = 11;
int s;
int state=0;
void setup() {
pinMode(laserPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
pinMode(8, INPUT);
pinMode(9, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
Serial.begin(57600);
pinMode(13, OUTPUT);
Serial.println("Adafruit MLX90614 test");
mlx.begin();
display1.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display2.begin(SSD1306_SWITCHCAPVCC, 0x3D); // initialize with the I2C addr 0x3C (for the 128x32)
}
void loop() {
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
digitalWrite(laserPin, HIGH); // Open the laser head
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(9, HIGH);
delayMicroseconds(5);
digitalWrite(9, LOW);
delayMicroseconds(5);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(8, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
if(inches<5){ // change number increse or decrese distanse eg 12 for 12 inches
// initialize and clear display
display1.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display1.clearDisplay();
display1.display();
display1.setTextSize(5);
display1.setTextColor(WHITE);
display1.setCursor(0,0);
display1.print(" ON");
display1.display();
digitalWrite(12, LOW);
delay(1000);
digitalWrite(12, HIGH);
delay(2000);
}
else {display1.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display1.clearDisplay();
display1.display();
display1.setTextSize(5);
display1.setTextColor(WHITE);
display1.setCursor(0,0);
display1.print(" Off");
display1.display();
digitalWrite(12, HIGH);
}
// Clear the buffer.
display2.clearDisplay();
// text display tests
display2.setTextSize(4);
display2.setTextColor(WHITE);
display2.setCursor(0,0);
//display.print("Ambient: ");//
//display.print(mlx.readAmbientTempF());//
//display.print(" c");//
display2.setCursor(0,0);
//display.print("Object: ");//
display2.print(round(mlx.readObjectTempF()));
display2.print(" F");
display2.display();
//delay(500); //
if (mlx.readObjectTempF()>98) {digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
//delay(500);//
}
long microsecondsToInches(long microseconds) {
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
Comments