I am working on this project for class. So far I only have the LED working on button press and the movement of the servo, while I hold the button. So if I hold the red button, it will move left until I let go or it reaches the end. Same for the yellow button just goes opposite direction. I want to add a DHT11 temp/humidity sensor to it. I am not sure how to write the code for that. So the temperature sensor will receive the data and then the servo motor will move to the correct position displaying the temperature on a scale. The buttons are used to change between temperature and humidity. So when I push the red button, the servo will display temperature, when I push the yellow button, the servo will display the humidity. This is what I have so far:
#include <Servo.h>
const int buttonPin = D1;
const int buttonPin2 = D2;
int buttonState = 0;
int buttonState2 = 0;
const int ledRed = D5;
const int ledYellow = D6;
1. This is the hardware setup.
Figure-1: hardware setup
2. This is the Display Scale for Temperature and Humidity driven by Servo Motor.
Figure-2: Temp and Humidity scale
3. Let us upload the following codes (taken from IDE Examples and edited). Observe that the the servo swings 0 - 180 deg and then 180 - 0 deg; after that, the Temp/humidity reading comes on the Serial Monitor.
#include <SimpleDHT.h>
int pinDHT11 = 16;
SimpleDHT11 dht11;
#include <Servo.h>
Servo myservo;
void setup()
{
Serial.begin(115200);
myservo.attach(2); // attaches the servo on GIO2 to the servo object
}
void loop()
{
moveServo();
showTH();
}
void moveServo()
{
int pos;
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void showTH()
{
// start working...
Serial.println("=================================");
Serial.println("Sample DHT11...");
// read without samples.
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
return;
}
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" H");
// DHT11 sampling rate is 1HZ.
delay(1500);
}
4. Sketch to position the shaft of Servo in proportion (un-calibrated) to Humidity and Temperature (measured by DHT11) when button H or button T is closed.
#include <SimpleDHT.h>
int pinDHT11 = 16;
SimpleDHT11 dht11;
byte temperature = 0;
byte humidity = 0;
#include <Servo.h>
Servo myservo;
void setup()
{
Serial.begin(115200);
myservo.attach(2); // attaches the servo on GIO2/D4 to the servo object
pinMode(4, INPUT_PULLUP); //H
pinMode(5, INPUT_PULLUP); //T
}
void loop()
{
showTH(); //shoe Temperature and Humidity on Serial Monitor
if (digitalRead(4) == LOW) //D2; button Humidity
{
moveServoH(humidity); //show humidity on Disal type Scale of Fig-2
}
else
{
if (digitalRead(5) == LOW) //D1 ; button Temp
{
moveServoT(temperature); //show Temperature on Dial type Scale of Fig-2
}
}
}
void moveServoH(byte x)
{
x = map(x, 10, 100, 90, -90);
myservo.write(x); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
void moveServoT(byte y)
{
y = map(y, 10, 100, 90, -90);
myservo.write(y); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
void showTH()
{
// start working...
Serial.println("=================================");
Serial.println("Sample DHT11...");
// read without samples.
//byte temperature = 0;
//byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess)
{
Serial.print("Read DHT11 failed, err=");
Serial.println(err);
delay(1000);
return;
}
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" H");
// DHT11 sampling rate is 1HZ.
delay(1500);
}