"Pause" void loop while another function is running

float voltage;
float volts;
unsigned int level;


//control pins for LED bar-graph
unsigned int con1  = 17;
unsigned int con2  = 16;
unsigned int con3  = 9;
unsigned int con4  = 8;
unsigned int con5  = 7;
unsigned int con6  = 6;
unsigned int con7  = 5;
unsigned int con8  = 4;
unsigned int con9  = 3;
unsigned int con10 = 2;

unsigned int learnModePin = 10;

bool learnModeActive = false;

const int LONG_TOGGLE_TIME = 3000; //3 seconds

int lastState = LOW; //previous state of learn

bool isToggling = false;
bool isLongDetected = false;


unsigned long currentTime;   //variable to store current time.
unsigned long debounceTime = 50; //set debounce time in mS
unsigned long toggledTime = 0;

int learnPinState; //variable for storing "learn" pin state


const int voltSense = analogRead(A0);


void setup() {

  //Wire.begin(); //init i2c comms
  Serial.begin(9600);

  //Wire.beginTransmission(display_Addr);

  //set LED pins as outputs

  pinMode(con1,  OUTPUT);
  pinMode(con2,  OUTPUT);
  pinMode(con3,  OUTPUT);
  pinMode(con4,  OUTPUT);
  pinMode(con5,  OUTPUT);
  pinMode(con6,  OUTPUT);
  pinMode(con7,  OUTPUT);
  pinMode(con8,  OUTPUT);
  pinMode(con9,  OUTPUT);
  pinMode(con10, OUTPUT);
  pinMode(learnModePin, INPUT_PULLUP);


  // digitalWrite(SCK, LOW);


  //adcWorker();

}

void loop() {

  //Serial.println("Hello, I am serial data!");
  adcWorker();

  learnPinState = digitalRead(learnModePin);


  if (lastState == HIGH && learnPinState == LOW) { //learn pin toggled

    learnModeActive = false;

    toggledTime = millis();
    isToggling = true;
    isLongDetected = false;
    Serial.println("learn pin toggled");
  }
  else if (lastState == LOW && learnPinState == HIGH) { //pin is un-toggled

    if (!learnModeActive == true) {

      isToggling = false;
      Serial.println("learn pin un-toggled");

    }

  }


  if (isToggling == true && isLongDetected == false) {

    long toggleDuration = millis() - toggledTime;

    if (toggleDuration > LONG_TOGGLE_TIME) {


      isLongDetected = true;
      learnMode();

    }


  }

  lastState = learnPinState;


}


void adcWorker() {

  Serial.print("in ADC worker");

  volts = analogRead(voltSense);

  voltage =  ((volts) * (5.0 / 1023.0));




}




void levelGraph(void) {
//code
}

void learnMode(void) {

  Serial.print("In Learn Mode");

  Serial.println("tick");
  delay(1000);
  Serial.println("tock");
  delay(1000);


}

This is probably a stupid question,
I have some code in my void loop() which detects a long keypress on pin 10, this works and when long keypress has been detected I go to another function called "learnMode". How do I stay in learnMode until I have finished what i want to happen. As it is at the moment learnMode has a simple bit of code that prints some debug text to serial monitor, as soon as it has done this it does right back to void Loop - How do I stay in learnMode for as long as I like for example, until a pin is triggered.

How do I stay in learnMode for as long as I like for example, until a pin is triggered.

You could use a while loop in the function and only exit it and hence the function when the exit condition is met

if I do...

  while(learnModePin == LOW){
  Serial.print("In Learn Mode"); 
  Serial.println("tick");
  delay(1000);
  Serial.println("tock");
  delay(1000);

  }

the code does not run but the pin IS LOW
why is this?

unsigned int learnModePin = 10;
  while(learnModePin == LOW)

The value of learnModePin is 10. It will always be HIGH

You need to use digitalRead() to read the state of the pin and test the state

I realised that about 1000mS after I wrote the response, Oops. I only do software occasionally and when I do I forget some very basic stuff.

Thanks

I realised that about 1000mS after I wrote the response

BTDTGTTS

Some suggestions for your code :

Use variables of an appropriate type to save memory, ie do the pin numbers need to be ints (2 bytes) when their value is never going to exceed 255 (1 byte)

Use the const modifier for values that will not change, such pin numbers. That prevents problems should you try to change the value by accident somewhere in your program and the compiler will probably put the value in a register thus not using any memory at all

Put each { and } on its own line and use Auto Format in the IDE to tidy up the code. That makes code blocks easier to see because of the very visible { and } and the indentation. Auto Format can be configured to move the { and } to their own line and to remove blank lines in functions if you want

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.