Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp
- Committer:
- elab
- Date:
- 2021-04-10
- Revision:
- 4:972778a8aca8
- Parent:
- 3:9d006b6e1d04
File content as of revision 4:972778a8aca8:
/*
    Project: analogRead
    File: main.cpp
    
    Reads from analog input, streams ASCII text to std serial using printf and
    lights onboard LED. Also demonstrates use of floating point literal suffix
    toeliminate warning and int constants for HIGH and LOW.
    
    Written by: Dr. C. S. Tritt
    Created: 3/27/17 (v. 1.1)
    
*/
#include "mbed.h"
const int HIGH = 1; // Optional, but makes code more readable.
const int LOW = 0; // Optional, but makes code more readable.
 
AnalogIn analog_value(A1); 
DigitalOut led(LED1);
PwmOut servo(D6);
int main() {
    float value; // Value to be read and sent to serial port.
    servo.period_ms(20);
    printf("\nAnalogIn example\n");
    
    while(true) {
        value = analog_value.read(); // Read the analog input value (0 to 1)
        printf("Value = %f\n", value); // Send value as text via serial port.
//        printf("Value_int = %d\n", int(value*180)); // Send value as text via serial port.
        int angle = int(value*180);
        servo.pulsewidth_us(10*angle+500);
        if (value > 0.5f) { // Activate built-in LED. The f is optional.
          led.write(HIGH);
        }
        else {
          led.write(LOW);
        }
        printf("LED = %d\n", (int) led.read()); // Send LED state via serial. 
        wait(0.25); // 250 ms
    }
}