fra221-pb11 / Mbed 2 deprecated hw02

Dependencies:   mbed

main.cpp

Committer:
xkzy
Date:
2018-10-19
Revision:
1:da458f1db225
Parent:
0:c8b89a6e4c0e
Child:
2:77448d13dd0d

File content as of revision 1:da458f1db225:

#include "mbed.h"
AnalogIn analog_value(A5);
BusOut vu_leds = (PA_3,PA_2,PA_10,PB_3,PB_5,PB_4,PB_10,PB_8);
BusOut digit1=(PA_9,PC_7,PB_6,PA_7);
BusOut digit2=(PB_0,PA_4,PA_1,PA_0);
DigitalIn user(USER_BUTTON);
DigitalOut led(LED1);
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
double power(int x,int y)
{
    int z=x;
    while(y>0) {
        z=z*x;
        y--;
    }return z;
}
void display_vu(unsigned int x)
{
    int out=power(2,x)-1;
    vu_leds=out;
}
void display_digits(int x)
{
    digit1=x/10;
    digit2=x%10;

}
int main()
{
    float adc1;
    int volt1;
    int volum;
    bool mode;
    while(1) {

        adc1 = analog_value.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
        volt1 = map(adc1,0,1.0,0,33); // Converts value in the 0V-3.3V range
        volum = map(adc1,0,1.0,0,8);
        if(mode) {
            display_vu(volum);
        } else {
            display_digits(volt1);
        }
        if(user) {
            mode=!mode;
            while(user);
        }
        wait(0.2); // 200 millisecond
    }
}