if then else demo with potmeter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 // redled is an object of class PwmOut. It uses the LED_RED pin
00004 // in human speech: redled is an output that can be controlled with PWM. LED_RED is the pin which is connected to the output
00005 PwmOut redled(LED_RED);
00006 
00007 //ditto...
00008 PwmOut greenled(LED_GREEN);
00009 PwmOut blueled(LED_BLUE);
00010 
00011 // pot is an object of class AnalogIn. It uses the PTB0 pin
00012 // in human speech: pot is an analog input. You can read the voltage on pin PTB0
00013 AnalogIn pot(PTB0);
00014 
00015 
00016 //start 'main' function. Should be done once in every C(++) program
00017 int main()
00018 {
00019     float potvalue;
00020     //setup some stuff
00021     //period of PWM signal is 10kHz. Every 100 microsecond a new PWM period is started
00022     redled.period_ms(0.1);
00023     greenled.period_ms(0.1);
00024     blueled.period_ms(0.1);
00025     //while 1 is unequal to zero. For humans: loop forever
00026     while(1)
00027     {
00028         //limit loop time
00029         wait(0.01);
00030         //read potentiometer, store in potvalue
00031         potvalue = pot.read();
00032         if(potvalue < 0.33)
00033         {
00034             redled = 1-potvalue; //subtract from 1 to get high value (almost off) at potmeter maximum
00035             greenled = 1; //off
00036             blueled  = 1; //off
00037         }
00038         else // value greater or equal to 0.33
00039         {
00040             if(potvalue < 0.66)
00041             {
00042                 redled = 1-potvalue;
00043                 greenled = 1-potvalue;
00044                 blueled = 1;
00045             }
00046             else //value greater or equal to 0.66
00047             {
00048                 redled = 1-potvalue;
00049                 greenled = 1-potvalue;
00050                 blueled = 1-potvalue;
00051             }
00052         }
00053     }
00054 }
00055