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 :
I have an analog input (IN1) which receive different voltages from pushbuttons.
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 :
@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....
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.
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.
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()
{
int val = analogRead(IN1);
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;
}
}