You are viewing an older revision! See the latest version

AnalogIn

Table of Contents

  1. Hello World!
  2. API
  3. Examples

The AnalogIn Interface is used to read the voltage applied to an analog input pin.

Hello World!

[Repository '/users/mbed_official/code/AnalogIn_HelloWorld_Mbed/docs/tip/main_8cpp_source.html' not found]

API

API summary

Import librarymbed

No documentation found.

Examples

Control a R/C servo with an analog input

#include "mbed.h"

AnalogIn position(p20);
PwmOut servo(p21);

int main() {
    servo.period(0.020f);          // servo requires a 20ms period
    while (1) {
        servo.pulsewidth(0.001f + 0.001f * position); // servo position determined by a pulse width between 1-2ms
    }
}


AnalogIn reading 16-bit normalized samples

#include "mbed.h"

AnalogIn input(p20);
DigitalOut led1(LED1);

int main() {
    uint16_t samples[1024];

    for(int i=0; i<1024; i++) {
        samples[i] = input.read_u16();
        wait_ms(1);
    }

    printf("Results:\n");
    for(int i=0; i<1024; i++) {
        printf("%d, 0x%04X\n", i, samples[i]);
    }
}   


#include "mbed.h"

AnalogIn ain(p20);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

int main() {
    while (1){
        led1 = (ain > 0.2f) ? 1 : 0;
        led2 = (ain > 0.4f) ? 1 : 0;
        led3 = (ain > 0.6f) ? 1 : 0;
        led4 = (ain > 0.8f) ? 1 : 0;
    }
}

All wikipages