How to switch beetween code

I have these two codes

#define SAMPLE_PERIOD       20ul
//#define DEBUG_MSG           0

const uint8_t pinPeriod = A1;
const uint8_t pinOnTime = A2;
const uint8_t pinLED = 13;

uint32_t
    timeNow,
    timeSample;

uint16_t
    valPeriod,
    valOnTime,
    valOffTime;
float
    fTemp;
void setup()
{
    pinMode( pinPeriod, INPUT );
    pinMode( pinOnTime, INPUT );
   
    pinMode( pinLED, OUTPUT);

    Serial.begin( 9600 );
   
}//setup

void loop()
{
    timeNow = millis();
    if( timeNow - timeSample >= 50ul )
    {   
        timeSample = timeNow;
        valPeriod = analogRead( pinPeriod );   
        valPeriod = map( valPeriod, 0, 1023, 500, 20 );             
       
        valOnTime = analogRead( pinOnTime );   
        valOnTime = map( valOnTime, 0, 1023, 50, 100 );             
   
        fTemp = (float)valPeriod * (float)valOnTime / 1000.0;
        valOnTime = (uint16_t)fTemp;
        valOffTime = valPeriod - valOnTime;
       
#if DEBUG_MSG   
        Serial.print( "Period: " ); Serial.print( valPeriod );
        Serial.print( "\tOn time: " ); Serial.print( valOnTime );
        Serial.print( "\tOff time: " ); Serial.println( valOffTime );
#endif 
     
    }//if   
   
    digitalWrite( pinLED, HIGH);
    delay( valOnTime );                           
    digitalWrite( pinLED, LOW);
    delay( valOffTime );
   
}//loop
//clipping indicator variables
boolean clipping = 0;

//data storage variables
byte newData = 0;
byte prevData = 0;
unsigned int time = 0;//keeps time and sends vales to store in timer[] occasionally
int timer[10];//sstorage for timing of events
int slope[10];//storage for slope of events
unsigned int totalTimer;//used to calculate period
unsigned int period;//storage for period of wave
byte index = 0;//current storage index
float frequency;//storage for frequency calculations
int maxSlope = 0;//used to calculate max slope as trigger point
int newSlope;//storage for incoming slope data

//variables for decided whether you have a match
byte noMatch = 0;//counts how many non-matches you've received to reset variables if it's been too long
byte slopeTol = 3;//slope tolerance- adjust this if you need
int timerTol = 10;//timer tolerance- adjust this if you need

//variables for amp detection
unsigned int ampTimer = 0;
byte maxAmp = 0;
byte checkMaxAmp;
byte ampThreshold = 30;//raise if you have a very noisy signal

void setup(){
  
  Serial.begin(9600);
  
  pinMode(13,OUTPUT);//led indicator pin
  pinMode(12,OUTPUT);//output pin
  
  cli();//diable interrupts
  
  //set up continuous sampling of analog pin 0 at 38.5kHz
 
  //clear ADCSRA and ADCSRB registers
  ADCSRA = 0;
  ADCSRB = 0;
  
  ADMUX |= (1 << REFS0); //set reference voltage
  ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
  
  ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
  ADCSRA |= (1 << ADATE); //enabble auto trigger
  ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
  ADCSRA |= (1 << ADEN); //enable ADC
  ADCSRA |= (1 << ADSC); //start ADC measurements
  
  sei();//enable interrupts
}

ISR(ADC_vect) {//when new ADC value ready
  
  PORTB &= B11101111;//set pin 12 low
  prevData = newData;//store previous value
  newData = ADCH;//get value from A0
  if (prevData < 127 && newData >=127){//if increasing and crossing midpoint
    newSlope = newData - prevData;//calculate slope
    if (abs(newSlope-maxSlope)<slopeTol){//if slopes are ==
      //record new data and reset time
      slope[index] = newSlope;
      timer[index] = time;
      time = 0;
      if (index == 0){//new max slope just reset
        PORTB |= B00010000;//set pin 12 high
        noMatch = 0;
        index++;//increment index
      }
      else if (abs(timer[0]-timer[index])<timerTol && abs(slope[0]-newSlope)<slopeTol){//if timer duration and slopes match
        //sum timer values
        totalTimer = 0;
        for (byte i=0;i<index;i++){
          totalTimer+=timer[i];
        }
        period = totalTimer;//set period
        //reset new zero index values to compare with
        timer[0] = timer[index];
        slope[0] = slope[index];
        index = 1;//set index to 1
        PORTB |= B00010000;//set pin 12 high
        noMatch = 0;
      }
      else{//crossing midpoint but not match
        index++;//increment index
        if (index > 9){
          reset();
        }
      }
    }
    else if (newSlope>maxSlope){//if new slope is much larger than max slope
      maxSlope = newSlope;
      time = 0;//reset clock
      noMatch = 0;
      index = 0;//reset index
    }
    else{//slope not steep enough
      noMatch++;//increment no match counter
      if (noMatch>9){
        reset();
      }
    }
  }
    
  if (newData == 0 || newData == 1023){//if clipping
    PORTB |= B00100000;//set pin 13 high- turn on clipping indicator led
    clipping = 1;//currently clipping
  }
  
  time++;//increment timer at rate of 38.5kHz
  
  ampTimer++;//increment amplitude timer
  if (abs(127-ADCH)>maxAmp){
    maxAmp = abs(127-ADCH);
  }
  if (ampTimer==1000){
    ampTimer = 0;
    checkMaxAmp = maxAmp;
    maxAmp = 0;
  }
  
}

void reset(){//clea out some variables
  index = 0;//reset index
  noMatch = 0;//reset match couner
  maxSlope = 0;//reset slope
}


void checkClipping(){//manage clipping indicator LED
  if (clipping){//if currently clipping
    PORTB &= B11011111;//turn off clipping indicator led
    clipping = 0;
  }
}


void loop(){
  
  checkClipping();
  
  if (checkMaxAmp>ampThreshold){
    frequency = 38462/float(period);//calculate frequency timer rate/period
  
    //print results
    Serial.print(frequency);
    Serial.println(" hz");
  }
  
  delay(100);//delete this if you want
  
  //do other stuff here
}

and i was confused how i would switch between these two codes with a button or switch, I know im going to have to combines them but how? any help is appreciated!

tophat12079:
and i was confused how i would switch between these two codes with a button or switch, I know im going to have to combines them but how? any help is appreciated!

This is not enough information to help. What do you want the final code to do? How is that different from the two independent sketches you have presented? What do you mean by "switch between these two codes"?

Just saying you want to combine the code is not enough. You have to tell us what you want it to do and preferably make your best attempt at combing it.

sorry, so one code creates a variable pwm signal by looking at pins A1 (for frequency) and A2 (for duty cycle), and the other samples music on pin A0 and turns it into a pwm signal. both of these codes output on pin D13 for a fiber optic. what i need is to be able to switch between these codes either through software or a switch/button. that way in one position of the switch/code/button i can use a variable pwm signal, and in another i can take audio from my computer and have it turned into a pwm signal.

The Arduino runs only one program, so you have to write a program that can switch between doing two different things, possibly controlled by a switch or other input.

One way to do that is to use functions:

void loop() {
check_switch();
if (switch_is_set) function1();
else function2();
}

The quick and dirty way.

WARNING: The ISR is only fetching the HIGH half of the ADC register which only contains 2 bits of data. That is going to sound awful. Better to make ‘newData’ an unsigned int and fetch the entire ADC register.

const byte SwitchPin = 2;


void setup()
{
  pinMode(SwitchPin, INPUT_PULLUP);


  setup1();
  setup2();
}


void loop()
{
  if (digitalRead(SwitchPin))
    loop1();  // Pin not grounded
  else
    loop2();  // Pin grounded
}




#define SAMPLE_PERIOD       20ul
//#define DEBUG_MSG           0


const uint8_t pinPeriod = A1;
const uint8_t pinOnTime = A2;
const uint8_t pinLED = 13;


uint32_t
timeNow,
timeSample;


uint16_t
valPeriod,
valOnTime,
valOffTime;
float
fTemp;
void setup1()
{
  pinMode( pinPeriod, INPUT );
  pinMode( pinOnTime, INPUT );


  pinMode( pinLED, OUTPUT);


  Serial.begin( 9600 );


}//setup


void loop1()
{
  timeNow = millis();
  if ( timeNow - timeSample >= 50ul )
  {
    timeSample = timeNow;
    valPeriod = analogRead( pinPeriod );
    valPeriod = map( valPeriod, 0, 1023, 500, 20 );


    valOnTime = analogRead( pinOnTime );
    valOnTime = map( valOnTime, 0, 1023, 50, 100 );


    fTemp = (float)valPeriod * (float)valOnTime / 1000.0;
    valOnTime = (uint16_t)fTemp;
    valOffTime = valPeriod - valOnTime;


#if DEBUG_MSG
    Serial.print( "Period: " ); Serial.print( valPeriod );
    Serial.print( "\tOn time: " ); Serial.print( valOnTime );
    Serial.print( "\tOff time: " ); Serial.println( valOffTime );
#endif


  }//if


  digitalWrite( pinLED, HIGH);
  delay( valOnTime );
  digitalWrite( pinLED, LOW);
  delay( valOffTime );


}//loop










//clipping indicator variables
boolean clipping = 0;


//data storage variables
byte newData = 0;
byte prevData = 0;
unsigned int time = 0;//keeps time and sends vales to store in timer[] occasionally
int timer[10];//sstorage for timing of events
int slope[10];//storage for slope of events
unsigned int totalTimer;//used to calculate period
unsigned int period;//storage for period of wave
byte index = 0;//current storage index
float frequency;//storage for frequency calculations
int maxSlope = 0;//used to calculate max slope as trigger point
int newSlope;//storage for incoming slope data


//variables for decided whether you have a match
byte noMatch = 0;//counts how many non-matches you've received to reset variables if it's been too long
byte slopeTol = 3;//slope tolerance- adjust this if you need
int timerTol = 10;//timer tolerance- adjust this if you need


//variables for amp detection
unsigned int ampTimer = 0;
byte maxAmp = 0;
byte checkMaxAmp;
byte ampThreshold = 30;//raise if you have a very noisy signal


void setup2()
{


  Serial.begin(9600);


  pinMode(13, OUTPUT); //led indicator pin
  pinMode(12, OUTPUT); //output pin


  cli();//diable interrupts


  //set up continuous sampling of analog pin 0 at 38.5kHz


  //clear ADCSRA and ADCSRB registers
  ADCSRA = 0;
  ADCSRB = 0;


  ADMUX |= (1 << REFS0); //set reference voltage
  ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only


  ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
  ADCSRA |= (1 << ADATE); //enabble auto trigger
  ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
  ADCSRA |= (1 << ADEN); //enable ADC
  ADCSRA |= (1 << ADSC); //start ADC measurements


  sei();//enable interrupts
}


ISR(ADC_vect)  //when new ADC value ready
{


  PORTB &= B11101111; //set pin 12 low
  prevData = newData; //store previous value
  newData = ADCH; //get value from A0
  if (prevData < 127 && newData >= 127) //if increasing and crossing midpoint
  {
    newSlope = newData - prevData;//calculate slope
    if (abs(newSlope - maxSlope) < slopeTol) //if slopes are ==
    {
      //record new data and reset time
      slope[index] = newSlope;
      timer[index] = time;
      time = 0;
      if (index == 0) //new max slope just reset
      {
        PORTB |= B00010000;//set pin 12 high
        noMatch = 0;
        index++;//increment index
      }
      else if (abs(timer[0] - timer[index]) < timerTol && abs(slope[0] - newSlope) < slopeTol) //if timer duration and slopes match
      {
        //sum timer values
        totalTimer = 0;
        for (byte i = 0; i < index; i++)
        {
          totalTimer += timer[i];
        }
        period = totalTimer;//set period
        //reset new zero index values to compare with
        timer[0] = timer[index];
        slope[0] = slope[index];
        index = 1;//set index to 1
        PORTB |= B00010000;//set pin 12 high
        noMatch = 0;
      }
      else //crossing midpoint but not match
      {
        index++;//increment index
        if (index > 9)
        {
          reset();
        }
      }
    }
    else if (newSlope > maxSlope) //if new slope is much larger than max slope
    {
      maxSlope = newSlope;
      time = 0;//reset clock
      noMatch = 0;
      index = 0;//reset index
    }
    else //slope not steep enough
    {
      noMatch++;//increment no match counter
      if (noMatch > 9)
      {
        reset();
      }
    }
  }


  if (newData == 0 || newData == 1023) //if clipping
  {
    PORTB |= B00100000;//set pin 13 high- turn on clipping indicator led
    clipping = 1;//currently clipping
  }


  time++;//increment timer at rate of 38.5kHz


  ampTimer++;//increment amplitude timer
  if (abs(127 - ADCH) > maxAmp)
  {
    maxAmp = abs(127 - ADCH);
  }
  if (ampTimer == 1000)
  {
    ampTimer = 0;
    checkMaxAmp = maxAmp;
    maxAmp = 0;
  }


}


void reset() //clea out some variables
{
  index = 0;//reset index
  noMatch = 0;//reset match couner
  maxSlope = 0;//reset slope
}




void checkClipping() //manage clipping indicator LED
{
  if (clipping) //if currently clipping
  {
    PORTB &= B11011111;//turn off clipping indicator led
    clipping = 0;
  }
}


void loop2()
{


  checkClipping();


  if (checkMaxAmp > ampThreshold)
  {
    frequency = 38462 / float(period); //calculate frequency timer rate/period


    //print results
    Serial.print(frequency);
    Serial.println(" hz");
  }


  delay(100);//delete this if you want


  //do other stuff here
}

lol. That first code looks like I wrote it but I wouldn't use delay() would I??