Attachment 'Light_Time_LDR_Fade.ino'

Download

   1 /*
   2  Author  : Loe Feijs and TU/e 2013.
   3  This example shows how to control a LED (LED via 470 Ohm between pin 9 and GND). 
   4  The LED goes on when the environment's bright-ness changes 
   5  suddenly. The averaged brightness is calculated by a moving 
   6  average with time constant of 20 seconds. The LDR is connect 
   7  between +5V and pin A0. The LDR has a pull-down resistor of 
   8  22k between pin A0 and GND. The LED fades out after 10 seconds. 
   9  It takes 2 seconds to fade out. 
  10  
  11  Author  : P.Peters
  12  Date    : 20131202
  13  
  14  Modified code such that it fits my test plug
  15  pin 9 is used as GND, and connects to the LED and button.
  16  pin 10 has a 470 Ohm resistor to the LED
  17  pin 11 has a 470 Ohm resistor to the other side of the button
  18  In order to operate, pin 9 must be 0, and pin 11 must have pullup
  19  Also added a fade in in the state machine when led switches on
  20  */
  21 
  22 //FSM states:
  23 #define OFF 0
  24 #define ON  1
  25 #define FADINGOUT 2
  26 #define FADINGIN 3
  27 
  28 //connect LED, LDR:
  29 #define gndPin 9
  30 #define ledPin 10
  31 #define buttonPin 11
  32 #define ldrPin A0
  33 
  34 #define onTime 400 //times 10 milliseconds
  35 #define fadeTime 200 //times 10 milliseconds
  36 
  37 int state = OFF;
  38 int timer = 0;
  39 boolean LDRevent = false;
  40 
  41 void LEDstep(){
  42   switch (state){
  43   case OFF: 
  44     //    if (digitalRead(buttonPin) == LOW) {
  45     if (LDRevent){
  46       // prepare timer for delay
  47       timer = fadeTime;
  48       // switch state
  49       state = FADINGIN;
  50     } 
  51     break;
  52   case ON: 
  53     if (timer > 0){
  54       timer--;
  55     } 
  56     else {
  57       // prepare timer for fading out
  58       timer = fadeTime;
  59       // switch state
  60       state = FADINGOUT;
  61     } 
  62     break;
  63   case FADINGOUT:
  64     if (timer > 0){
  65       //fade from 250 to 0 during fadeTime steps
  66       timer--;
  67       analogWrite(ledPin,10*timer/8); 
  68     }  
  69     else {
  70       // switch state
  71       state = OFF;
  72       LDRevent = false;
  73       // state transition actions
  74       analogWrite(ledPin,0);
  75     }
  76     break;
  77   case FADINGIN:
  78     if (timer > 0){
  79       //fade from 0 to 255 during fadeInTime steps
  80       timer--;
  81       analogWrite(ledPin,250-10*timer/8); 
  82     }  
  83     else {
  84       // switch state
  85       state = ON;
  86       timer = onTime;
  87       // state transition actions
  88       analogWrite(ledPin,255);
  89     }
  90     break;
  91   }
  92 }
  93 
  94 float avg;  
  95 
  96 void setup()  { 
  97   pinMode(gndPin,OUTPUT);
  98   digitalWrite(gndPin,0);
  99   pinMode(ledPin, OUTPUT); 
 100   pinMode(buttonPin, INPUT_PULLUP);
 101   pinMode(ldrPin, INPUT);
 102   avg = (float)analogRead(ldrPin);
 103 } 
 104 
 105 void LDRstep(){
 106   //LDR from +5V to pin A0, pulldown 22k to GND
 107   int v = analogRead(ldrPin);
 108   avg = 0.9995 * avg + 0.0005* (float)v;
 109   float delta = 20.0; // 20 times 10msec 
 110   float dif = (float)v - avg;
 111   if (abs(dif) > delta)
 112     LDRevent = true;
 113 }
 114 
 115 void loop()  { 
 116   LDRstep();
 117   LEDstep();
 118   delay(10); // 10 ms delay between steps
 119 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2015-12-20 02:08:32, 1586.5 KB) [[attachment:AlgorithmsAndDataStructures.pdf]]
  • [get | view] (2015-12-20 02:08:32, 327.3 KB) [[attachment:AlgorithmsAndDataStructures_examples.zip]]
  • [get | view] (2015-12-20 02:08:32, 1186.9 KB) [[attachment:ArduinoAndSerialCommunication.pdf]]
  • [get | view] (2015-12-20 02:08:32, 3212.2 KB) [[attachment:ArduinoAndSerialCommunication.pptx]]
  • [get | view] (2015-12-20 02:08:32, 2650.4 KB) [[attachment:Arduino_sheets.pdf]]
  • [get | view] (2015-12-20 02:08:32, 578.3 KB) [[attachment:ArraysAndFunctions.pdf]]
  • [get | view] (2015-12-20 02:08:32, 18.2 KB) [[attachment:ArraysAndFunctions_examples.zip]]
  • [get | view] (2015-12-20 02:08:32, 1.0 KB) [[attachment:DrawingCreatures.zip]]
  • [get | view] (2015-12-20 02:08:32, 583.5 KB) [[attachment:FSM_sheets.pdf]]
  • [get | view] (2015-12-20 02:08:33, 3768.5 KB) [[attachment:GSArduino.pdf]]
  • [get | view] (2015-12-20 02:08:33, 151.8 KB) [[attachment:GUIExamples.zip]]
  • [get | view] (2015-12-20 02:08:32, 1248.8 KB) [[attachment:Graphics.pdf]]
  • [get | view] (2015-12-20 02:08:33, 4715.7 KB) [[attachment:GraphicsIntroduction.pdf]]
  • [get | view] (2015-12-20 02:08:33, 2.3 KB) [[attachment:Graphics_Exercises.zip]]
  • [get | view] (2015-12-20 02:08:33, 344.9 KB) [[attachment:GuiAndInteractivity.pdf]]
  • [get | view] (2015-12-20 02:08:33, 324.3 KB) [[attachment:Introduction.pdf]]
  • [get | view] (2015-12-20 02:08:33, 353.5 KB) [[attachment:IoT.pdf]]
  • [get | view] (2015-12-20 02:08:33, 1023.3 KB) [[attachment:IoT2013Q3.pdf]]
  • [get | view] (2015-12-20 02:08:33, 46.6 KB) [[attachment:IoT2013Q3Examples.zip]]
  • [get | view] (2015-12-20 02:08:33, 113.7 KB) [[attachment:IoTExamples.zip]]
  • [get | view] (2015-12-20 02:08:33, 258.2 KB) [[attachment:IoT_Examples.zip]]
  • [get | view] (2015-12-20 02:08:33, 1055.1 KB) [[attachment:IoT_lecture.pdf]]
  • [get | view] (2015-12-20 02:08:33, 2215.0 KB) [[attachment:IoT_lecture.pptx]]
  • [get | view] (2015-12-20 02:08:33, 2.7 KB) [[attachment:Light_Time_LDR_Fade.ino]]
  • [get | view] (2015-12-20 02:08:33, 1.1 KB) [[attachment:Light_Time_Switch.ino]]
  • [get | view] (2015-12-20 02:08:33, 5.7 KB) [[attachment:Multimedia_examples.pdf]]
  • [get | view] (2015-12-20 02:08:33, 5.7 KB) [[attachment:Multimedia_examples_WS03.zip]]
  • [get | view] (2015-12-20 02:08:34, 5423.2 KB) [[attachment:OOCSI_in_Processing.pdf]]
  • [get | view] (2015-12-20 02:08:33, 863.2 KB) [[attachment:ObjectOrientation.pdf]]
  • [get | view] (2015-12-20 02:08:33, 3.1 KB) [[attachment:ObjectOrientation_examples_BouncingBalls.pde]]
  • [get | view] (2015-12-20 02:08:33, 1.4 KB) [[attachment:ObjectOrientation_examples_Cars.pdf]]
  • [get | view] (2015-12-20 02:08:33, 1.4 KB) [[attachment:ObjectOrientation_examples_Cars.zip]]
  • [get | view] (2015-12-20 02:08:33, 1.6 KB) [[attachment:ObjectOrientation_examples_EPD.pdf]]
  • [get | view] (2015-12-20 02:08:33, 1.6 KB) [[attachment:ObjectOrientation_examples_EPD.zip]]
  • [get | view] (2015-12-20 02:08:33, 0.8 KB) [[attachment:ObjectOrientation_examples_Earth.pdf]]
  • [get | view] (2015-12-20 02:08:33, 0.8 KB) [[attachment:ObjectOrientation_examples_Earth.zip]]
  • [get | view] (2015-12-20 02:08:33, 1.3 KB) [[attachment:ObjectOrientation_examples_Planets.pdf]]
  • [get | view] (2015-12-20 02:08:33, 1.3 KB) [[attachment:ObjectOrientation_examples_Planets.zip]]
  • [get | view] (2015-12-20 02:08:34, 76.4 KB) [[attachment:RegistrationList.pdf]]
  • [get | view] (2015-12-20 02:08:34, 73.0 KB) [[attachment:RegistrationList.xls]]
  • [get | view] (2015-12-20 02:08:34, 2080.5 KB) [[attachment:UsingLibraries.pdf]]
  • [get | view] (2015-12-20 02:08:34, 1176.2 KB) [[attachment:VariablesAndFlowControl.pdf]]
  • [get | view] (2015-12-20 02:08:32, 2.9 KB) [[attachment:bouncingBalls.pde]]
  • [get | view] (2015-12-20 02:08:32, 276.9 KB) [[attachment:examples.jpg]]
  • [get | view] (2015-12-20 02:08:32, 35.3 KB) [[attachment:examples320.jpg]]
  • [get | view] (2015-12-20 02:08:34, 21.4 KB) [[attachment:oocsi_processing_library.zip]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.