# Variable rising edge

Hello, I hope you're fine.

My name is Jérôme, I'm a complete beginner with Arduino programmation.

I've already posted my question in the french forum, but nobody seems to be able to help me.

Here is my problem :

The value of IN1 (val_IN1) is "analysed" and is converted to the value of bf1 from 0 to 7.
When no button is pressed, the value of bf1 is 0.
The value of the variable f1 memorize the value of bf1.

Here is the expected behaviour : if bf1 == 1, f1 = 1, if bf1 == 2, f1 = 2, if bf1 == 2 again, f1 = 0.

Basically, I want to toggle the value of f1 depending on the value of bf1.

My problem is the value of f1 does a loop between 0 and the value of bf1 as you can see here :

I would like to be able to use the rising edge of the variable bf1, is it possible ?

Here is the code :

``````const int IN1 = A5;

int val_IN1 = 0;
int bf1 = 0;
int f1 = 0;
int f1_prec = 0;

long time = 0;      // the last time the output pin was toggled
long debounce = 200;// the debounce time, increase if the output flickers

void setup()
{
Serial.begin(9600);
pinMode(IN1, INPUT);
}

void loop()
{
float volt = sensorValue * (5.0 / 1023.0);
float val = sensorValue;
Serial.println(f1);

if (val_IN1 > 950                  && millis() - time > debounce) {bf1 = 1;}
if (val_IN1 > 750 && val_IN1 < 850 && millis() - time > debounce) {bf1 = 2;}
if (val_IN1 > 570 && val_IN1 < 670 && millis() - time > debounce) {bf1 = 3;}
if (val_IN1 > 430 && val_IN1 < 530 && millis() - time > debounce) {bf1 = 4;}
if (val_IN1 > 310 && val_IN1 < 410 && millis() - time > debounce) {bf1 = 5;}
if (val_IN1 > 200 && val_IN1 < 300 && millis() - time > debounce) {bf1 = 6;}
if (val_IN1 > 80  && val_IN1 < 180 && millis() - time > debounce) {bf1 = 7;}
if (val_IN1 < 50                   && millis() - time > debounce) {bf1 = 0;}

if (bf1 == 1)  {f1 = 1;}      if (bf1 == 1  &&         f1_prec == 1)             {f1 = 0;}
if (bf1 == 2)  {f1 = 2;}      if (bf1 == 2  &&         f1_prec == 2)             {f1 = 0;}
if (bf1 == 3)  {f1 = 3;}      if (bf1 == 3  &&         f1_prec == 3)             {f1 = 0;}
if (bf1 == 4)  {f1 = 4;}      if (bf1 == 4  &&         f1_prec == 4)             {f1 = 0;}
if (bf1 == 5)  {f1 = 5;}      if (bf1 == 5  &&         f1_prec == 5)             {f1 = 0;}
if (bf1 == 6)  {f1 = 6;}      if (bf1 == 6  &&         f1_prec == 6)             {f1 = 0;}
if (bf1 == 7)  {f1 = 7;}      if (bf1 == 7  &&         f1_prec == 7)             {f1 = 0;}

if (f1 == 0){f1_prec = 0;}
if (f1 == 1){f1_prec = 1;}
if (f1 == 2){f1_prec = 2;}
if (f1 == 3){f1_prec = 3;}
if (f1 == 4){f1_prec = 4;}
if (f1 == 5){f1_prec = 5;}
if (f1 == 6){f1_prec = 6;}
if (f1 == 7){f1_prec = 7;}
}
``````

Thank you very much in advance for you help and have a wonderfull day !

Jérôme.

Take a look at the StateChangeDetection example in the IDE

1 Like

All of that can be reduced to simple comparison and assignment

``````f1 = bf1;
if ( bf1 == f1_prec )  f1=0;
``````
1 Like

@UKHeliBob I had a look, I'm trying something, I'll post the code soon.

@blh64 , I tried but it don't work because bf1 always = 0 when no button is pressed, and if I remove the bf1 = 0, then f1 goes into an infinite loop....

Thank you very much for your help !!!

Time stamp variable 'time' is never updated anywhere. It should also be an 'unsigned long'.

You should begin this project again, keep almost nothing and start from the beginning. Make a clear specification in plain language for yourself before you even touch the programming keys. If you don't, your code will be a patchwork of guesses that probably won't work even after a lot of wasted time. Consider, step by step, what actions need to happen.

The lack of that planning, is probably why nobody can help you on the other forum.

Which Arduino are you using?

When no button is pressed, the value of bf1 is 0.
The value of the variable f1 memorize the value of bf1.

Here is the expected behaviour : if bf1 == 1, f1 = 1, if bf1 == 2, f1 = 2, if bf1 == 2 again, f1 = 0.

Basically, I want to toggle the value of f1 depending on the value of bf1.

What analog voltage corresponds to each level for bf1; if bf1==0 is 0 volts, then bf1==1 is x volts, bf1==2 is y volts, bf1==3 is z volts etc?

What is 'x'?

If you're using an AVR-based Arduino you might be able to use the analog comparator with a threshold set to, say, 0.5 of 'x'; use an analog comparator interrupt to signal the rising edge of the input and set to measure the analog input voltage some calibrated time after the rising edge to get around the settling time.

Which Arduino? What is the ADC value when each button is pressed?

Yes. That's what we call 'State Change Detection'. Try this sketch:

``````const int IN1 = A5;

int PreviousButtonID = 0;

unsigned long time = 0;      // the last time the input changed
unsigned debounce = 200;     // the debounce time, increase if the output flickers

int CurrentButton()
{

if (val > 950 ) return 1;
if (val > 750 && val < 850) return 2;
if (val > 570 && val < 670) return 3;
if (val > 430 && val < 530) return 4;
if (val > 310 && val < 410) return 5;
if (val > 200 && val < 300) return 6;
if (val > 80  && val < 180) return 7;

return 0;
}

void setup()
{
Serial.begin(9600);
}

void loop()
{
int buttonID = CurrentButton();
if (buttonID !=  PreviousButtonID && millis() - time > debounce)
{
time = millis();

// Value just changed to buttonID
if (buttonID == 0)
{
Serial.print("Button ");
Serial.print(PreviousButtonID);
Serial.println(" released.");
}
else
{
Serial.print("Button ");
Serial.print(buttonID);
Serial.println(" pressed.");
}

PreviousButtonID = buttonID;
}
}``````
1 Like

Okay then. As a complete newcomer, it is your choice to accept advise or not. Good Luck!

Hello @johnwasser and thank you for the sketch.

I'll give a try to it right now.

Thank you for your help !

Edit :

It worked ! I've learned a really important feature thank to you.

Thanks to everybody and have a very nice day !

Jérôme.

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