Boolean not toggling

I'm not able to figure out how to call my sweep() function - I'm using a momentary button to toggle modes.

#define BUTTON 4
int lastButtonState = HIGH;
boolean mode = false;

void setup() {
pinMode(BUTTON, INPUT_PULLUP);
}

void loop()
{
  int buttonState = digitalRead(BUTTON);
  if ((buttonState != lastButtonState) && (buttonState == HIGH) ) {
    mode = !mode;
    Serial.println("mode not mode");
    Serial.println(mode);
  }
  lastButtonState = buttonState;
  if (mode = false) {
    sweep();
  }
  else {
    cw();
  }
}

mode always returns 1 when the button is released and the if statement executes, so cw() always runs...

You need an extra = in your test for the value of mode.

Thanks, that worked!

Now, the functions take too long and a button state change is rarely seen. I believe both interrupts are already being used by an encoder library (I think the 328p has only 2 interrupts).

Is there another way to toggle between running two functions?

You are not using interrupts in the code you posted. What do the sweep() and cw() functions do ? I would bet that they use the delay() function. Am I correct ? Please post the whole program.

It is for controlling a frequency source generator. At some point, I want to set the button to cycle through various parameters and have the encoder change the value:

mode: cw | sweep
F1: start frequency, cycling through MHz, kHz, Hz
F2: stop frequency, cycling through MHz, kHz, Hz
fStep: step size, cycling through MHz, kHz, Hz
etc.

//AD9850 DDS test
#define DDS_CLOCK 124999100
#define  CLOCK  13  //pin connections for DDS
#define  LOAD 6
#define  DATA  11 
#define  RESET 9  
#define BUTTON 4
int lastButtonState = HIGH;
boolean mode = false;

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
Encoder myEnc(2, 3);
//   avoid using pins with LEDs attached
long oldPosition  = -999;

void setup()
{
  Serial.begin(9600);
  Serial.println("AD9850 DDS");
  Serial.println("Basic Encoder Test:");
  pinMode (CLOCK, OUTPUT); 
  pinMode (LOAD,  OUTPUT); 
  pinMode (DATA,  OUTPUT); 
  pinMode (RESET, OUTPUT); 
  AD9850_init();
  AD9850_reset();
  SetFrequency(10000000);
  Serial.println("10,000,000 Hz");
  pinMode(BUTTON, INPUT_PULLUP);
}

void loop()
{
  //read the pushbutton value into a variable
  int buttonState = digitalRead(BUTTON);
  if ((buttonState != lastButtonState) && (buttonState == HIGH) ) {
    mode = !mode;
    Serial.println("mode not mode");
    Serial.println(mode);
  }
  lastButtonState = buttonState;

  if (mode == false) {
    cw();
  }
  else {
    sweep();
  }
}

void cw()
{
  Serial.println("AD9850 DDS");
  Serial.println("CW Mode");
  long freq = 1000000;
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    freq = freq - (25 * newPosition);
    SetFrequency(freq);
  }
  Serial.println(long (freq));
}

void sweep()
{
  Serial.println("AD9850 DDS");
  Serial.println("Sweep Mode");
  long freq = 990000;
  long F2 = 1010000;
  long fStep = 1;
  Serial.print("Start Frequency: ");
  Serial.print(long (freq));
  Serial.println(" Hz");
  long timeStart = micros();
  SetFrequency(freq);  // freq
  while (freq < F2) {
    SetFrequency(freq);  // freq
    freq = freq + fStep;
  }
  long timeStop = micros();
  Serial.print("Stop Frequency: ");
  Serial.print(long (freq));
  Serial.println(" Hz");
  Serial.print("Start Time: ");
  Serial.print(timeStart);
  Serial.println(" us");
  Serial.print("Stop Time: ");
  Serial.print(timeStop);
  Serial.println(" us");
  Serial.print("Sweep Time: ");
  Serial.print((timeStop - timeStart));
  Serial.println(" us");
  Serial.print("Switching Time: ");
  Serial.print((timeStop - timeStart) / freq);
  Serial.println(" us/pt");
  Serial.println("");
}

void SetFrequency(unsigned long frequency)
{
//  unsigned long tuning_word = (frequency * pow(2, 32)) / DDS_CLOCK;
int32_t tuning_word = (frequency * 0x100000000LL)/124999100L;
//int32_t tuning_word = frequency * 4294967296LL/124999100L;

  digitalWrite (LOAD, LOW); 
  shiftOut(DATA, CLOCK, LSBFIRST, tuning_word);
  shiftOut(DATA, CLOCK, LSBFIRST, tuning_word >> 8);
  shiftOut(DATA, CLOCK, LSBFIRST, tuning_word >> 16);
  shiftOut(DATA, CLOCK, LSBFIRST, tuning_word >> 24);
  shiftOut(DATA, CLOCK, LSBFIRST, 0x0);
  digitalWrite (LOAD, HIGH); 
}

void AD9850_init()
{
  digitalWrite(RESET, LOW);
  digitalWrite(CLOCK, LOW);
  digitalWrite(LOAD, LOW);
  digitalWrite(DATA, LOW);
}

void AD9850_reset()
{
  //reset sequence is:
  // CLOCK & LOAD = LOW
  //  Pulse RESET high for a few uS (use 5 uS here)
  //  Pulse CLOCK high for a few uS (use 5 uS here)
  //  Set DATA to ZERO and pulse LOAD for a few uS (use 5 uS here)
  // data sheet diagrams show only RESET and CLOCK being used to reset the device, but I see no output unless I also
  // toggle the LOAD line here.
  digitalWrite(CLOCK, LOW);
  digitalWrite(LOAD, LOW);
  digitalWrite(RESET, LOW);
  delay(5);
  digitalWrite(RESET, HIGH);  //pulse RESET
  delay(5);
  digitalWrite(RESET, LOW);
  delay(5);
  digitalWrite(CLOCK, LOW);
  delay(5);
  digitalWrite(CLOCK, HIGH);  //pulse CLOCK
  delay(5);
  digitalWrite(CLOCK, LOW);
  delay(5);
  digitalWrite(DATA, LOW);    //make sure DATA pin is LOW
  digitalWrite(LOAD, LOW);
  delay(5);
  digitalWrite(LOAD, HIGH);  //pulse LOAD
  delay(5);
  digitalWrite(LOAD, LOW);
  // Chip is RESET now
}

OK, I tried reassigning pins so encoder uses pins 4 & 5 and the encoder still works fine, now the button uses pin 2 with attachinterrupt, and that should work. I tried it with failure I bet because I specified the pin # instead of the interrupt #.