part1

Dependencies:   mbed

main.cpp

Committer:
jaredwil
Date:
2015-02-17
Revision:
4:34df824930f6
Parent:
3:664e38d7ae34
Child:
5:cb200e69a544

File content as of revision 4:34df824930f6:

#include "mbed.h"
//Right now this is only working for inputs length 3

//Initialize PWM
PwmOut PWM1(p21);
Serial pc(USBTX, USBRX);
Timer t1;


//Initialize Input that is 8 character
char input[10];

  
 int main()
{
    //define input variables
    float freq;
    int dur;
    //Check if input is valid
    int isint;
    while(1){
         
            //wait for a valid input
            while(isint != 2) 
            {
                //Input Desired Tone
                printf("Input Desired Tone: Frequency (Hz) Duration (ms) followed by enter\r");
                char c = pc.getc();
                int i =0;
                while(c != '\r') {
                        printf("%c",c);
                        input[i++]=c;
                        c = pc.getc();
                    }
                    input[i] = '\0';
                //fgets(input, sizeof input, stdin);  //input size length 9 followed by enter (xxxx xxxx/r)
                isint = sscanf(input, "%f %d",&freq,&dur);      //assign input variables
                
                printf("isint = %d\r",isint); //debug check if input is correct
                //if input is incorrect this will be displayed
                if(isint!= 2)
                printf("You did not enter a number.Please enter an argument's number\r");
            
            }
        
          if(pc.readable()) {
              //if enter is pressed continue to output tone
              if(pc.getc() == '\r')
             {
                //show user what value was input
                printf("Valid Input Frequency = %3.0f, Duration  = %d\r", freq, dur);
                isint = 0;  //reset input
                
                //Play tone with correct freq and durration
                //Timer used for duration
                t1.reset();
                t1.start();
                PWM1.period(1/freq);               // set PWM period to 10 ms   
                PWM1=0.5;                         // set duty cycle to 50% 
                while(t1.read_ms() < dur){
                    //WAIT
                    }
                PWM1=0.0;                         //turn off
                t1.stop();
                
             }
            //if anyother button is pressed abort and start over
             else{
                printf("You didn't press enter you idiot\r");
                isint = 0;  
                 }
            }    
      }
      
}