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

Dependencies:   mbed

Fork of Nightlight3 by Charles Tritt

Committer:
CSTritt
Date:
Thu Apr 06 18:09:47 2017 +0000
Revision:
5:f723b267eae3
Parent:
4:aa100356f053
Child:
6:0c1ab2c11252
Smart Nightlight program modified for common anode LED.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 0:8ed2f4a2a2fe 1 /*
CSTritt 5:f723b267eae3 2 Project: Nightlight3
CSTritt 0:8ed2f4a2a2fe 3 File: main.cpp
CSTritt 0:8ed2f4a2a2fe 4
CSTritt 5:f723b267eae3 5 See Word document. Common anode version (makes equations odd).
CSTritt 0:8ed2f4a2a2fe 6
CSTritt 0:8ed2f4a2a2fe 7 Written by: Dr. C. S. Tritt
CSTritt 0:8ed2f4a2a2fe 8 Created: 3/26/17 (v. 1.0)
CSTritt 0:8ed2f4a2a2fe 9
CSTritt 0:8ed2f4a2a2fe 10 */
CSTritt 0:8ed2f4a2a2fe 11 #include "mbed.h"
CSTritt 0:8ed2f4a2a2fe 12
CSTritt 2:5682a72277ed 13 const int HIGH = 1; // Inclusion is optional, but makes code more readable.
CSTritt 2:5682a72277ed 14 const int LOW = 0; // Inclusion is optional, but makes code more readable.
CSTritt 2:5682a72277ed 15
CSTritt 5:f723b267eae3 16 const float br_min = 0.60; // Read from serial stream and enter.
CSTritt 5:f723b267eae3 17 const float br_max = 0.89; // Read from serial stream and enter.
CSTritt 2:5682a72277ed 18 const float k_1 = 0.7; // These values work well...
CSTritt 2:5682a72277ed 19 const float k_2 = 0.5;
CSTritt 2:5682a72277ed 20 const float k_3 = 0.3;
CSTritt 2:5682a72277ed 21 const float all_off = br_min + k_1*(br_max - br_min); // Thresholds... All off.
CSTritt 2:5682a72277ed 22 const float blu_grn = br_min + k_2*(br_max - br_min); // Blue-green fade.
CSTritt 2:5682a72277ed 23 const float grn_red = br_min + k_3*(br_max - br_min); // Green-red fade.
CSTritt 0:8ed2f4a2a2fe 24
CSTritt 5:f723b267eae3 25 AnalogIn photocell(A0); // Create object for photocell.
CSTritt 2:5682a72277ed 26 PwmOut red(D9), grn(D10), blu(D11); // Create objects for LED connected pins.
CSTritt 0:8ed2f4a2a2fe 27
CSTritt 0:8ed2f4a2a2fe 28 int main() {
CSTritt 2:5682a72277ed 29 float brightness; // 0 to 1 max. range. Larger indicates brighter light.
CSTritt 0:8ed2f4a2a2fe 30
CSTritt 2:5682a72277ed 31 printf("\nSmart nightlight example\n"); // ID software.
CSTritt 0:8ed2f4a2a2fe 32
CSTritt 0:8ed2f4a2a2fe 33 while(true) {
CSTritt 2:5682a72277ed 34 brightness = photocell; // Read light level (0 to 1).
CSTritt 2:5682a72277ed 35 printf("Value = %f\n", brightness); // Send as text via serial port.
CSTritt 2:5682a72277ed 36 if (brightness > all_off) { // Bright light. All LEDs off.
CSTritt 5:f723b267eae3 37 red = HIGH;
CSTritt 5:f723b267eae3 38 grn = HIGH;
CSTritt 5:f723b267eae3 39 blu = HIGH;
CSTritt 2:5682a72277ed 40 }
CSTritt 2:5682a72277ed 41 else if (brightness > blu_grn) { // Blue to green fade.
CSTritt 5:f723b267eae3 42 red = HIGH;
CSTritt 5:f723b267eae3 43 grn = 1.0f - (all_off - brightness)/(all_off - blu_grn);
CSTritt 2:5682a72277ed 44 blu = 1.0f - grn;
CSTritt 0:8ed2f4a2a2fe 45 }
CSTritt 2:5682a72277ed 46 else if (brightness > grn_red) { // Green to red fade.
CSTritt 5:f723b267eae3 47 red = 1.0f - (blu_grn - brightness)/(blu_grn - grn_red);
CSTritt 2:5682a72277ed 48 grn = 1.0f - red;
CSTritt 5:f723b267eae3 49 blu = HIGH;
CSTritt 0:8ed2f4a2a2fe 50 }
CSTritt 2:5682a72277ed 51 else { // Red on full intensity.
CSTritt 5:f723b267eae3 52 red = LOW;
CSTritt 5:f723b267eae3 53 grn = HIGH;
CSTritt 5:f723b267eae3 54 blu = HIGH;
CSTritt 2:5682a72277ed 55 }
CSTritt 2:5682a72277ed 56 wait(0.1); // Delay 100 ms
CSTritt 0:8ed2f4a2a2fe 57 }
CSTritt 4:aa100356f053 58 }