Arduphil
Published © CC BY

Arduino House

I created a house with a light that gradually turns on a light if it is dark and an alarm connected to the door.

BeginnerShowcase (no instructions)977
Arduino House

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1

Software apps and online services

Arduino IDE
Arduino IDE
Blynk
Blynk

Story

Read more

Schematics

Fritzing

Code

casa.ino

Arduino
This do everything
/*
Little Home by Filippo
This code is licensed under Creative Commons BY license. If reuse you should link the page where you find this code.

Have a nice making!
*/

/**************************************************************
 *
 BLYNK comments
 
 * 1. Optional, but recommended.
 *    Connect additional USB-serial adapter to see the prints.
 *
 * 2. Edit auth token and upload this sketch.
 *
 * 3. Run the script (script located in "scripts" folder of library root,
 *    e.g. 'blynk-library/scripts') for redirecting traffic to server:
 *
 *      for Windows:
 *                     1. Open cmd.exe
 *                     2. write : (your way to blynk-ser.bat folder) example: "cd C:\blynk-library-0.3.1\blynk-library-0.3.1\scripts"
 *                     3. write : "blynk-ser.bat -c COM4" (where COM4 is port with your Arduino)
 *                     4. And press "Enter" , press "Enter" and press "Enter"
 *
 *      for Linux and OSX:
 *
 *                    ./blynk-ser.sh (may need to run with sudo)
 *
 *    You can specify port, baud rate, and server endpoint like this:
 *      ./blynk-ser.sh -c <serial port> -b <baud rate> -s <server address> -p <server port>
 *
 *    For instance :
 *      ./blynk-ser.sh -c /dev/ttyACM0 -b 9600 -s blynk-cloud.com -p 8442
 *
 *    Run blynk-ser.sh -h for more information
 *
 *    Be sure to select the right serial port (there may be multiple).
 *
 *    Attention!
 *        Arduino IDE may complain with "programmer is not responding".
 *        You need to terminate script before uploading new sketch.
 *
 * 4. Start blynking! :)
 *
 **************************************************************/

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>
#include <SimpleTimer.h>


SimpleTimer timer;


// You should get Auth Token in the Blynk App.
char auth[] = "a8659c1939b645a88afeca5928a0118a";
 //variables
int rpin=A0, doorsensor=A1, button=A2; //which pin read?
int value;  //read value
int parsed; //value after map
int setmin=790, setmax=960; //Values for fotoresistence (low=light)
unsigned long cmil, pmil=0; //current and previous millis
int trpin=5,tr;
int buzz=4;

class led{
 private:
   int pin;
   bool state;
   unsigned long current;   
   unsigned long previous;
  public:
   led(int a){pin=a;pinMode(pin, OUTPUT);state=0;previous=millis();};
   led(){};
    void set_on(){digitalWrite(pin,HIGH);state=1;};
    void set_off(){digitalWrite(pin,LOW);state=0;};
    void blynk(); 
    void set_pin(int a){pin=a;pinMode(pin,OUTPUT);};
};

class alarm{
  private:
  led alarmled;
  bool state;
  int sensor;
  public:
  alarm(int a, int b){alarmled.set_pin(a); sensor=b; state=0;};
  void set_on(){alarmled.blynk();state=1;};
  void set_off(){alarmled.set_off();state=0;};
  bool is_on(){return state;};
  bool warning(){if(state==1&&analogRead(sensor)>10) return 1; else return 0;};
};

void led::blynk(){
 current=millis();
if(state) if((current-previous)>800) {set_off(); previous=current;}
if(!state) if((current-previous)>800) {set_on(); previous=current;}
}

alarm door= alarm(9,doorsensor);
led test= led(13);


 void fakeloop(){
  test.blynk();
  if(digitalRead(button)) door.set_on(); else door.set_off();
  //light
  cmil=millis();
  value=analogRead(rpin);
  parsed=map(value,setmin,setmax,0,1023);
  parsed=constrain(parsed,0,1023); //no overscale
tr=map(parsed,0,1023,0,255);
analogWrite(trpin,tr);
if(door.warning()) {tone(buzz,440); Blynk.email("fili27182@gmail.com", "#arduino", "Warning, door's open!!"); Blynk.notify("Warning");
} else noTone(buzz);
//if(cmil-pmil>900){Serial.print("read:");Serial.println(value);Serial.print("write:");Serial.println(tr);Serial.println(); pmil=cmil;}
//delay(1);
}

void setup()
{
  SwSerial.begin(9600);
  Blynk.begin(auth);
  timer.setInterval(1000L, fakeloop);
}

void loop()
{
  Blynk.run();
  timer.run();
}

Credits

Arduphil

Arduphil

1 project • 5 followers

Comments

Add projectSign up / Login