Sawtooth wave form on oscilloscope with variable amplitude and frequency

Dependencies:   mbed

main.cpp

Committer:
ccschneider
Date:
2018-09-27
Revision:
0:ce916b042621

File content as of revision 0:ce916b042621:

// Cecilia Schneider, OCE 360, September 24,2018
// HW #3, exercise 4, Analog I/O
// Sawtooth waveform on DAC output to view on oscilloscope
#include "mbed.h"
AnalogOut Aout(p18); //oscilliscope on pin 18
AnalogIn Ain(p20); //potentiometer on pin 20
DigitalIn Sin(p7); //switch on pin 7 
float ADCin;
float Switchin;
float i;
float j;

int main() {
    while(1) {
        ADCin = Ain; //
        if (Sin == 1) { 
            Switchin = 1; //when the switch is on, the amplitude will be 1
        }
        else if (Sin == 0) {
            Switchin = 0.5; //when the switch is off, the amplitude will be 0.5
        }
            for (i=0;i<Switchin;i=i+0.001) {
                Aout=i;
                wait(0.001*ADCin); //allows user to control frequency with potentiometer 
            }
            for (j=Switchin;j>0;j=j-0.001) {
                Aout=j;
                wait(0.001*ADCin);
            }
    }
}