![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
This is how i got the hardware to work. Not saying that it cant work any other way. I haven't tied anything of yours in. So likely delete mine from rev 2 or 3, and then put this instead. Then it might work.
Dependencies: mbed
Revision 0:c426396d5fdf, committed 2017-06-22
- Comitter:
- PET
- Date:
- Thu Jun 22 04:45:01 2017 +0000
- Commit message:
- If you wanted to work on it, or look at it.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Jun 22 04:45:01 2017 +0000 @@ -0,0 +1,290 @@ +#include "mbed.h" +#include "rgb_led.h" + +/* +- number[] is the 3 digits. [0] is the digit for the letter L or H, and needs + to be a 0 or a 1, for the display to light up. +- number[1] is the 'tens', number [2] is the 'ones'. +- The program takes care of the transistor count itself. So no need to + send anything for that to the Ticker any longer. Now only the digits needs + to be placed in number. +- I got the ticker to work out here. Not in .h etc. +- And I am fairly sure it says somewhere in the big white programming book + that a enum starts from 1. However here it starts at 0. Så status needs to + subtracted a 1, before it gets send to the function below. (RGB...). +*/ + +DigitalOut Dig_1(PB_6); +DigitalOut Dig_2(PC_7); +DigitalOut Dig_3(PA_9); +DigitalOut Seg_A(PA_7); //original design says PA_3. I think there is a button +DigitalOut Seg_B(PA_10);// here (PA_7). +DigitalOut Seg_C(PB_3); +DigitalOut Seg_D(PB_5); +DigitalOut Seg_E(PB_4); +DigitalOut Seg_F(PB_10); +DigitalOut Seg_G(PA_8); + +int number[3] = {0, 0, 0}; +int dig = 0; +int status; + +void rgb_outp(int status); +void output(); + +Ticker output_ticker; + +main() +{ + int i; + output_ticker.attach(&output, 0.001); + + for(i = 0; i < 11; i++) // Would need deletion from here.. + { + if(i > 9) + { + i = 1; + }; + + number[0]++; + if(number[0] > 9) + { + status = 0; + rgb_outp(status); + number[0] = 0; + number[1]++; + if(number[1] > 9) + { + status = 4; + rgb_outp(status); + number[1] = 0; + number[2]++; + } + } + if((number[0] == 9) && (number[1] == 9) && (number[2] == 9)) + { + number[0] = 0; + number[1] = 0; + number[2] = 0; + } + + + wait(0.2); + } // until here. +} + +void output() +{ + int out_value; + + dig++; + if(dig == 4) + { + dig = 1; + } + + switch(dig) + { + case 1: + Dig_1 = 0; + Dig_2 = 0; + Dig_3 = 1; + out_value = number[0]; + break; + case 2: + Dig_1 = 0; + Dig_2 = 1; + Dig_3 = 0; + out_value = number[1]; + break; + case 3: + Dig_1 = 1; + Dig_2 = 0; + Dig_3 = 0; + if(number[2] == 0) + { + out_value = 20; + } + else if(number[2] == 1) + { + out_value = 30; + } + else + { + Dig_1 = 0; + Dig_2 = 0; + Dig_3 = 0; + } + break; + default: + Dig_1 = 0; + Dig_2 = 0; + Dig_3 = 0; + out_value = 0; + } + + switch(out_value) + { + case 0: + Seg_A = 0; + Seg_B = 0; + Seg_C = 0; + Seg_D = 0; + Seg_E = 0; + Seg_F = 0; + Seg_G = 1; + break; + case 1: + Seg_A = 1; + Seg_B = 0; + Seg_C = 0; + Seg_D = 1; + Seg_E = 1; + Seg_F = 1; + Seg_G = 1; + break; + case 2: + Seg_A = 0; + Seg_B = 0; + Seg_C = 1; + Seg_D = 0; + Seg_E = 0; + Seg_F = 1; + Seg_G = 0; + break; + case 3: + Seg_A = 0; + Seg_B = 0; + Seg_C = 0; + Seg_D = 0; + Seg_E = 1; + Seg_F = 1; + Seg_G = 0; + break; + case 4: + Seg_A = 1; + Seg_B = 0; + Seg_C = 0; + Seg_D = 1; + Seg_E = 1; + Seg_F = 0; + Seg_G = 0; + break; + case 5: + Seg_A = 0; + Seg_B = 1; + Seg_C = 0; + Seg_D = 0; + Seg_E = 1; + Seg_F = 0; + Seg_G = 0; + break; + case 6: + Seg_A = 0; + Seg_B = 1; + Seg_C = 0; + Seg_D = 0; + Seg_E = 0; + Seg_F = 0; + Seg_G = 0; + break; + case 7: + Seg_A = 0; + Seg_B = 0; + Seg_C = 0; + Seg_D = 1; + Seg_E = 1; + Seg_F = 1; + Seg_G = 1; + break; + case 8: + Seg_A = 0; + Seg_B = 0; + Seg_C = 0; + Seg_D = 0; + Seg_E = 0; + Seg_F = 0; + Seg_G = 0; + break; + case 9: + Seg_A = 0; + Seg_B = 0; + Seg_C = 0; + Seg_D = 0; + Seg_E = 1; + Seg_F = 0; + Seg_G = 0; + break; + case 20: + Seg_A = 1; + Seg_B = 1; + Seg_C = 1; + Seg_D = 0; + Seg_E = 0; + Seg_F = 0; + Seg_G = 1; + break; + case 30: + Seg_A = 1; + Seg_B = 0; + Seg_C = 0; + Seg_D = 1; + Seg_E = 0; + Seg_F = 0; + Seg_G = 0; + break; + default: + Seg_A = 1; + Seg_B = 1; + Seg_C = 1; + Seg_D = 1; + Seg_E = 1; + Seg_F = 1; + Seg_G = 1; + } +} + +void rgb_outp(int status) +{ + RGB_LED lamp(PB_13,PB_14, PB_15); // Creates an object out of the class RGB_LED. + // Connect pins on the Nucleo, to the pins the + // class. + + enum colour // Enumeration is used only for making the program more + { // easily readable. + green, // Is alike an int, starts at green = 1, + orange, // orange = 2 etc. + red, + red_blink, + blue + }; + + colour RGB_out = static_cast<colour>(status); + // Taking the value from the sensors and change them the enum type. + // Enum is somewhat akin to an int already, but RGB_out != status.. + + switch(RGB_out) + { + case green: + lamp.set(0.0f, 0.0f, 1.0f); + break; + case orange: + lamp.set(1.0f, 1.0f, 0.0f); + break; + case red: + lamp.set(1.0f, 0.0f, 0.0f); + break; + case red_blink: + lamp.flash(1.0f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f); + // On for 50% of every 1 seconds. + break; + case blue: + lamp.flash(1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.5f); + // On for 50% of every 0.5 seconds. + break; + default: // Error has occured, blue. Could just have defaulted instead + lamp.set(1.0f, 1.0f, 1.0f); // of case blue, however as it is an + } + + return; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Jun 22 04:45:01 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed/builds/0f02307a0877 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rgb_led.cpp Thu Jun 22 04:45:01 2017 +0000 @@ -0,0 +1,48 @@ +/* +-------------------------------------------------------------------------------- +-- Project: PRO2 "Awareness and Optimisation of energy consumption" +-- Team: Team 1 + +-- File Name: rgb_led.cpp +-- Author: Poul Erik Tjørnfelt +-- Date: 07/05-2017 +-- Copyright: Open to all +-- Version: 0.6 - Creation of file. +-- 1.0 - Finished version. +-- +-- Description: The .cpp file for a class, that creates instances of +-- the actual RGB lamp that is used. +-- +-- +-------------------------------------------------------------------------------- +*/ + +#include "mbed.h" +#include "rgb_led.h" + +RGB_LED::RGB_LED(PinName pin_R, PinName pin_G, PinName pin_B) + :pin_r(pin_R), pin_g(pin_G), pin_b(pin_B) // Constructor +{ + +} + +void RGB_LED::set(float red,float green, float blue) +{ + pin_r = red; // The amount of the single colours that we want in the + pin_g = green; // actual colour (ex. purple = 0.7f, 0.0, 0.7f), gotten from + pin_b = blue; // www.w3schools.com/colors/colors_picker.asp. + /* + We only use red, orange (2 parts red, 1 part green), green and blue. + */ +} + +void RGB_LED::flash(float per_r, float time_r, float per_g, float time_g, + float per_b, float time_b) +{ + pin_r.period(per_r); // Sets the period in seconds of the LED. + pin_r.write(time_r); // The %age of the period that the LED is turned on. + pin_g.period(per_g); + pin_g.write(time_g); + pin_b.period(per_b); + pin_b.write(time_b); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rgb_led.h Thu Jun 22 04:45:01 2017 +0000 @@ -0,0 +1,37 @@ +/* +-------------------------------------------------------------------------------- +-- Project: PRO2 "Awareness and Optimisation of energy consumption" +-- Team: Team 1 + +-- File Name: rgb_led.h +-- Author: Poul Erik Tjørnfelt +-- Date: 07/05-2017 +-- Copyright: Open to all +-- Version: 0.6 - Creation of file. +-- 1.0 - File approval. +-- +-- Description: The header file for a class, that creates instances of +-- the actual RGB lamp that is used. +-- +-------------------------------------------------------------------------------- +*/ + +#ifndef RGB_LED_H +#define RGB_LED_H +#include "mbed.h" + +class RGB_LED +{ +public: + RGB_LED(PinName pin_R, PinName pin_G, PinName pin_B); + void set(float red, float green, float blue); + void flash(float per_r, float time_r, float per_g, float time_g, + float per_b, float time_b); + +private: + PwmOut pin_r; + PwmOut pin_g; + PwmOut pin_b; +}; + +#endif \ No newline at end of file