ibrahim ozturk

Entrepreneur, Tech CTO, Software Developer, Electronics Engineer, Author



Hopefully, I have got my free mbed thanks to ARM. (mbed LPC1768)

I will publish here my hobby projects on mbed board asap.

https://mbed.org/

First example on mbed:

This example code can adjust the brightness level of 4 LEDs on mbed board over serial terminal. Basically, there are 6 inputs which are ‘1’,’2′,’3′,’4′ for the selecting  of active LED and ‘i’,’d’  for the selecting of active action on brightness level. For instance, if you press 2 and then i, the brightness of 2. LED will be increased on mbed. Try it and enjoy with it.

/**
  ******************************************************************************
  * @file    main.cpp
  * @author  Ibrahim Ozturk
  * @version V1.0.0
  * @date    22-May-2013
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FILE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING DEVELOPERS
  * WITH CODING INFORMATION REGARDING THEIR MBED APPLICATIONS IN ORDER FOR THEM TO SAVE
  * TIME. IF YOU ADD THE AUTHOR'S NAME IN YOUR CODE, YOU CAN USE THIS CODE ANYWEHERE YOU WANT.
  *
  ******************************************************************************
  */
#include "mbed.h"

#define LED_ON  1
#define LED_OFF 0

#define BRIGHTNESS_INCREASE  0
#define BRIGHTNESS_DECREASE  1
#define BRIGHTNESS_STABLE    2

#define BRIGHTNESS_STEP_SIZE    0.01
#define BRIGHTNESS_MAX_VAL      0.5
#define BRIGHTNESS_MIN_VAL      0

Serial pc(USBTX, USBRX); // tx, rx
PwmOut myLed1(LED1);
PwmOut myLed2(LED2);
PwmOut myLed3(LED3);
PwmOut myLed4(LED4);

void update_brightness(void) ;
void show_update_info(void);

struct MyLeds
{
    float brightness;
    bool  status;
};
struct MyActiveLed
{
    int   number;
    float brightness;
    int   brightnessSide;
};

MyLeds Led1,Led2,Led3,Led4;
MyActiveLed activeLed;

int main() {
    pc.printf("This program can control 4 LEDs' brightness level over serial terminal\r\n");

    //initialise
    activeLed.number = 1;
    activeLed.brightness = BRIGHTNESS_MIN_VAL;
    activeLed.brightnessSide = BRIGHTNESS_STABLE;

    Led1.brightness = BRIGHTNESS_MIN_VAL;
    Led2.brightness = BRIGHTNESS_MIN_VAL;
    Led3.brightness = BRIGHTNESS_MIN_VAL;
    Led4.brightness = BRIGHTNESS_MIN_VAL;
    char controlInput;

    pc.printf("Press 'u' to turn LED brightness up, 'd' to turn it down\r\n");
    pc.printf("Choose LED to Control (Just Type 1,2,3,4 or i,d) \r\n");

    while(1) {
        controlInput = pc.getc();

        switch (controlInput) {
            case '1':
                activeLed.number = 1;
                activeLed.brightness = Led1.brightness;
                activeLed.brightnessSide = BRIGHTNESS_STABLE;
                break;
            case '2':
                activeLed.number  = 2;
                activeLed.brightness = Led2.brightness;
                activeLed.brightnessSide = BRIGHTNESS_STABLE;
                break;
            case '3':
                activeLed.number  = 3;
                activeLed.brightness = Led3.brightness;
                activeLed.brightnessSide = BRIGHTNESS_STABLE;
                break;
            case '4':
                activeLed.number  = 4;
                activeLed.brightness = Led4.brightness;
                activeLed.brightnessSide = BRIGHTNESS_STABLE;
                break;
            case 'i':
                activeLed.brightnessSide = BRIGHTNESS_INCREASE;
                break;
            case 'd':
                activeLed.brightnessSide = BRIGHTNESS_DECREASE;
                break;
            default :
                pc.printf("Please type a valid input as 1,2,3,4,i or d !\r\n");
                break;
         }
         update_brightness();
         show_update_info();
    }
}

void update_brightness(void)
{
    if((activeLed.brightnessSide == BRIGHTNESS_INCREASE) && (activeLed.brightness < BRIGHTNESS_MAX_VAL))     {         switch(activeLed.number){             case 1:                    activeLed.brightness += BRIGHTNESS_STEP_SIZE;                    Led1.brightness = activeLed.brightness;                    myLed1 = activeLed.brightness;                     break;             case 2:                    activeLed.brightness += BRIGHTNESS_STEP_SIZE;                    Led2.brightness = activeLed.brightness;                    myLed2 = activeLed.brightness;                     break;             case 3:                    activeLed.brightness += BRIGHTNESS_STEP_SIZE;                    Led3.brightness = activeLed.brightness;                    myLed3 = activeLed.brightness;                     break;              case 4:                    activeLed.brightness += BRIGHTNESS_STEP_SIZE;                    Led4.brightness = activeLed.brightness;                    myLed4 = activeLed.brightness;                     break;                            }     }     else if((activeLed.brightnessSide == BRIGHTNESS_DECREASE) && (activeLed.brightness  > BRIGHTNESS_MIN_VAL))
    {
        switch(activeLed.number){
            case 1:
                   activeLed.brightness -= BRIGHTNESS_STEP_SIZE;
                   Led1.brightness = activeLed.brightness;
                   myLed1 = activeLed.brightness;
                   break;
            case 2:
                   activeLed.brightness -= BRIGHTNESS_STEP_SIZE;
                   Led2.brightness = activeLed.brightness;
                   myLed2 = activeLed.brightness;
                    break;
            case 3:
                   activeLed.brightness -= BRIGHTNESS_STEP_SIZE;
                   Led3.brightness = activeLed.brightness;
                   myLed3 = activeLed.brightness;
                    break;
            case 4:
                   activeLed.brightness -= BRIGHTNESS_STEP_SIZE;
                   Led4.brightness = activeLed.brightness;
                   myLed4 = activeLed.brightness;
                   break;
         }
    }
}
void show_update_info(void)
{
   pc.printf(" LED NUM : %d  ",activeLed.number);
   pc.printf(" LED SIDE : %d  ",activeLed.brightnessSide);
   pc.printf(" LED BRIGHTNESS : %f...\r\n",activeLed.brightness);
}

 

SHARE :

(1) Comment

adminozturkib
6 years ago · Reply

if you have further questions donot hesitate to ask!
Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*


error: Content is protected !!