Reset a boolean function

Hi !

I have another question. How can i reset a return value from a boolean function? Because if i do not reset, once it return true, it will always return true the second time it goes into the loop. I have tried some but none is functioning. Glad if anyone could help.
Thanks :slight_smile:

if (state == 5) { // detectar la caida del producto
    static unsigned long previousMillis;
    previousMillis = millis();
    do {
      caidaProducto();
    }
    while ((millis() - previousMillis < 20000) && !caidaProducto());
    if (caidaProducto()) {
      Serial.println("recoger su bebida ... ");
      state = 6;
      caidaProducto() != caidaProducto(); // reset caida producto bool from true to false
    }
    else {
      Serial.println("volver a dispensar el producto... ");
      state = 4;
    }
  }

that’s a test (which result is not used), not an assignment…

1 Like

caidaProducto is a function, not a variable. Presumably it returns a value based on… something.

You cannot assign a value to a function in this manner, nor instruct a function to change its behaviour.

Please supply your entire program, yes, the whole thing

OR

the smallest example you can create, in the context of a complete program that we could run for ourselves, that demonstrates you problem or illustrates what you are trying to do.

With that, what you have provided is nearly meaningless.

We look forward to helping.

a7

1 Like

Hello everyone! i have a problem. i am totally confused. When i make this only function in a new file, it’s totally working. but when i try to put the exact same function together with some other function, it doesn’t working. Can anyone solve my problem or tell me where did i do wrong ?
Thanks in advanced.
P/s : I only post a snippet of my coding because the whole coding is quite long

#include "MapFloat.h"
#include <Keypad.h>

const int LED_ERROR = 10;
const int LED_REFRIGERACION = A4;
const int LED_MOTOR1 = A2;
const int LED_MOTOR2 = A3;
const int LED_CAMBIOS = 12;
const int BUTTON_CAIDA = 11;
const int BUTTON_IMPORTE = A5;
const int UPS = A0;
const int SENSOR_TEMPERATURA = A1;

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the kpd

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

String refrescos[]  = {"A35", "A36", "A37", "A38", "A39", "A40"};
String input_codigo;
unsigned long previousMillis = 0;
float importe = 0.00;
float precio;
String refresco;
int state;



void setup() {
  // put your setup code here, to run once:
  pinMode(LED_ERROR, OUTPUT);
  pinMode(LED_REFRIGERACION, OUTPUT);
  pinMode(LED_MOTOR1, OUTPUT);
  pinMode(LED_MOTOR2, OUTPUT);
  pinMode(LED_CAMBIOS, OUTPUT);
  pinMode(BUTTON_CAIDA, INPUT_PULLUP);
  pinMode(BUTTON_IMPORTE, INPUT_PULLUP);
  pinMode(UPS, INPUT);
  pinMode(SENSOR_TEMPERATURA, INPUT);
  Serial.begin(9600);
  input_codigo.reserve(32);
}

.
.
.
.
.
.
.
if (state == 5) { // detectar la caida del producto
    static unsigned long previousMillis;
    previousMillis = millis();
    do {
      Serial.println("previous millis");
      Serial.println(previousMillis);
      Serial.println("current millis");
      Serial.println(millis());
     // bool caye = caidaProducto();
      if (caidaProducto()) {
        Serial.println("el producto se ha caido ... ");
        state = 6;
      }
      else {
        Serial.println("volver a dispensar el producto... ");
        state = 4;
      }
    }
    while ((millis() - previousMillis < 10000) && !caidaProducto());
    Serial.println("sale de while");

  }

.
.
.
.
.
.
//deteccion de la caida producto
bool caidaProducto() {
  int lastState = LOW;  // the previous state from the input pin
  int currentState;     // the current reading from the input pin
  unsigned long pressedTime  = 0;
  bool isPressing = false;
  bool estaCayendo = false;

  currentState = digitalRead(BUTTON_CAIDA);
  if (lastState == HIGH && currentState == LOW) {       // button is pressed
    pressedTime = millis(); //Save the instants when the button is pressed
    isPressing = true;
    estaCayendo = false;
  } else if (lastState == LOW && currentState == HIGH) { // button is released
    isPressing = false;
  }

  if (isPressing == true && estaCayendo == false) { // if button is pressed
    long pressDuration = millis() - pressedTime; // calculate the time when the button is pressed

    if ( pressDuration > 5000 ) {
      Serial.println("El producto se ha caido");
      estaCayendo = true;
    }
  }
  lastState = currentState;  // save the the last state
  return estaCayendo;
}

Please describe the problem fully. What does not working mean ? What should the program do, what does it actually do ?

The problem is once it’s executed the do it always goes to the else and return to state=4 even if the function caidaProducto is true. It shoud go to the if and move to the next state=6, right ? I’ll post the caidaProducto function below the coding.

Hello
please forward the sketch detail for

 caidaProducto();
1 Like

Topics on the same subject merged

1 Like
bool caidaProducto() {
  int lastState = LOW;  // the previous state from the input pin
  int currentState;     // the current reading from the input pin
...
  lastState = currentState;  // save the the last state
  return estaCayendo;
}

lastState is not static, so you won’t be saving the last state for the next calll

to your other more general question, assuming you have a function returning a static variable that once it gets to true stays to true and you want to reset that static variable, you need to find a way to tell the function to reset. An easy solution is to have a parameter with a default value. Here is an example

bool getBooleanState(bool forceReset = false) {
  static bool myPermanentValue = false;
  if (forceReset) myPermanentValue = false;
  // here goes your code
  myPermanentValue |= (random(0, 101) > 50); // whatever but once true stays true until you force a reset
}

when you use the function in a normal way, you call just getBooleanState(); but when you want to reset the state to false and start fresh, you would call getBooleanState(true);

can you please give a brief explanation about what does each code means because i’m not quite understand :grimacing: thanks

Please fully answer the question asked in reply #5.

Okay. Here is my problem. I need to program a vending machine. So everytime it goes into the loop it need to check over and over again. And for this snippet of coding ( i’ll post it again), the program need to check whether the product have fallen or not (that’s what caidaProducto function does). So for the first time i run, i push the button so that caidaProducto returns true. But when it goes for the second time into the loop, it also returns true even if i did not push the button. So the problem is i don’t know how can i reset the caidaProducto function to false so that when it goes into the loop for a second time, it need to check whether the button has been pushed or not.
I don’t know if i explained myself well. Anyway here is my code:

#include "MapFloat.h"
#include <Keypad.h>

const int LED_ERROR = 10;
const int LED_REFRIGERACION = A4;
const int LED_MOTOR1 = A2;
const int LED_MOTOR2 = A3;
const int LED_CAMBIOS = 12;
const int BUTTON_CAIDA = 11;
const int BUTTON_IMPORTE = A5;
const int UPS = A0;
const int SENSOR_TEMPERATURA = A1;

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the kpd

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

String refrescos[]  = {"A35", "A36", "A37", "A38", "A39", "A40"};
String input_codigo;
unsigned long previousMillis = 0;
float importe = 0.00;
float precio;
String refresco;
int state;
int lastState = LOW;  // the previous state from the input pin
int currentState;     // the current reading from the input pin
unsigned long pressedTime  = 0;
bool estaCayendo = false;
bool isPressing = false;


void setup() {
  // put your setup code here, to run once:
  pinMode(LED_ERROR, OUTPUT);
  pinMode(LED_REFRIGERACION, OUTPUT);
  pinMode(LED_MOTOR1, OUTPUT);
  pinMode(LED_MOTOR2, OUTPUT);
  pinMode(LED_CAMBIOS, OUTPUT);
  pinMode(BUTTON_CAIDA, INPUT_PULLUP);
  pinMode(BUTTON_IMPORTE, INPUT_PULLUP);
  pinMode(UPS, INPUT);
  pinMode(SENSOR_TEMPERATURA, INPUT);
  Serial.begin(9600);
  input_codigo.reserve(32);
}
.
.
.
.
.
.
.
.
.
if (state == 5) { // detectar la caida del producto
    static unsigned long previousMillis;
    previousMillis = millis();
    do {
      caidaProducto();
    }
    while ((millis() - previousMillis < 20000) && !caidaProducto());
    if (caidaProducto()) {
      Serial.println("recoger su bebida ... ");
      state = 6;
      //caidaProducto() == false;
    }
    else {
      Serial.println("volver a dispensar el producto... ");
      state = 4;
    }
  }
.
.
.
.
.
.
.
.
.
bool caidaProducto() {
  currentState = digitalRead(BUTTON_CAIDA);
  if (lastState == HIGH && currentState == LOW) {       // button is pressed
    pressedTime = millis(); //Save the instants when the button is pressed
    isPressing = true;
    estaCayendo = false;
  } else if (lastState == LOW && currentState == HIGH) { // button is released
    isPressing = false;
  }

  if (isPressing == true && estaCayendo == false) { // if button is pressed
    long pressDuration = millis() - pressedTime; // calculate the time when the button is pressed

    if ( pressDuration > 1000 ) {
      estaCayendo = true;
    }
  }
  lastState = currentState;  // save the the last state
  return estaCayendo;// here goes your code
}

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