This is a program that generates a sinusoidal output wave with a frequency determined by the user. A terminal emulator is required to interact with the program.

Dependencies:   mbed

main.cpp

Committer:
302Instructor
Date:
2015-01-22
Revision:
0:40edabd603cf

File content as of revision 0:40edabd603cf:

#include "mbed.h"
#include <iostream>

Serial pc(USBTX, USBRX);
AnalogOut Aout(PTE30);
float i,delay,f;
bool flag = 0;

float brightness = 0.0;

void delay_change()
{
    Aout = 0;
    printf("\n\rPlease enter new frequency: ");
    std::cin >> f;
    delay = 1/(f*200);
    float freq = 1/(delay*200);
    printf("\n\rFrequency: %f",freq);
    flag = 0;
}

int main()
{
    printf("\n\rWelcome, please enter a frequency: \n");
    std::cin >> f;
    pc.attach(*delay_change);
    delay = 1/(f*200);
    float freq = 1/(delay*200);
    printf("\n\rFrequency: %f",freq);

    while(1) {
        for (i=0.01; i<=2; i=i+0.01) {
            if (flag == 0) {
                printf("\n\rPress ENTER on the keyboard to input a new frequency. \n");
                flag = 1;
            }
            Aout=0.5+0.5*sin(i*3.14159);
            wait(delay);
        }
    }
}