read_analog_value

Dependencies:   mbed

Fork of Nucleo_read_analog_value by FRA221_2017

main.cpp

Committer:
59340500018
Date:
2017-11-22
Revision:
3:70d41b9d0705
Parent:
2:3ff3ecfd9c70

File content as of revision 3:70d41b9d0705:

#include "mbed.h"
//LAB2 
AnalogIn analog_value(A1);
DigitalIn button(USER_BUTTON);   
DigitalOut led0(D2),led1(D3),led2(D4),led3(D5),led4(D6),led5(D7),led6(D8),led7(D9); //,seg1(D10),seg2(D11),seg3(D12),seg4(D13),seg5(D14),seg6(D15);
unsigned int convertcount = 0;
unsigned int Array_A[4];
unsigned int Array_B[4];
BusOut segmentA(D14,D15);
BusOut segmentB(D10,D11,D12,D13);
/* void ConvertToBinary(unsigned int n, char pos)
{
    if (pos == 'a'){
        if (n / 2 != 0) {

            ConvertToBinary(n / 2,'a');

    }
    Array_A[convertcount] = n%2;
    convertcount = convertcount+1;
    }
    
    else if (pos == 'b'){
        if (n / 2 != 0) {
            ConvertToBinary(n / 2,'b');
    }
        Array_B[convertcount] = n%2;
        convertcount = convertcount+1;
    }
    seg1 = Array_B[3]; seg2 = Array_B[2]; seg3 = Array_B[1]; seg4 = Array_B[0]; seg5 = Array_A[3]; seg6 = Array_A[2];
}*/

void VU_LED(){
            float A1_val;
            A1_val = analog_value.read();
            A1_val = A1_val*3300;
            if (A1_val > 412.5f){
                led0 = 1;
                led1=0; led2=0; led3=0; led4=0; led5=0; led6=0; led7=0;
                if (A1_val > 825.0f){
                    led1 = 1;
                    led2=0; led3=0; led4=0; led5=0; led6=0; led7=0;
                    if (A1_val > 1237.5f){
                        led2 = 1;
                        led3=0; led4=0; led5=0; led6=0; led7=0;
                        if (A1_val > 1650.0f){
                            led3 = 1;
                            led4=0; led5=0; led6=0; led7=0;
                            if (A1_val > 2062.5f){
                                led4 = 1;
                                led5=0; led6=0; led7=0;
                                if (A1_val > 2475.0f){
                                    led5 = 1;
                                    led6=0; led7=0;
                                    if(A1_val > 2887.5f){
                                        led6 = 1;
                                        led7=0;
                                        if (A1_val > 3250.0f){
                                            led7 = 1;
                                        }
                                    } 
                                }
                            }
                        }
                    }
                }
            }
            else{
                    led0 = 0;
            }
    }

void LED_Segment(){
    float A1_uval;
    unsigned int Temp_A, Temp_B;
    A1_uval = analog_value.read();
    A1_uval = A1_uval*3.3f;
    Temp_A = A1_uval;
    Temp_B = (A1_uval*10);
    Temp_B = Temp_B % 10;
    segmentA = Temp_A;
    segmentB = Temp_B;
    /*convertcount = 0;
    ConvertToBinary(Temp_A,'a');
    convertcount = 0;
    ConvertToBinary(Temp_B,'b');*/
    
}
    
int main() {
    int x;  
    while(1){
        if (button==0){
            x = 0;
            while(1){
            if (x % 2 == 0){
                    while(1){
                    //seg1 = 0; seg2 = 0; seg3 = 0; seg4 = 0; seg5 = 0; seg6 = 0;
                    segmentA = 0;
                    segmentB = 0; 
                    VU_LED();
                    if (button == 0) break; }
                    x++;
                    break; 
                }
            }
            while(1){
            if(x % 2 != 0){
                    while(1){
                    led0=0; led1=0; led2=0; led3=0; led4=0; led5=0; led6=0; led7=0;
                    LED_Segment();
                    if (button == 0) break; }
                    x--;
                    break;
                }
            }  
        } 
    }  
     /*if (button==0){
            Temp = 0; 
            if (Temp==0){
                seg1 = 0; seg2 = 0; seg3 = 0; seg4 = 0; seg5 = 0; seg6 = 0; 
                VU_LED();
                }
            else if (Temp==1){
                led0=0; led1=0; led2=0; led3=0; led4=0; led5=0; led6=0; led7=0;
                LED_Segment();
                }           
            }
        else if (button==1){
            Temp = 1;
            if (Temp==1){
                led0=0; led1=0; led2=0; led3=0; led4=0; led5=0; led6=0; led7=0;
                LED_Segment();
                }
            else if (Temp==0){
                seg1 = 0; seg2 = 0; seg3 = 0; seg4 = 0; seg5 = 0; seg6 = 0; 
                VU_LED();
                }
            }*/
    /*unsigned int x = 0;
    while(1){
        while (button==0){
            x = 1;
            if(x == 1) {
                seg1 = 0; seg2 = 0; seg3 = 0; seg4 = 0; seg5 = 0; seg6 = 0; 
                VU_LED();
                x = 2;
            }
        }
        if (x==2){
                if(x == 2){
                    led0=0; led1=0; led2=0; led3=0; led4=0; led5=0; led6=0; led7=0;
                    LED_Segment();
                }
        }
                
                while(button == 0){
                    if(x == 2){
                    led0=0; led1=0; led2=0; led3=0; led4=0; led5=0; led6=0; led7=0;
                    LED_Segment();
                    x = 0;
                    break;
                    }
                }*/

}
/*
AnalogIn analog_value(A0);
 
DigitalOut led(LED1);
    float meas;
    unsigned short meas2
    
    while(1) {
        meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) or unsigned short read_u16
        meas = meas * 3300; // Change the value to be in the 0 to 3300 range
        if (meas > 2000) { // If the value is greater than 2V then switch the LED on
          led = 1;
        }
        else {
          led = 0;
        }
        wait(0.2); // 200 ms
    }*/