Issue with preselection counter reset

Hello,

I have built a program, with a counter Up / Down, with two range of counting.

The first, from 0 to 100, and second from 100 to 200.

The control of the range selection and the counting, is done by simply three buttons.
One for the range selection, and the rest for UP/DOWN counting.

I monitor my result via the IDE serial monitor, at 1200 bauds.

In generality, everything work like I want, except for one thing :

When I switch from range two to range one, normally, my monitor, will display the minimum range of my counting, from 100 it will pass to 0.

But it stay to 100, in parse of 0.

The range set, is done with "constrain" function, where some care to take with the emploiement of others function inside the brakets.

I tested it with just numerical variables, to insure me that my problem won't generated by this function, but without any success.

For me the problem, is when the counter pass from range 2 to range 1, it will not reset to the minimum range, or hold by a specific function, but after some test and using of oters functions, I cannot find why it hold like that.

If any idea or solution, I am open to listen.

Many thanks, in advance

v0.1.ino (1.2 KB)

Your question will get more attention if you post the code here.

Use code tags, one way is the <CODE/> button you'll see in the message composition window.

type or paste code here

Just replace the directions to paste your code with your code.

a7

I think you'll like it better at 115200 baud.


Hello,

Sorry, I am not very familiar with this new dashboard editor.

I put here my complete code directly :


//Variables 

// *** Input entries

int buttonUP = 8;
int buttonDN = 9;
int buttonSEL = 11;

// *** Digital convertion 

int ButtonUP = 0;
int ButtonDN = 0;
int ButtonSEL = 0;

// *** Counters

int ctr = 0;
int rge = 0;

// *** Preselection ranging 

int rgemin [] = {0, 100}; 
int rgemax [] = {101, 200};




void setup() {

// *** Hard Serial port 
Serial.begin(1200);


// *** Digital pull up setting 

pinMode(buttonUP, INPUT_PULLUP);
pinMode(buttonDN, INPUT_PULLUP);
pinMode(buttonSEL, INPUT_PULLUP);

}




void counter () {

// *** Digital control reading

ButtonUP = digitalRead (buttonUP);
ButtonDN = digitalRead (buttonDN);
ButtonSEL = digitalRead (buttonSEL);


// *** Range selector control 

if (ButtonSEL == LOW) {

rge = 1;

}

else {

rge = 0;

}

/*
// *** Range definition 

if(rge == 1) {

ctr = constrain (ctr, 100, 200);

}

else {

ctr == 0;
ctr = constrain (ctr, 0, 100);

}
*/


ctr = constrain (ctr, rgemin[rge], rgemax[rge]);


// *** Manual counting 

if (ButtonUP == LOW && ctr < 250 ) {

ctr ++;


}

if (ButtonDN == LOW && ctr > 0 ) {

ctr --;

}

// *** Serial port reading 

  Serial.print(rge);
  Serial.print(" ");
  Serial.println(ctr);


}

void loop() {

// Running sub program

  counter();
  delay(50);



}

Many thanks

https://docs.arduino.cc/language-reference/en/functions/math/constrain/

The function returns the following:

  • x: if the input parameter (x) is between a and b.
    
  • a: if the input parameter (x) is less than a.
    
  • b: if the input parameter (x) is greater than b.
    

so it works exactly as expected.

If you want sometyhing else, write your own function or use the code in comment or maybe function map may be of help.

yes,

It constrain work,

But when I switch the range from higher to lower, it stay at the lower reference of the upper range.

Even, without increasing the counter.

For exemple,

My counter is at 0, I switch to range 2 (100 to 200), the counter pass from 0 to 100 (minimum range), where i can increase and decrease normally, in the range.

But,

When, i switch to range 1 (0 to 100), the counter hold at 100, instead of 0.

If the function constrain, can switch the minimum counting value from 0 to 100, why it not do the reverse, from 100 to 0 ?

I think, a reset of the counter is necessary, but I have some problems with that, cause no ones of my tentatives work.

Or, it hold my counter to 0.

Map, will change the minimum division of a range,

It more appropriate to an analog value, but I don't know if it is appropriate with a counter.

But, I could search in this way too,

The real goal of my work is to use this counter to switch between two range of pictures.

One serie from 0 to 100, and second one from 100 to 200.
The first serie, is the normal range, and the second one is a setting range, for a gauge meter.

100 is a valid result for range1 so why would it return to 0?

When you switch ranges, if you always want it to go to the lowest value, add some more logic into your range button press

if (ButtonSEL == LOW) {
  rge = 1;
  ctr = rgemin[rge];
}
else {
  rge = 0;
  ctr = rgemin[rge];
}

Better yet, learn how to do these things when a button is pressed, not if a button is pressed. That way, you press the button, the range changes and then release the button and it doesn't revert to the other range.

Hello,

Many apologies, for the late replying.

I finally found the solution by implementing a new reset sintax, with a certain condition.

And now my issue left.


//Variables 

// *** Input entries

int buttonUP = 8;
int buttonDN = 9;
int buttonSEL = 11;
int raz = 12;

// *** Digital convertion 

int ButtonUP = 0;
int ButtonDN = 0;
int ButtonSEL = 0;
int Raz = 0;

// *** Counters

int ctr = 0;
int rge = 0;

// *** Preselection ranging 

int rgemin [] = {0, 101}; 
int rgemax [] = {100, 200};




void setup() {

// *** Hard Serial port 
Serial.begin(1200);


// *** Digital pull up setting 

pinMode(buttonUP, INPUT_PULLUP);
pinMode(buttonDN, INPUT_PULLUP);
pinMode(buttonSEL, INPUT_PULLUP);
pinMode(raz, INPUT_PULLUP);

}




void counter () {

// *** Digital control reading

ButtonUP = digitalRead (buttonUP);
ButtonDN = digitalRead (buttonDN);
ButtonSEL = digitalRead (buttonSEL);
Raz = digitalRead (raz);


// *** Range selector control 

if (ButtonSEL == LOW) {

rge = 1;


}

else {

rge = 0;


}

// *** Counter reinitialisation 


if (rge == 0 && ctr > rgemax[rge]) {

ctr = 0;

}

// *** Range limiting 

ctr = constrain (ctr, rgemin[rge], rgemax[rge]);


// *** Manual counting 

if (ButtonUP == LOW && ctr < 250 ) {

ctr ++;


}

if (ButtonDN == LOW && ctr > 0 ) {

ctr --;

}

// *** Serial port reading 

  Serial.print(rge);
  Serial.print(" ");
  Serial.println(ctr);


}

void loop() {

// Running sub program

  counter();
  delay(50);



}


I understood after tested it with a new raz button.

Effectively, after a certain time behind my computer, my neurons unconnects and soften...