Sawtooth program

Dependencies:   mbed

Fork of shomberg_hw_3 by Russell Shomberg

Committer:
rshomberg
Date:
Tue Sep 25 15:42:54 2018 +0000
Revision:
2:312a0a9c4485
Parent:
1:cbee04784c60
Child:
3:56972a65cd0a
Clean and clarify code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rshomberg 0:82635173a413 1 /**
rshomberg 0:82635173a413 2 MBED Analug Output Sawtooth
rshomberg 0:82635173a413 3 main.cpp
rshomberg 0:82635173a413 4
rshomberg 0:82635173a413 5 Purpose: Output a sawtooth waveform
rshomberg 0:82635173a413 6 Adjust frequency using variable resistor
rshomberg 0:82635173a413 7 Adjust amplitude using switch
rshomberg 0:82635173a413 8
rshomberg 0:82635173a413 9 @author Russell Shomberg
rshomberg 0:82635173a413 10 @version 1.0 2018-09-25
rshomberg 0:82635173a413 11
rshomberg 2:312a0a9c4485 12 Issues:
rshomberg 2:312a0a9c4485 13 Frequency and amplitude only change at the start of the waveform by design
rshomberg 0:82635173a413 14
rshomberg 0:82635173a413 15
rshomberg 0:82635173a413 16 */
rshomberg 0:82635173a413 17
rshomberg 0:82635173a413 18 // INCLUDES
rshomberg 0:82635173a413 19 #include "mbed.h"
rshomberg 0:82635173a413 20
rshomberg 0:82635173a413 21 // INPUTS
rshomberg 2:312a0a9c4485 22 DigitalIn switchPosition(p7); // wire p7 to middle connection of 2 position switch between Vref and GND
rshomberg 2:312a0a9c4485 23 AnalogIn Ain(p20); // wire p20 to a variable resister connected from Vref and GND
rshomberg 0:82635173a413 24
rshomberg 0:82635173a413 25 // OUTPUTS
rshomberg 2:312a0a9c4485 26 Serial pc(USBTX, USBRX); // for debugging
rshomberg 2:312a0a9c4485 27 AnalogOut Aout(p18); // leave open lead on p18 for signal output
rshomberg 0:82635173a413 28
rshomberg 0:82635173a413 29 // VARIABLES
rshomberg 2:312a0a9c4485 30 float period; // range between ~0 and 1 (seconds)
rshomberg 2:312a0a9c4485 31 float amplitude; // switch between 1 and -.5
rshomberg 2:312a0a9c4485 32 float i; // index
rshomberg 0:82635173a413 33
rshomberg 0:82635173a413 34 int main() {
rshomberg 0:82635173a413 35 while(1) {
rshomberg 1:cbee04784c60 36
rshomberg 2:312a0a9c4485 37 // Check settings at start of loop which are changed with
rshomberg 1:cbee04784c60 38 if (switchPosition==1) {amplitude = 1;}
rshomberg 1:cbee04784c60 39 else {amplitude = 0.5;}
rshomberg 1:cbee04784c60 40
rshomberg 1:cbee04784c60 41 period = Ain;
rshomberg 1:cbee04784c60 42
rshomberg 2:312a0a9c4485 43 // Debugging code
rshomberg 1:cbee04784c60 44 //printf("Amplitude = %1.2f Volts\n\r", amplitude * 3.3f);
rshomberg 1:cbee04784c60 45 //printf("Period = %1.2f seconds\n\r", period);
rshomberg 1:cbee04784c60 46
rshomberg 1:cbee04784c60 47 for (i=0;i<1;i=i+.001){
rshomberg 1:cbee04784c60 48 Aout = i*amplitude;
rshomberg 1:cbee04784c60 49 wait(0.001*period);
rshomberg 0:82635173a413 50 }
rshomberg 1:cbee04784c60 51
rshomberg 1:cbee04784c60 52 for (i=1;i>0;i=i-.001){
rshomberg 1:cbee04784c60 53 Aout = i*amplitude;
rshomberg 1:cbee04784c60 54 wait(0.001*period);
rshomberg 0:82635173a413 55 }
rshomberg 0:82635173a413 56 }
rshomberg 0:82635173a413 57 }