This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
notes:c4eng:fall2024:projects:ptb2 [2023/10/26 03:25] – created - external edit 127.0.0.1 | notes:c4eng:fall2024:projects:ptb2 [2024/10/31 00:27] (current) – [multicolor LED] dnayo | ||
---|---|---|---|
Line 2: | Line 2: | ||
=====switch===== | =====switch===== | ||
- | |||
- | The switch used has three connection pins: | ||
- | * Connect the central pin to 3.3V, using a 10 KΩ resistor in series | ||
- | * Connect one of the outer pins to a GPIO pin, using a 10 KΩ resistor in series. This pin serves as the ground | ||
=====button===== | =====button===== | ||
- | Your button(s) should produce | + | A component in the electronics kit that can be used to give an input to your pi. The button has 4 legs and should straddle the middle trench/ |
=====multicolor LED===== | =====multicolor LED===== | ||
- | | + | Inside of the electronics kit and inside one of the bags of LED's that are a part of Matt's class you will find a larger |
- | The circuit | + | |
=====if-else===== | =====if-else===== | ||
- | + | A conditional | |
- | The if else statement | + | |
- | + | Code to be executed | |
- | An easy way to write an if statement that executes when our button is pressed, | + | } |
- | < | + | |
- | if(digitalRead(BUTTON)){ | + | else{ //If the initial condition |
- | //some code here// | + | |
- | } | + | } |
- | </ | + | |
- | 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===== | ||
- | In this project, we want to stay within a range from 0 to 100, and cycle to the other end of the range when exceeded. One way to do this is to use the modulus operator to take the remainder of our expression divided by our upper range + one factor, i.e. | ||
- | < | ||
- | (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' |