void loop doesn't loop back if the state change

Can someone help me to check why after it change state I have to restart Arduino again and again. seems like it doesn't check the condition in the loop.

void nescafeFunction() {

currentTime = millis();
if (secondFlag2 == 0) { //delay time for button, 6 seconds for button 1
startTime = millis();
secondFlag2 = 1; // show timer as running, set an output pin high
servo2.write(105);
digitalWrite(return2, LOW);
}

//Button will turn off the latch after 6 sec count
if (secondFlag2 == 1) {
elapsedTime = currentTime - startTime;
if (elapsedTime >= 4000) { // declare 5seconds = 5000UL;
// show timer as stopped, clear the output pin
servo2.write(0);
delay(5000);
secondFlag2 = 0;

////////////////////// Last stage
digitalWrite(return2, HIGH);
if (digitalRead(return2) == HIGH) {
loop();

}
}
}
}

//////////////////////////////////////////////////////////////////////

void loop() {
Serial.begin(9600);
currentTime = millis();
int button_milo = digitalRead(milo);
int button_nescafe = digitalRead(nescafe);

nescafe_1:
if (button_nescafe == HIGH) {
nescafeFunction();
}
if (button_nescafe == LOW) {
servo2.write(0);
goto milo_1;
goto nescafe_1;
}

milo_1:
if (button_milo == HIGH) {
miloFunction();
}
if (button_milo == LOW) {
servo1.write(0);
goto nescafe_1;
goto milo_1;
}

loop();

}

Where are the code tags? Read How to post code properly and then use the </> icon to create [code]...[/code] tags around your code so that it is easier to read.

Where's the setup function?

You can't call the loop function from within the loop function (aka recursion).

   goto milo_1;
    goto nescafe_1;

Using a goto statement is really frowned upon but what is really, really wrong is having one after the other. How is that second goto ever going to be executed?

Pete

thanks for the reply, I'm really desperate that why I include goto function, even though I remove is the same, that why I need to find a way how to make my void loop become real loop keep on checking the state if milo or nescafe is high. seems like my program just only flow once. if I change the state to low, I have to reset it in order to get that result.

Sound like Malaysian code..haha

sorry I'm not Malaysian but I live near by there.

Hi,
From what I can see of your code you need to ask only two questions.

  • IF nescafe button HIGH and milo button LOW then nescafefunctom
  • IF milo button HIGH and nescafe button LOW then milofunction.

You only need two IF statements to cover your two possibilities, any other combination will not jump into the functions.

Where is you milofunction?

Tom... :slight_smile:

Hi,
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

We need to see you complete code.

Thanks.. Tom.. :slight_smile:

thanks for the reply. yes I understand if there is two statement we can use if, but is it possible we use call function which looks more nicer. I just wondering, how come call function cant use if statement and become as a loop. Every time if I change the state I have to reset my Arduino to make the things goes well. Any suggestion on this matter?

Hi,
Can you post your complete code, you are missing void setup(), and milofunction and variable declarations.

Thanks.. Tom. :slight_smile:

////////////////////////////////////////////////////////////////////////////////////////

void miloFunction() {

  currentTime = millis();
  if (secondFlag1 == 0) { //delay time for button, 6 seconds for button 1
    startTime = millis();
    secondFlag1 = 1; // show timer as running, set an output pin high
    servo1.write(105);
    digitalWrite(return1, LOW);
  }
  //Button will turn off the latch after 6 sec count
  if (secondFlag1 == 1) {
    elapsedTime = currentTime - startTime;
    if (elapsedTime >= 4000) { // declare 5seconds = 5000UL;
      // show timer as stopped, clear the output pin
      servo1.write(0);
      delay(5000);
      secondFlag1 = 0;
      ////////////////////// Last stage
      digitalWrite(return1, HIGH);
      if (digitalRead(return1) == HIGH) {
        loop();
      }
    }

  }
}

/////////////////////////////////////////////////////////////////////////////////////////

void nescafeFunction() {

  currentTime = millis();
  if (secondFlag2 == 0) { //delay time for button, 6 seconds for button 1
    startTime = millis();
    secondFlag2 = 1; // show timer as running, set an output pin high
    servo2.write(105);
    digitalWrite(return2, LOW);
  }

  //Button will turn off the latch after 6 sec count
  if (secondFlag2 == 1) {
    elapsedTime = currentTime - startTime;
    if (elapsedTime >= 4000) { // declare 5seconds = 5000UL;
      // show timer as stopped, clear the output pin
      servo2.write(0);
      delay(5000);
      secondFlag2 = 0;

      ////////////////////// Last stage
      digitalWrite(return2, HIGH);
      if (digitalRead(return2) == HIGH) {
        loop();

      }
    }
  }
}



//////////////////////////////////////////////////////////////////////





void loop() {
  Serial.begin(9600);
  currentTime = millis();
  int button_milo = digitalRead(milo);
  int button_nescafe = digitalRead(nescafe);

nescafe_1:
  if (button_nescafe == HIGH) {
    nescafeFunction();
  }
  if (button_nescafe == LOW) {
    servo2.write(0);
    goto milo_1;
    goto nescafe_1;
  }

milo_1:
  if (button_milo == HIGH) {
    miloFunction();
  }
  if (button_milo == LOW) {
    servo1.write(0);
    goto nescafe_1;
    goto milo_1;
  }

  loop();

}

////////////////////////////////////////////////////////////////////////////////////////////

Hi,
That code does not compile, so how do you know what its doing?

Tom... :slight_smile:

please check your pm. I send you all my coding

Hi,
Post it here, you don't just need me to help you.

Tom... :slight_smile:

Hi,
This is what your loop basically should look like.

void loop()
{
  unsigned long currentTime = millis();
  int button_milo = digitalRead(miloPin);
  int button_nescafe = digitalRead(nescafePin);

  if (button_nescafe == HIGH && button_milo == LOW)
  {
    nescafeFunction();
  }

  if (button_nescafe == LOW && button_milo == HIGH)
  {
    miloFunction();
  }
}

Note the names for the pin variables, it makes them self explanatory.
Tom... :slight_smile:

#include <Servo.h>

unsigned long currentTime = 0;
unsigned long elapsedTime;
unsigned long startTime;
int secondFlag1 = 0;
int secondFlag2 = 0;
int milo = 3;
int nescafe = 5;
int return1;
int return2;

Servo servo1; //for milo valve
Servo servo2; //for nescafe valve
Servo servo3; //for sugar


void setup() {
  //pinMode(button,INPUT);
  pinMode (milo, INPUT);
  pinMode (nescafe, INPUT);
  servo1.attach(9);
  servo2.attach(10);
  servo3.attach(11);


  servo1.write(0);
  servo2.write(0);
}

////////////////////////////////////////////////////////////////////////////////////////

void miloFunction() {

  currentTime = millis();
  if (secondFlag1 == 0) { //delay time for button, 6 seconds for button 1
    startTime = millis();
    secondFlag1 = 1; // show timer as running, set an output pin high
    servo1.write(105);
    digitalWrite(return1, LOW);
  }
  //Button will turn off the latch after 6 sec count
  if (secondFlag1 == 1) {
    elapsedTime = currentTime - startTime;
    if (elapsedTime >= 4000) { // declare 5seconds = 5000UL;
      // show timer as stopped, clear the output pin
      servo1.write(0);
      delay(5000);
      secondFlag1 = 0;
      ////////////////////// Last stage
      digitalWrite(return1, HIGH);
      if (digitalRead(return1) == HIGH) {
        loop();
      }
    }

  }
}

/////////////////////////////////////////////////////////////////////////////////////////

void nescafeFunction() {

  currentTime = millis();
  if (secondFlag2 == 0) { //delay time for button, 6 seconds for button 1
    startTime = millis();
    secondFlag2 = 1; // show timer as running, set an output pin high
    servo2.write(105);
    digitalWrite(return2, LOW);
  }

  //Button will turn off the latch after 6 sec count
  if (secondFlag2 == 1) {
    elapsedTime = currentTime - startTime;
    if (elapsedTime >= 4000) { // declare 5seconds = 5000UL;
      // show timer as stopped, clear the output pin
      servo2.write(0);
      delay(5000);
      secondFlag2 = 0;

      ////////////////////// Last stage
      digitalWrite(return2, HIGH);
      if (digitalRead(return2) == HIGH) {
        loop();

      }
    }
  }
}



//////////////////////////////////////////////////////////////////////





void loop() {
  Serial.begin(9600);
  currentTime = millis();
  int button_milo = digitalRead(milo);
  int button_nescafe = digitalRead(nescafe);

nescafe_1:
  if (button_nescafe == HIGH) {
    nescafeFunction();
  }
  if (button_nescafe == LOW) {
    servo2.write(0);
    goto milo_1;
    goto nescafe_1;
  }

milo_1:
  if (button_milo == HIGH) {
    miloFunction();
  }
  if (button_milo == LOW) {
    servo1.write(0);
    goto nescafe_1;
    goto milo_1;
  }

  loop();

}

////////////////////////////////////////////////////////////////////////////////////////////

Hi,
Good that compiled.

Put Serial.begin(9600); in the setup not loop.

You need to review how you do your process, you should not need "goto" statements.

Tom.. :slight_smile:

Don't call loop() from any of your code. It can result in something called recursion and will sooner or later result in memory problems and a crash.

Can't follow your code at this moment as I using a cell phone but that stood out.

Hi,
Can you describe in point form what process is nescafe and milo.
What do they control and for how long.
What your input and outputs are.

Thanks.. Tom.. :slight_smile:

  1. Using mobile apps to select either milo or nescafe
  2. choose sugar level (which I haven't put in)
  3. if we choose milo, servo1 will turn to open ball valve for at least 3-5 seconds
  4. Same as nescafe too.

now the problem is when I manually by pass milo as HIGH
it goes to milo function to turn the servo.

but if I set milo as LOW and nescafe as HIGH.
it wont go in to nescafe function. unless I reset my arudino again.

Hi,
Thanks for the explanation of the functions, so each just operates a different servo/valve to dispense coffee or chocolate.
Have you tried the if statements I suggested in post #13?

but if I set milo as LOW and nescafe as HIGH.
it wont go in to nescafe function. unless I reset my arudino again.

That would be because of the convoluted goto's and loop(); statements you have sprinkled through your code.

What are you trying to do with return1 and return2, they are both 0, yet you use them like pin numbers.

digitalWrite(return1, LOW);

And;

  digitalWrite(return1, HIGH);
      if (digitalRead(return1) == HIGH) {
        loop();
      }

Tom.... :slight_smile:
(I'm off to bed, nearly midnight here... :sleeping: )