
2b
Dependencies: mbed
Diff: main.cpp
- Revision:
- 0:65445b54da31
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Sep 22 15:08:28 2017 +0000 @@ -0,0 +1,126 @@ +/************************************************************** +/ RGB LED Colour Variation Program +/ +/ The program varies the colour of the RGB LED based on the +/ analogue input value of Pot2 +/ +/**************************************************************/ +#include "mbed.h" +Serial pc(USBTX, USBRX); // USB serial interface +AnalogIn A_in(p20); // analogue input pin for reference values +//DigitalOut myled(LED1); // onboard LED to indicate activity +PwmOut Rled(p23); // red component of application board RGB LED +PwmOut Gled(p24); // green component of application board RGB LED +PwmOut Bled(p25); // blue component of application board RGB LED +int main() { + + float analogue_val=0.0; // variable to hold the analogue input values as decimals + float colour_level[11][4]={{0.09,1.0,0.0,0.0}, +{0.18,1.0,0.5,0.0}, +{0.27,1.0,1.0,0.0}, +{0.36,0.5,1.0,0.0}, +{0.45,0.0,1.0,0.0}, +{0.54,0.0,1.0,0.5}, +{0.63,0.0,1.0,1.0}, +{0.72,0.0,0.5,1.0}, +{0.81,0.0,0.0,1.0}, +{0.90,0.5,0.0,1.0}, +{1.00,1.0,0.0,1.0}}; // analogue input values vs. RGB levels + // set the USB serial interface baud rate + pc.baud(921600); + + while(1) { // repeat indefinitely + + // read the voltage on the analogue input pin and convert it to a + // decimal number with 12-bit resolution + analogue_val = A_in.read(); + + // set the number of lit LEDs to be proportional to the ADC input value + if(analogue_val < 0.09) { + Rled = 1 ; + Gled = 0 ; + Bled = 0 ; + } + else if(analogue_val < 0.18) { + + Rled = 1 ; + Gled = 0.5 ; + Bled = 0 ; + + } + else if(analogue_val < 0.27) { + + Rled = 1 ; + Gled = 1 ; + Bled = 0 ; + + + } + else if(analogue_val < 0.36) { + + Rled = 0.5 ; + Gled = 1 ; + Bled = 0 ; + + + } + else if(analogue_val < 0.45) { + + Rled = 0 ; + Gled = 1 ; + Bled = 0 ; + + + } + else if(analogue_val < 0.54) { + + Rled = 0 ; + Gled = 1 ; + Bled = 0.5 ; + + + } + else if(analogue_val < 0.63) { + + Rled = 0 ; + Gled = 1 ; + Bled = 1 ; + + + } + else if(analogue_val < 0.72) { + + Rled = 0 ; + Gled = 0.5 ; + Bled = 1 ; + + + } + else if(analogue_val < 0.81) { + + Rled = 0 ; + Gled = 0 ; + Bled = 1 ; + + + } + else if(analogue_val < 0.9) { + + Rled = 0.5 ; + Gled = 0 ; + Bled = 1 ; + + + } + else if(analogue_val < 0.1) { + + Rled = 1 ; + Gled = 0 ; + Bled = 1 ; + + + + + } + +}}