Advertisement
kowalyshyn_o

Night Light Stuffy

Jun 13th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. /**************************************************************************************
  2.    ISTE 2016 Workshop: The new WWW -- Wonderful World of Wearables
  3.    Night Light Stuffy
  4.  
  5.    How it works
  6.    
  7.    Read value from light sensor conntected to A2, serial monitor displays live data
  8.    Returns values 0 to 1023 (1024 steps or 2^10 - 10 bit resolution)
  9.    Value is scaled to V units with the formula: sensorValue * (5V /1023)
  10.    Note digital pins provide up to: 40mA and 5V (0.2W power)
  11.  
  12.    RGB LED is connected to digital pins 9, 10, 11
  13.    Depending on light intensity, a PWM value is written back to pins 9, 10, 11 (analogWrite command)
  14.  
  15.    TROUBLESHOOTING THE ARDUINO ENVIRONMENT
  16.    -delete preferences file ~/library/preferences/com.arduino.
  17.    -restart Arduino application
  18.  
  19.    ADVANCED USERS: DISABLE ROOTLESS MODE OSX 10.11 (El Capitan) FOR FTDI ISSUES
  20.    1. Reboot into recovery mode (cmd-R)
  21.    2. Log into terminal and enter: csrutil disable or csrutil enable --without kext (to only disable kernal extensions like serial drivers)
  22.    3. Reboot
  23.    4. Check using csrutil status
  24.    
  25.    http://tzapu.com/2015/09/24/making-ch340-ch341-serial-adapters-work-under-el-capitan-os-x/
  26.  
  27.    
  28.  **************************************************************************************/
  29.  
  30. //Define constants - values do not change in program from starting state
  31.  
  32. #define sensorPin A5       //sensor pin, reads voltage in 10-bit resolution (0-1023)
  33. #define redPin 11          //R petal on RGB LED module connected to digital pin 11
  34. #define greenPin 9         //G petal on RGB LED module connected to digital pin 9
  35. #define bluePin 10         //B petal on RGB LED module connected to digital pin 10
  36. #define time_delay 0       //set delay that LED will display the color, used a test variable, normal set to 0
  37.  
  38. //Define variables
  39.  
  40. int sensorValue = 0;        //variable that returns value from sensorPin
  41. float sensorVoltage = 0.0;  //float variable to hold converted voltages
  42. float threshold1 = 0.08;    //RED, turn on when room is dark
  43. float threshold2 = 0.50;    //GREEN, turn on normal light conditions
  44. float threshold3 = 1.20;    //BLUE, turn on when light is bright
  45. float thresholdMax = 2.00;  //Very bright room, turn off LED
  46.  
  47. void setup() {
  48.   // initialize serial communication at 9600 baud
  49.   Serial.begin(9600);
  50.  
  51.   // set up pins connected to RGB module
  52.   pinMode(redPin, OUTPUT);    // sets the redPin to be an output
  53.   pinMode(greenPin, OUTPUT);  // sets the greenPin to be an output
  54.   pinMode(bluePin, OUTPUT);   // sets the bluePin to be an input
  55.  
  56. }
  57.  
  58. // define color function to set colors in RGB values from 0-255
  59. void color (unsigned char red, unsigned char green, unsigned char blue)
  60. {
  61.   analogWrite(redPin, 255 - red);
  62.   analogWrite(bluePin, 255 - blue);
  63.   analogWrite(greenPin, 255 - green);
  64. }
  65.  
  66.  
  67. // the main loop routine runs over and over again:
  68. void loop() {
  69.  
  70.   // STEP 1 - read the input on analog pin 0:
  71.   sensorValue = analogRead(sensorPin);
  72.  
  73.   // STEP 2 - convert to equivalent voltage value from value read between 0-1023
  74.   sensorVoltage = sensorValue * (5.0 / 1023.0);
  75.  
  76.  
  77.   // STEP 3 - activate LED depending on light intensity using an elif ladder
  78.   if (sensorVoltage < threshold1) {
  79.     color(255, 0, 0);   //turn the RGB LED red
  80.     delay(time_delay);  //delay to hold light before changing
  81.   }
  82.  
  83.   else if ((sensorVoltage > threshold1) && (sensorVoltage < threshold2)) {
  84.     color(0, 255, 0);   //turn the RGB LED green
  85.     delay(time_delay);
  86.   }
  87.  
  88.   else if ((sensorVoltage > threshold2) && (sensorVoltage < threshold3)) {
  89.     color(0, 0, 255);   //turn the RGB LED blue
  90.     delay(time_delay);
  91.   }
  92.  
  93.   else if ((sensorVoltage > threshold3) && (sensorVoltage < thresholdMax)) {
  94.     color(0, 0, 0); //turn off LED, set to no color.
  95.   }
  96.  
  97.   // output to serial monitor for review as needed
  98.   Serial.print("Light sensor value is: ");
  99.   Serial.print(sensorVoltage);
  100.   Serial.println(" Volts");
  101.   delay(200);   // delay in between reads for viewing
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement