Running Different methods with a switch.

Ok, so I’m not completely familiar with this programming interface and I need some help with a project.

Currently, I have two programs that I want to call by moving a switch. One program lights a bar of LEDs according to pressure given to an FSR. The other program plays a note (on a piezo element) whenever any amount of pressure is given to the FSR. I have both program working by themselves, but I don’t know how to incorporate a switch to change between them.

Also, not really a big deal, but on the Mode 1 program, the notes play as I press the FSR, but the for loop at the end doesn’t seem to be working properly. By that I mean that every time I hit it, the piezo only plays the first note, (ex: d, d, d, d, d).

Below is a snippet from each program. Please help guide me in the right direction.

Mode 1:

int length = 26;
char notes[] = "deedgededeedgededeedgede";
int beats[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int tempo = 300;

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'g2' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 805 };
  

  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void setup() {
  pinMode(speakerPin, OUTPUT);
}

void loop() {
  int value = analogRead(sensorPin);
  for (int i = 0; i < length; i++) {
    if (value > 0) {
      playNote(notes[i], beats[i] * tempo);
      i++;
      value = 0;
    } else {
      delay(0);
      i++;
    }
  }
}

Mode 2:

void loop() {
  int value1 = analogRead(sensePin1);
  int value2 = analogRead(sensePin2);
    
  if ((value1 > 0) && (value1 < 250)) {
    analogWrite(ledPin1, value1);
  } 
  else if ((value1 > 250) && (value1 < 500)) {
    analogWrite(ledPin1, 9600);
    analogWrite(ledPin2, value1);
  } 
  else if ((value1 > 500) && (value1 < 750)) {
    analogWrite(ledPin1, 9600);
    analogWrite(ledPin2, 9600);
    analogWrite(ledPin3, value1);
  } 
  else if ((value1 > 750) && (value1 < 1000)) {
    analogWrite(ledPin1, 9600);
    analogWrite(ledPin2, 9600);
    analogWrite(ledPin3, 9600);
    analogWrite(ledPin4, value1);
  } 
  else {
    analogWrite(ledPin1, 0);
    analogWrite(ledPin2, 0);
    analogWrite(ledPin3, 0);
    analogWrite(ledPin4, 0);
  }
  if ((value2 > 0) && (value2 < 250)) {
    analogWrite(ledPin5, value2);
  } 
  else if ((value2 > 250) && (value2 < 500)) {
    analogWrite(ledPin5, 9600);
    analogWrite(ledPin6, value2);
  } 
  else if ((value2 > 500) && (value2 < 750)) {
    analogWrite(ledPin5, 9600);
    analogWrite(ledPin6, 9600);
    analogWrite(ledPin7, value2);
  } 
  else if ((value2 > 750) && (value2 < 1000)) {
    analogWrite(ledPin5, 9600);
    analogWrite(ledPin6, 9600);
    analogWrite(ledPin7, 9600);
    analogWrite(ledPin8, value2);
  } 
  else {
    analogWrite(ledPin5, 0);
    analogWrite(ledPin6, 0);
    analogWrite(ledPin7, 0);
    analogWrite(ledPin8, 0);
  }
}

Thanks!

You simply need to put the code in the two "loop"s into functions "mode1" and "mode2", then your new "loop" selects between them.

Thanks for the quick response!

So something like this?:

void loop() {
if (something = 0) {
  ##Run mode1;
} else {
  ##Run mode2; 

void mode1() {
##code##
}

void mode2() {
##code##
}

I just bought a basic power switch from Radioshack, how can I wire it to make my program recognize the switch? Also, I know this is a very amateur question, but how can I call each method from the loop?

Wire the switch between ground and a digital pin. Make the digital pin an input, and enable the built in pull up resistor for the pin. Read about how to perform a comparison in C using ==