Displays 4 bit binary count on bar graph display. Demonstrates BusOut usage.

Dependencies:   mbed

Fork of Nightlight3 by Charles Tritt

Revision:
2:5682a72277ed
Parent:
1:4647b43d61ef
Child:
4:aa100356f053
--- a/main.cpp	Mon Mar 27 14:29:20 2017 +0000
+++ b/main.cpp	Thu Mar 30 19:59:58 2017 +0000
@@ -1,11 +1,8 @@
 /*
-    Project: analogRead_Overlaods
+    Project: Nightlight2
     File: main.cpp
     
-    Reads from analog input, streams ASCII text to std serial using printf, and
-    lights onboard LED. Also demonstrates use of floating point literal sufix to 
-    eliminate warning and int constants for HIGH and LOW. This version uses
-    overloaded operators and sends LED state read from DigitalOut object.
+    See Word document.
     
     Written by: Dr. C. S. Tritt
     Created: 3/26/17 (v. 1.0)
@@ -13,28 +10,49 @@
 */
 #include "mbed.h"
 
-const int HIGH = 1; // Optional, but makes code more readable.
-const int LOW = 0; // Optional, but makes code more readable.
+const int HIGH = 1; // Inclusion is optional, but makes code more readable.
+const int LOW = 0; // Inclusion is optional, but makes code more readable.
+
+const float br_min = 0.070; // Read from serial stream and enter.
+const float br_max = 0.430; // Read from serial stream and enter.
+const float k_1 = 0.7; // These values work well...
+const float k_2 = 0.5;
+const float k_3 = 0.3;
+const float all_off = br_min + k_1*(br_max - br_min); // Thresholds... All off.
+const float blu_grn = br_min + k_2*(br_max - br_min); // Blue-green fade.
+const float grn_red = br_min + k_3*(br_max - br_min); // Green-red fade.
  
-AnalogIn analog_value(A0);
- 
-DigitalOut led(LED1);
+AnalogIn photocell(A1); // Create object for photocell.
+PwmOut red(D9), grn(D10), blu(D11); // Create objects for LED connected pins.
 
 int main() {
-    float value; // Value to be read and sent to serial port.
+    float brightness; // 0 to 1 max. range. Larger indicates brighter light.
     
-    printf("\nAnalogIn example\n");
+    printf("\nSmart nightlight example\n"); // ID software.
     
     while(true) {
-        value = analog_value; // Read the analog input value (0 to 1)
-        printf("Value = %f\n", value); // Send value as text via serial port.
-        if (value > 0.5f) { // Activate built-in LED. The f is optional.
-          led = HIGH;
+        brightness = photocell; // Read light level (0 to 1).
+        printf("Value = %f\n", brightness); // Send as text via serial port.
+        if (brightness > all_off) { // Bright light. All LEDs off.
+          red = LOW;
+          grn = LOW;
+          blu = LOW;
+        }
+        else if (brightness > blu_grn) { // Blue to green fade.
+          red = LOW;
+          grn = (all_off - brightness)/(all_off - blu_grn);
+          blu = 1.0f - grn;
         }
-        else {
-          led = LOW;
+        else if (brightness > grn_red) { // Green to red fade.
+          red = (blu_grn - brightness)/(blu_grn - grn_red);
+          grn = 1.0f - red;
+          blu = LOW;
         }
-        printf("LED = %d\n", (int) led); // Send LED state as serial text.    
-        wait(0.25); // 250 ms
+        else { // Red on full intensity.
+          red = HIGH;
+          grn = LOW;
+          blu = LOW;            
+        }
+        wait(0.1); // Delay 100 ms
     }
 }