Skip to content

Instantly share code, notes, and snippets.

@next-marianmoldovan
Created February 3, 2015 16:46
Show Gist options
  • Save next-marianmoldovan/f3fbc4a9cbe375275297 to your computer and use it in GitHub Desktop.
Save next-marianmoldovan/f3fbc4a9cbe375275297 to your computer and use it in GitHub Desktop.
Safe, leds, speaker, sound sensor, ldr and temp/hum sensor
#include <Firmata.h>
#include <Boards.h>
#include <math.h>
#include <DHT.h>
#define RED 12
#define GREEN 13
#define SOUND_SENSOR A4
#define LIGHT_SENSOR 0
#define DHTPIN A2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float lightSensor;
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(SOUND_SENSOR, INPUT);
pinMode(LIGHT_SENSOR, INPUT);
Serial.begin(9600);
dht.begin();
}
// the loop function runs over and over again forever
void loop() {
//tone(10, 440, 100);
digitalWrite(RED, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(GREEN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(RED, LOW); // turn the LED off by making the voltage LOW
digitalWrite(GREEN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
}
else {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
}
int sensorValue = analogRead(LIGHT_SENSOR);
lightSensor = (float)(1023-sensorValue)*10/sensorValue;
Serial.print(sensorValue);
Serial.println(" light");
int soundValue = analogRead (SOUND_SENSOR);//use A0 to read the electrical signal
Serial.print(soundValue);
Serial.println(" sound");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment