Menu

Temperature sensor Arduino Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
//LiquidCrystal_I2C lcd(0x3F, 16, 2);


#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11

static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );

 

/*
* Initialize the serial port.
*/
void setup( )
{
Serial.begin( 9600);
}

 

/*
* Poll for a measurement, keeping the state machine alive. Returns
* true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );

/* Measure once every four seconds. */
if( millis( ) - measurement_timestamp > 3000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return( true );
}
}

return( false );
}

 

/*
* Main program loop.
*/
void loop( )
{
float temperature;
float humidity;

/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
if( measure_environment( &temperature, &humidity ) == true )
{
Serial.print( "T = " );
Serial.print( temperature, 1 );
Serial.print( " deg. C, H = " );
Serial.print( humidity, 1 );
Serial.println( "%" );


// initialize the LCD,
lcd.begin();

// Turn on the blacklight and print a message.
lcd.backlight();

 

 

lcd.clear();
lcd.print("Temp = ");
lcd.print(temperature);
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print("Humidity= ");
lcd.print(humidity);
//lcd.print(millis() / 1000);
delay(500);

RGB controlled by Ultrasonic Sensor Arduino Code

#define BLUE 3
#define GREEN 5
#define RED 6


#include "SR04.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.begin();
lcd.backlight();
Serial.begin(9600);
delay(1000);


pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);

}

int redValue;
int greenValue;
int blueValue;

void loop() {
#define delayTime 10 // fading time between colors


lcd.clear();
a=sr04.Distance();
Serial.print(a);
lcd.print(a);

if (a>20 && a<30)
{analogWrite(BLUE, 10);
analogWrite(GREEN, 255);
analogWrite(RED, 255);}
else if (a<=20&&a>10)

{analogWrite(BLUE, 255);
analogWrite(GREEN, 10);
analogWrite(RED, 255);}

else if (a<=10&&a>0)
{analogWrite(BLUE, 255);
analogWrite(GREEN, 255);
analogWrite(RED, 10);}

else
{analogWrite(BLUE, 255);
analogWrite(GREEN, 255);
analogWrite(RED, 255);}

Serial.println("cm");
// lcd.println("cm");
delay(100);
}