This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:c4eng:fall2023:projects:ptb2 [2023/10/26 03:15] – mwinter4 | notes:c4eng:fall2023:projects:ptb2 [2023/10/26 03:25] (current) – [if-else] mwinter4 | ||
---|---|---|---|
Line 16: | Line 16: | ||
The if else statement essentially means that " __if__ this condition is true do the following thing, __else__ do this thing instead" | The if else statement essentially means that " __if__ this condition is true do the following thing, __else__ do this thing instead" | ||
+ | An easy way to write an if statement that executes when our button is pressed, is just to simply combine functions: | ||
+ | < | ||
+ | if(digitalRead(BUTTON)){ | ||
+ | // | ||
+ | } | ||
+ | </ | ||
+ | With this method, there is no need to write out a full expression. You can simply evaluate within the if statement. | ||
=====staying within a range===== | =====staying within a range===== | ||
Line 22: | Line 29: | ||
(10*direction)%110; | (10*direction)%110; | ||
</ | </ | ||
+ | |||
+ | Another way to do this would be to add an if statement each time the variable is changed, i.e. | ||
+ | < | ||
+ | value = 110; | ||
+ | if(value> | ||
+ | value = 0; | ||
+ | </ | ||
+ | Because there' | ||
+ | |||
+ | Using this method, we can also make sure we stay in the positives with an additional if statement: | ||
+ | < | ||
+ | if(value> | ||
+ | value = 0; | ||
+ | if(value< | ||
+ | value = 100; | ||
+ | </ | ||
+ | Now when we go below 0 or above 100, we will cycle to the other side of the range. It is important to add these if statements directly after the value is changed, to ensure the out-of-bounds value doesn' |