User Tools

Site Tools


notes:c4eng:fall2024:projects:ptb2

Differences

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

Link to this comparison view

Next revision
Previous revision
notes:c4eng:fall2024:projects:ptb2 [2023/10/26 03:25] – created - external edit 127.0.0.1notes: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 an input for the RED, GREEN, and BLUE LEDsAll the buttons should be set to input mode since they're input devicesWith that informationI would code the button to the corresponding LED. So Red button to the red LED, blue to blue, and green to greenThe switch also should be set to input, just like the buttons are. Each button must also be connected to 3.3v pinThere are 2 on the t-cobblerI would recommend dividing your wires between the 2 and not just using one+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/valley up the middle of the breadboardBoth of the "lower" legs should be connected to a 10K ohm resistorOne of those resistors should then run to ground from each button via a wireand the other resistor should be connected to a pin to power the button (one of the 3.3v pins) via a wire. Then the "upper" left left leg of the button should be grounded. You must then set the connected gpio pins to "input" mode. When everything is connected and powered, the button should read as "0" when it is not being pressed, but then becomes a "1" when it is being pressedYou can check this by running "gpio readall" while the button is not being pressedthen again while the button is being held down. You should notice the "0" change into a "1" in the displayed table
 =====multicolor LED===== =====multicolor LED=====
- [ptb2] The multicolor LED should output the colors __RED__, __GREEN__, and __BLUE__. With that information , I would personally color code the buttons associated with the specific color desired when the button is pressed. Meaning have __RED__ button cap for when you want the multicolor LED to output __RED__, and so on for each color. This just makes it easier to test your code +Inside of the electronics kit and inside one of the bags of LED's that are part of Matt's class you will find a larger LED that instead of being one color is naturally white. This LED will also have 4 legs beneath it instead of just 2. Now that you have this LED you will need to connect it to your breadboard for power as well as general pin outputsSo, the longest leg of the LED is the leg in which you need to connect to 3.3v power, as it is the common anode version. In the common cathode version, it is the GND leg. Then the next longest leg that is alone to one side of the longest leg is the leg connected to the red output of the LED. Finally the 2 legs to the other side of the longest leg are Green and then Blue, the longer of these 2 legs is the green output leg and the shorter is the blue output leg. The leg that is connected to the 3.3v power does not need resistor but resistor is highly recommended, especially in the case of initial testing. However, all three of the output legs that will control a color need a resistor in order to function. Each of these legs need a 220 kΩ (Kiloohm) resistor.
-The circuit of the multicolor LED should have a 3.3 V running through the longest leg of the LED. Then there will be a 220k ohm resistor for each of the other legs that are wired to three different gpio pinsThe gpio pins serve as ground, so separate ground wire is not needed+
 =====if-else===== =====if-else=====
- +A conditional statement in computer programming that allows you to perform 2 different blocks of code depending on whether or not the given condition is metThe condition is a simple as  
-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. +     if (condition == 1){  //checks to see if the condition is true (true = 1 in programming) 
- +        Code to be executed   //This code will be executed if the condition is met  
-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)){ +     else{                 //If the initial condition is not met/is false 
-   //some code here// +         alternative block of code to be executed  //Code to be executed if the condition is not met 
-+         }
-</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=====
  
-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.  
-<code> 
-(10*direction)%110; // 110, since we're jumping by 10s.  
-</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/fall2024/projects/ptb2.1698290706.txt.gz · Last modified: 2023/10/26 03:25 by 127.0.0.1