User Tools

Site Tools


notes:c4eng:fall2023:projects:ptb2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
notes:c4eng:fall2023:projects:ptb2 [2023/10/26 03:15] mwinter4notes: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". If the condition inside the parentheses evaluates to true , the code inside the if block will execute. The if else statement essentially means that " __if__ this condition is true do the following thing, __else__ do this thing instead". If the condition inside the parentheses evaluates to true , the code inside the if block will execute.
  
 +An easy way to write an if statement that executes when our button is pressed, is just to simply combine functions: 
 +<code>
 +if(digitalRead(BUTTON)){
 +   //some code here//
 +}
 +</code>
 +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; // 110, since we're jumping by 10s.  (10*direction)%110; // 110, since we're jumping by 10s. 
 </code> </code>
 +
 +Another way to do this would be to add an if statement each time the variable is changed, i.e. 
 +<code>
 +value = 110; 
 +if(value>100)
 +   value = 0;
 +</code>
 +Because there's only one statement being executed after the if statement, there is no need to add curly brackets. 
 +
 +Using this method, we can also make sure we stay in the positives with an additional if statement: 
 +<code>
 +if(value>100)
 +   value = 0; 
 +if(value<0)
 +   value = 100; 
 +</code> 
 +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't get used in any code executions.
notes/c4eng/fall2023/projects/ptb2.1698290155.txt.gz · Last modified: 2023/10/26 03:15 by mwinter4