Hi i'm new to the world of Arduino and my first project was to build a useless box. It works perfectly fine but when looking at the code it should get stuck somewhere.
So the state of the switch T1 gets saved in v1. But then the program should stuck in the "if loop" even if the switch is not amymore pressed as the variable v1 will not change anymore.
Hope somebody can help and sorry if the code is written badly ;).
spycatcher2k:
There is no 'if loop' there is an if else, then it goes back to the top of loop().
But why wouldn't it do nothing after it ran through the if else part because it then changes the value of v1 and this stays the same so it shouldn't enter the if else again?
UKHeliBob:
As a matter of interest, why have you got two variables, pos and pos2, that always have the same value ?
Why two servos in a useless box ? Can you please provide a link to the design ?
There is one motor to open the box and one to move the handle. I could have just used one pos-variable but as I said the code might not be perfect as it is the first code I wrote on my own.
Vrisuoel:
There is one motor to open the box and one to move the handle. I could have just used one pos-variable but as I said the code might not be perfect as it is the first code I wrote on my own.
quilkin:
but your loop() always calls digitalRead() and puts a new value into v1, so it's constantly updating and the 'if' clause will act accordingly.
void loop() {
v1=digitalRead(T1);
if(!v1)
....
....
Yes, but in the beginning the switch is given a high state, which is first saved in v1. If I push the switch the state gets low and therefore the program enters the "if" part. But if the switch gets read again it should save a low state for v1. And as the switch doesn't get pressed anymore, the condition of !v1 will never be met again.
So after pushing the switch all that should happen is that the two pos variables increase by 1 but then it should stop and do nothing again untill the switch is pushed again.
digitalRead() does NOT return a boolean. While treating the value as a boolean is legal, it is NOT a best practice. Explicitly comparing the value to HIGH or LOW makes it clear that you KNOW what value pressed returns.
digitalRead() does NOT return a boolean. While treating the value as a boolean is legal, it is NOT a best practice. Explicitly comparing the value to HIGH or LOW makes it clear that you KNOW what value pressed returns.
It is just a normal toggle switch with one side connected to gnd and one side connected to a pin on the arduino.
Well of course I know that the value will switch from 1, to 0 after pressing the switch (assuming I could treat it as a boolean). But after pressing the switch v1 will get 0 and it will never get not 0 (!0), as requested in the if clause. Therefore it should skip it.
When not pushed, code will execute else part of if..else. Decreasing S1 and S2 one step at a time until zero. Execution will be stucked in while loop until S1 and S2 is zero. Without it, you could activate the servo again before it return to 0 degrees.
When button is pushed, (!v1) will be true and S1 and S2 will increase one step at a time and set the servo at that angle. I don't know what will happen if you write a higher value than 180 to the servo. Hold it long enough and pos and pos2 will overflow (>32767) and become nagative numbers.
Vrisuoel:
Yes, but in the beginning the switch is given a high state, which is first saved in v1. If I push the switch the state gets low and therefore the program enters the "if" part. But if the switch gets read again it should save a low state for v1. And as the switch doesn't get pressed anymore, the condition of !v1 will never be met again.
...
I hope you can follow my thoughts.
When condition of !v1 is not met, else part of if..else will be executed.
OP: I think you need to look at the state change detection example. You want to do something, apparently, when the switch BECOMES pressed, not IS pressed, and something else when the switch BECOMES released, not IS released.