Like others I am just starting with arduino and programming... My only programming experience was with basic 20 years ago, so I am clueless...
This is what I'm trying to accomplish:
1-. I have 1 toggle switche On-Off (SWITCH). This SWITCH if On will disable the VAR after 1000 ms, if Off it won't disable the VAR
2-. ANALOG is just an analog reading and THRESHOLD is a set value (threshold).
Conditions:
If ANALOG is > SET and SWITCH is Off, I need the VAR Hi all the time.
If ANALOG is > SET and SWITCH is On, I need the VAR Hi for 1000ms and then Off
Please note that I will be using VAR in different parts of the program to activate digital outputs or different process (like if VAR==1, then activate something)
Again, my programming knowledge is ZERO, but trying this is what I did. Goes in the voild loop() part. All the variables, I/O's and everything else is done before, I am just showing the control part... Do you guys know a better way to do it...
ANALOG = analogRead(SENSOR_1_APIN);
if (ANALOG > SET && SWITCH==0) {
VAR==1;
}
else if (ANALOG > SET && SWITCH==1) {
VAR==1;
delay(1000);
VAR==0;
}
Personally I would not use an 'else if' structure, there is no need for the else, just have the if part.
You are not missing anything blinding obvious, you could if you want use a 'switch' construct but what you are doing looks as good as anything else.
Keep in mind delay is blocking. If you need another part of your program to do something for the 1 second that VAR = 1 then you will need to try a different approach. If you take a peek at the "Blink Without Delay" example it should give you some ideas.
I pieced together some code using some of my libraries.
This assumes that you need to have the button/switch pressed AS the analogReading exceeds the ANALOG_THRESHOLD.
Button button = Button(12,PULLUP); //switch at digital pin 12
const int ANALOG_THRESHOLD = 512; //the threshold to exceed
const byte ANALOG_PIN = 2; //the analog pin to read
int analogReading = 0; //the reading
byte state = 0; //previously called VAR
void setup(){
pinMode(ANALOG_PIN,INPUT);
}
void loop(){
//maybe it's time to reset?
scheduler.update();
//read the analog pin
analogReading = analogRead(ANALOG_PIN);
//act upon the reading
if (analogReading>ANALOG_THRESHOLD){
state = 1;
if (button.isPressed()){
scheduler.schedule(reset,1000); //schedule a reset after 500 milliseconds
}
}