# simple, but not so simple.

Was working on what seemed like an easy project in theory, but not so simple.

I have a push button input, analog input, and 2 led outputs.

Led1 is high, led2 is high. Analog input it getting 1v. When the button is pressed I need led1 to go low, 10ms after led2 to go low. Analog input will be going from 1v to 5v and then to 2v. I need led1 and led2 to go high as soon as analog input is getting 2v. Both leds need to stay low until analog input gets 2v.

I tried using if statements and since when you press the button the analog stays at 1v for a certain amount time after the button gets depressed, the leds go high before analog input gets 2v.

Why do you want to do these things?

Please, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.
Put your sketch between the code tags `[code][/code]`

.

Would the below describe what you want to achieve?

``````state = 0

if state == 0 and voltage == 1V
if button pressed
state = 1

if state == 1
led1 off
wait 20ms
state = 2

if state == 2
led2 == off
state = 3

if state == 3 and voltage > 2V
state = 0
``````
``````const int sensorPin = A0; // potentiometer
const int buttonPin =2; //button pin
int buttonState=0;
const int calPin = 13;  // internal light on micro
int outputValue=0;
int sensorValue=0;

void setup() {
pinMode(2,INPUT_PULLUP);
pinMode(13,OUTPUT);
digitalWrite(calPin,HIGH);
}
void loop(){
Serial.begin(9600);

if ((digitalRead(2)==LOW) && (sensorValue >200 && sensorValue <210)){
digitalWrite(13, LOW);}

else if (sensorValue >400 && sensorValue <420){
digitalWrite(13, HIGH); }

delay(100);

if ((digitalRead(2)==LOW) && (sensorValue >400 && sensorValue <420)){
digitalWrite(13, LOW);}

else if (sensorValue >600 && sensorValue <620){
digitalWrite(13, HIGH); }

delay(100);

}
``````

So I am testing it out with an led on the d13 for now, just using 1 led to make it simple. Once the code is working with 1 led I can add 2nd one in there.

So if the button is low and sensor input is 1v I need to turn the led off till sensor input is at 2v. Then if the button is low and sensor is 2v I need led to go low till sensor input is at 3v.

The problem I am having is that led is blinking as soon as I pull the button pin low…

You have very narrow error bands between 200 and 220 or 400 and 420

Are you sure whatever is sending the voltage is super stable and without spikes or oscillations and that your arduino captures that in the right way?

The push button is not debounced

If you have a fast scope I would suggest you monitor closely what happens on the signals and how clean they are

I would also suggests that you wait for your event, record 500 values (1000 bytes half your memory) without printing in an array and then display the values just to see what's going on. maximum analog reading rate is about 10,000 times a second - so 500 values is not too long, but the interesting part will be to see if you have oscillations

J-M-L:
You have very narrow error bands between 200 and 220 or 400 and 420

Are you sure whatever is sending the voltage is super stable and without spikes or oscillations and that your arduino captures that in the right way?

The push button is not debounced

If you have a fast scope I would suggest you monitor closely what happens on the signals and how clean they are

I would also suggests that you wait for your event, record 500 values (1000 bytes half your memory) without printing in an array and then display the values just to see what's going on. maximum analog reading rate is about 10,000 times a second - so 500 values is not too long, but the interesting part will be to see if you have oscillations

There is nothing I can do about narrow error bands.

It isn't super stable, but arduino captures it just fine, analog values are +-5 on serial print, well within the narrow band.

I am just grounding the pin for now without debounce to get it somewhat working, then I will add debounce and 2nd led.

The issue that I am having is that the led needs to go low for 10-200ms, and the button might trigger other parts of the code, any fix for that?

Thank you.

the button might trigger other parts of the code, any fix for that?

You mean you want to use the button while the led is low or that bounces would create a challenge? I don't think you can reliably assume that a human will be able to press the button within 10-200ms of the initial press. 10ms is actually in the range of bouncing for some low quality buttons

J-M-L:
You mean you want to use the button while the led is low or that bounces would create a challenge? I don't think you can reliably assume that a human will be able to press the button within 10-200ms of the initial press. 10ms is actually in the range of bouncing for some low quality buttons

No, what I meant to say is that I don't want 1 press of the button trigger multiple led off times.

For example. it's at the position one with analog reading of 1v...............led on. The button gets pressed for for 300ms...........in that time the led was off, analog reading went to 2v, so led turned back on, but the button triggered the led to go off again. Am I making any sense? Delay at the end of it should solve that problem I would think.

OK so what you want is that the button has to return to its off position and only trigger when pressed again?

It would be much easier if you approach that with a state machine as suggested in post 3

• button pressed
• button released
• timer

all these will trigger states or variables status changes

combining states & variables you can define actions

you say

"led2 to go high as soon as analog input is getting 2v" --> what happens to LED1 when you get the 2v?
"Analog input will be going from 1v to 5v and then to 2v." --> what happens if you go to 2V within the 10ms after Led1 went off?

also - side question - "When the button is pressed I need led1 to go low, 10ms after led2 to go low." --> do you expect to be able to perceive a 10ms delay between the 2 LEDs visually?

I will try state machine next and see if I can get it to work, and no I won't be able to see the difference of 10ms between 2 leds.

for the state machine you will need to know the answers to

"led2 to go high as soon as analog input is getting 2v" --> what happens to LED1 when you get the 2v?

"Analog input will be going from 1v to 5v and then to 2v." --> what happens if you go to 2V within the 10ms after Led1 went off?

(also - I'm curious why you want them to be turned off at 10ms difference if you won't see it. if the future design not connected to LEDs and you'll need that delta between both going OFF?)

J-M-L:
for the state machine you will need to know the answers to

"led2 to go high as soon as analog input is getting 2v" --> what happens to LED1 when you get the 2v?

"Analog input will be going from 1v to 5v and then to 2v." --> what happens if you go to 2V within the 10ms after Led1 went off?

(also - I'm curious why you want them to be turned off at 10ms difference if you won't see it. if the future design not connected to LEDs and you'll need that delta between both going OFF?)

Led1 and led2 will be doing the same thing, except that led1 will need to go high 10ms before led2 or led2 needs to go high after led1 or vise versa.

Analog input will not go to 2v before led1 and led2 will go off.

Analog input will see 1/2/3/4 volts, it will start at 1v..........button pressed..........led1 off........delay10ms......led2 off...........until analog input sees 2v........then both led1 and led2 turn on at the same time.

Then it will do the same thing except it will be 2v and then as soon it sees 3v both leds will turn back on.