Sawtooth wave form on oscilloscope with variable amplitude and frequency

Dependencies:   mbed

Revision:
0:ce916b042621
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 27 14:24:55 2018 +0000
@@ -0,0 +1,31 @@
+// 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);
+            }
+    }
+}