Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
/*** * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation. * * DESCRIPTION * This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad * <henrik.ekblad@gmail.com> Vera Arduino Sensor project. * Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches. * * The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip. * The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected * to the LED negative terminal and the MOSFET Source pin is connected to ground. * * This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit. * * REVISION HISTORY * Version 1.0 - February 15, 2014 - Bruce Lacey * Version 1.1 - August 13, 2014 - Converted to 1.4 (hek) * Version 1.1a - August 14, 2014 - Added MotionSensor (Ed) * Version 1.1b - December 22, 2014 - Added DHT-Sensor (Ed) * Version 1.1c - December 29, 2014 - Added LED timeout (Ed) * Version 1.1d - Januari 4, 2015 - Updated lib to 1.4.1 (Ed) * ***/ #define SN "DimmableLED" #define SV "1.1d" #include <MySensors.h> #include <SPI.h> #include <DHT.h> #define Motion_PIN 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!) #define INTERRUPT Motion_PIN-2 // Usually the interrupt = pin -2 (on uno/nano anyway) #define LED_PIN 5 // Arduino pin attached to MOSFET Gate pin #define DHT_PIN 4 #define FADE_DELAY 30 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim) #define Motion_CHILD 1 // Id of the MotionSensor child #define Dimmer_CHILD 2 // Id of the DimmableLED child #define Hum_CHILD 3 // Id of the DHT-humidity child #define Temp_CHILD 4 // Id of the DHT-temperature child boolean lastMotion = false; MySensors gw(9,10); DHT dht; float lastTemp; float lastHum; static int currentLevel = 0; // Current dim level... long previousMillisDHT = 0; // stores last time DHT was updated long previousMillisLED = 0; // stores last time LED was updated long intervalDHT = 60000; // read DHT every .... long durationLED = 1800000; // Max 'On'-time for LED's (watchdog) MyMessage motionMsg(Motion_CHILD, V_TRIPPED); MyMessage dimmerMsg(Dimmer_CHILD, V_DIMMER); MyMessage lightMsg(Dimmer_CHILD, V_LIGHT); MyMessage humMsg(Hum_CHILD, V_HUM); MyMessage tempMsg(Temp_CHILD, V_TEMP); void setup() { // Serial.println( SN ); gw.begin( incomingMessage ); // Listen to dimmer-info from Vera dht.setup(DHT_PIN); // Register all sensors to gw (they will be created as child devices) gw.present( Motion_CHILD, S_MOTION ); gw.present( Dimmer_CHILD, S_DIMMER ); gw.present( Hum_CHILD, S_HUM ); gw.present( Temp_CHILD, S_TEMP ); gw.sendSketchInfo(SN, SV); // Pull the gateway's current dim level - restore light level upon sender node power-up gw.request( Dimmer_CHILD, V_DIMMER ); } void loop() { gw.process(); // Read digital motion value boolean motion = digitalRead(Motion_PIN) == HIGH; if (lastMotion != motion) { lastMotion = motion; gw.send(motionMsg.set(motion ? "1" : "0" )); // Send motion value to gw } /* // DHT22: unsigned long currentMillisDHT = millis(); if(currentMillisDHT - previousMillisDHT > intervalDHT) { previousMillisDHT = currentMillisDHT; float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else if (temperature != lastTemp) { lastTemp = temperature; gw.send(tempMsg.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum) { lastHum = humidity; gw.send(humMsg.set(humidity, 1)); Serial.print("H: "); Serial.println(humidity); } } */ // Limit LED 'On'-time. unsigned long currentMillis = millis(); if (( currentLevel > 0 ) && ((currentMillis - previousMillisLED) > durationLED)) { analogWrite( LED_PIN, 0 ); // Kill LED's after max 'On'-tijd gw.send(dimmerMsg.set(0)); currentLevel=0; } } void incomingMessage(const MyMessage &message) { if (message.type == V_LIGHT || message.type == V_DIMMER) { // Retrieve the power or dim level from the incoming request message int requestedLevel = atoi( message.data ); // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on] requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 ); // Clip incoming level to valid range of 0 to 100 requestedLevel = requestedLevel > 100 ? 100 : requestedLevel; requestedLevel = requestedLevel < 0 ? 0 : requestedLevel; // Serial.print( "Changing level from " ); // Serial.print( currentLevel ); // Serial.print( " to " ); // Serial.println( requestedLevel ); fadeToLevel( requestedLevel ); // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value... // gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0)); } } // This method provides a graceful fade up/down effect void fadeToLevel( int toLevel ) { if (toLevel > 0){ previousMillisLED = millis();} int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1; while ( currentLevel != toLevel ) { currentLevel += delta; analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) ); delay( FADE_DELAY ); } }
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
Google Docs - Exploit
1 min ago | 0.32 KB
Earn money on Changelly
CSS | 15 min ago | 0.32 KB
SwapEngine Bug [build 1.3]
29 min ago | 0.32 KB
December smells like money
CSS | 44 min ago | 0.32 KB
✅ Changelly.com Exploit
58 min ago | 0.32 KB
SelectionEdit.pas
Delphi | 8 hours ago | 27.66 KB
SelectionEdit.h
C++ | 8 hours ago | 1.42 KB
SelectionEdit.cpp
C++ | 8 hours ago | 17.22 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!