diff --git a/docs/how-to-docu/Blank File.md b/docs/how-to-docu/Blank File.md index a0368a2..d8ac6e2 100644 --- a/docs/how-to-docu/Blank File.md +++ b/docs/how-to-docu/Blank File.md @@ -5,4 +5,7 @@ sidebar_position: 100 # Blank Docusaurus File ## Why? -This is a blank docusaurus file that can be copied in Visual Studio to serve as a basis for new webpages. \ No newline at end of file +This is a blank docusaurus file that can be copied in Visual Studio to serve as a basis for new webpages. + +## Sources +[Example Source](https://docusaurus.io/) diff --git a/docs/how-to-docu/first-blog-post.md b/docs/how-to-docu/first-blog-post.md deleted file mode 100644 index ea472bb..0000000 --- a/docs/how-to-docu/first-blog-post.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -sidebar_position: 3 ---- - -# Create a Blog Post - -Docusaurus creates a **page for each blog post**, but also a **blog index page**, a **tag system**, an **RSS** feed... - -## Create your first Post - -Create a file at `blog/2021-02-28-greetings.md`: - -```md title="blog/2021-02-28-greetings.md" ---- -slug: greetings -title: Greetings! -authors: - - name: Joel Marcey - title: Co-creator of Docusaurus 1 - url: https://github.com/JoelMarcey - image_url: https://github.com/JoelMarcey.png - - name: Sébastien Lorber - title: Docusaurus maintainer - url: https://sebastienlorber.com - image_url: https://github.com/slorber.png -tags: [greetings] ---- - -Congratulations, you have made your first post! - -Feel free to play around and edit this post as much you like. -``` - -A new blog post is now available at [http://localhost:3000/blog/greetings](http://localhost:3000/blog/greetings). diff --git a/docs/how-to-docu/intro.md b/docs/how-to-docu/intro.md index 514d380..c12ab75 100644 --- a/docs/how-to-docu/intro.md +++ b/docs/how-to-docu/intro.md @@ -1,7 +1,7 @@ --- sidebar_position: 0 --- -# Docusaurous Intro +# Docusaurus Intro Let's discover **Docusaurus in less than 5 minutes**. diff --git a/docs/how-to/how-to-bugfix.md b/docs/how-to/how-to-bugfix.md new file mode 100644 index 0000000..5db7b09 --- /dev/null +++ b/docs/how-to/how-to-bugfix.md @@ -0,0 +1,48 @@ +# Programming Subteam Error Log + +Welcome to the NoMythic Programming Error Log. This is a document created by Rowan Flood on Dec 5, 2024. the purpose of this document is to keep track of the tricky errors our subteam comes across. Every time we encounter an error message that takes a significant amount of finagling to get rid of, or that didn’t have in intuitive solution, write about it here! Copy the error message after you realise it’s gonna be a tough problem, and write the solution once you find it! + +### TEMPLATE (copy then use): +#### Date, Year. Name: +Problem: make sure to include the exact error message copied from terminal or RioLog +Solution: specifically what you changed in order to solve the problem +Method (Optional): How you found the solution (for example: searching “this” on ChieffDelphi, asking ChatGPT, guessing) +2024/2025 season + +#### December, 2024. Rowan Flood: +Problem: I am using the WpiLib 2024.1.1-beta-1 version of wpilibsuite, and when I built the code, I got this error code from AdvantageKit: +“The version of AdvantageKit installed in this project requires WPILib 2025.1.1-beta-1, but WPILib 2025.1.1-beta-2 is currently installed. Please update AdvantageKit and/or WPILib to compatible versions (the supported version of WPILib is listed in the release notes for each AdvantageKit version). DO NOT override this check; running with invalid versions will result in a broken project with issues that are difficult to diagnose.” + +Solution: changing the 3rd line of build.gradle to say “2025.1.1-beta-1” instead of “2025.1.1-beta-2” as the version number for wpi.first.GradleRIO + +Method (optional): I explained the situation to chatGPT, and fixed the error with one of the solutions suggested. + +#### December, 2024. Rowan Flood, Xiaohan Liu: +Problem: when we built the code, we got this error from our launch.json file: +Unable to format JSON +Java.Lang.AssertionError: Unable to format JSON +Followed by a looong stack trace of could we didn’t make. + +Solution: deleted commas after “true” and “false” in our launch.json file + +Method (Optional): looked at template code that built just fine, and tried to find differences between the launch.JSON files. The only difference we found was two commas that weren’t in the template code + +#### December, 2024. Rowan Flood: +Problem: code would crash immediately after starting if a USB drive was not plugged in for recording logs to. After plugging in a drive, we had to power cycle the robot before code would work. Here is the error message: + +Solution: added code in Robot.java that checks if a USB drive is visible by the RIO, and - if not - prints an error message and saves logs to an internal file instead of attempting to save to the drive. Here is the code: + +Method (Optional): pasted a picture of the error code into ChatGPT, than asked how to carry out the solution + +#### 2025 February, Evan and Griffin +Problem: Deploy fails with the following error: +Execution failed for task ':deployfrcStaticFileDeployroborio'. +//> A failure occurred while executing edu.wpi.first.deployutils.deploy.artifact.ArtifactDeployWorker +// > SFTP error (SSH_FX_FAILURE): Failure + +Cause: RoboRio internal storage was filled with log files, and there was no space to upload new pathplanner paths + +Solution: Use filezilla or ssh to log into the roborio and delete log files +//> ssh lvuser@10.24.91.2 # Log into the roborio +//> du -sh logs # Should print a fairly large size, indicating a large number of logs on the roborio +//> rm -r logs # Delete all logs from the roborio diff --git a/docs/robot-docs/Replay Simulation/Integrating_Inputs_Class_into_Subsystem.md b/docs/robot-docs/Replay Simulation/Integrating_Inputs_Class_into_Subsystem.md index a02e3d9..b2ff356 100644 --- a/docs/robot-docs/Replay Simulation/Integrating_Inputs_Class_into_Subsystem.md +++ b/docs/robot-docs/Replay Simulation/Integrating_Inputs_Class_into_Subsystem.md @@ -14,7 +14,8 @@ DrivetrainInputsAutoLogged inputs; public DrivetrainSubystem() { inputs = new DrivetrainInputsAutoLogged(); - //define other Objects for the subsystem + //define other Objects for the +} ``` diff --git a/docs/robot-docs/Replay Simulation/Sim Intro.md b/docs/robot-docs/Replay Simulation/Sim Intro.md index 5f5d859..27a9906 100644 --- a/docs/robot-docs/Replay Simulation/Sim Intro.md +++ b/docs/robot-docs/Replay Simulation/Sim Intro.md @@ -3,9 +3,11 @@ sidebar_position: 1 --- # Intro To Simulation -In FRC, Simulation is testing code without a physical robot. This allows you to quickly debug and develop code before the robot is made and while it is competing. Simulation comes in three main forms in order of difficulty: Running code on a laptop, simulating hardware, and simulating environment(not normally used in FRC). +In FRC, Simulation is testing code without a physical robot. This allows you to quickly debug and develop code before the robot is made and while it is competing. Simulation comes in three main forms in order of difficulty: Running code on a laptop, simulating hardware, and simulating environment (not normally used in FRC). ## Sources -[NoMythic_Simulation_Google_Doc](https://docs.google.com/document/d/13F6GO_e3fENr6bYeQux29UXtqi7mdO8z0uJELuAZY3Y/edit?pli=1&tab=t.ejcfcjau18wn) -[Mechanical_Advantage(6328)_Presentation](https://www.youtube.com/watch?v=8FfwFQcvRmU) -[WPILib_imulation](https://docs.wpilib.org/en/stable/docs/software/wpilib-tools/robot-simulation/introduction.html) \ No newline at end of file +[NoMythic Simulation Google Doc](https://docs.google.com/document/d/13F6GO_e3fENr6bYeQux29UXtqi7mdO8z0uJELuAZY3Y/edit?pli=1&tab=t.ejcfcjau18wn) + +[Mechanical Advantage (6328) Presentation](https://www.youtube.com/watch?v=8FfwFQcvRmU) + +[WPILib Simulation](https://docs.wpilib.org/en/stable/docs/software/wpilib-tools/robot-simulation/introduction.html) \ No newline at end of file diff --git a/docs/tutorials/circuit-python-tutorial.md b/docs/tutorials/circuit-python-tutorial.md deleted file mode 100644 index 74b34e2..0000000 --- a/docs/tutorials/circuit-python-tutorial.md +++ /dev/null @@ -1,3 +0,0 @@ -# Circuit Python - -This tutorial will be moved over from [Github](https://github.com/2491-NoMythic/circuitPython/wiki) diff --git a/docs/tutorials/circuit-python-tutorial/_category.json b/docs/tutorials/circuit-python-tutorial/_category.json new file mode 100644 index 0000000..fc7025e --- /dev/null +++ b/docs/tutorials/circuit-python-tutorial/_category.json @@ -0,0 +1,8 @@ +{ + "label": "Circuit Playground Tutorial", + "position": 3, + "link": { + "type": "generated-index", + "description": "How to use Circuit Playgrounds with Python... Not very useful because we use Java with the Circuit Playgrounds now. " + } +} diff --git a/docs/tutorials/circuit-python-tutorial/_img/circuit_playground.png b/docs/tutorials/circuit-python-tutorial/_img/circuit_playground.png new file mode 100644 index 0000000..be45596 Binary files /dev/null and b/docs/tutorials/circuit-python-tutorial/_img/circuit_playground.png differ diff --git a/docs/tutorials/circuit-python-tutorial/_img/playground_lights.jpg b/docs/tutorials/circuit-python-tutorial/_img/playground_lights.jpg new file mode 100644 index 0000000..4be3c79 Binary files /dev/null and b/docs/tutorials/circuit-python-tutorial/_img/playground_lights.jpg differ diff --git a/docs/tutorials/circuit-python-tutorial/circuit-python-advanced.md b/docs/tutorials/circuit-python-tutorial/circuit-python-advanced.md new file mode 100644 index 0000000..8d5cb63 --- /dev/null +++ b/docs/tutorials/circuit-python-tutorial/circuit-python-advanced.md @@ -0,0 +1,42 @@ +--- +sidebar_position: 5 +--- +# Circuit Python: Advanced +This is a page for those that feel comfortable with the code that was on the other pages. If you don't feel comfortable with that stuff yet, I would suggest you head back and experiment. Think of something you could do with the neopixels and play with the code. + +## Next steps +Ok, there are no solutions on this page. You now know the basic building blocks, and it is time to write more code based on a suggestion. So here is a list of ideas. You don't need to do them in order either. Feels free to use Google, or the Adfruit learning guides, or the circuit python documentation. If you need help, look back at other examples we already did. Ask a team mate. If you are really stuck, ask a mentor. + +* Play 2 different tones when pressing the two different buttons. +* * Did you play a tone for a specific time? Or turn it off when you let go of the button? +* * Did you create a function that played the tone? Did you need to? +* Play a different tone for every one of the touch pads. +* Store several notes in an list, and use a loop to play all of them. +* * If you need some help with a list of playing a song, you can try [here.](https://learn.adafruit.com/basic-datastructures-in-circuitpython/overview) However, the examples of playing notes are not the same on the Circuit Playground Express. You will also have to look up how to play a tone. +* Listen for a noise, and play a tone or turn on a pixel if the noise is too loud. +* Measure the temperature. Hold your finger on the temp sensor to see the changes. +* * Print the current temperature to the console. +* * Show the temperature by changing the colors of the pixels. +* * * You can show set temperature ranges, or a continuous change of colors. +* * Create a round thermometer showing cold to hot by number of pixels shown. +* Measure the light level. Cover the sensor with your hand to see the changes. +* Print the current level to the console. +* * Play a tone based on the light level +* * * Show less pixels on if the light is low, more if it is bright +* Use the accelerometer. +* * Change the tone played depending on the angle of the board left to right. +* * Change the pixel displayed based on the angle of the board in both left to right and forward and back. +* Use the neopixel library to control an external set of neopixels +* * Set the color for the whole string +* * Change the color for each pixel +* * Create a 'moving' animation +* Control a servo connected to the device +* * Have it turn forward with button_a, and backward with button_b +* * Have it move to different positions based on the touch pad pressed +* Create the game of Simon. +* * The board will show random lights, the user will then need to touch the right touch pads to continue +* * Can you make the game get harder by increasing the speed or number of lights shown? + + +## Sources +[GitHub Origin](https://github.com/2491-NoMythic/circuitPython/wiki/5.Advanced) \ No newline at end of file diff --git a/docs/tutorials/circuit-python-tutorial/circuit-python-hardware.md b/docs/tutorials/circuit-python-tutorial/circuit-python-hardware.md new file mode 100644 index 0000000..b3dfefe --- /dev/null +++ b/docs/tutorials/circuit-python-tutorial/circuit-python-hardware.md @@ -0,0 +1,25 @@ +--- +sidebar_position: 2 +--- + +# Circuit Python: Hardware +Our devices is the [Circuit Playground Express.](https://www.adafruit.com/category/965) + +![playground_lights](./_img/playground_lights.jpg) +*A Circuit Playground with Activated Lights* + +This is a really cool little device. It has a lot built in. There are neopixels (RGB lights) arranged in a ring. Around the outside of that are touch sensors. There is a buzzer, a microphone, 2 physical buttons, a switch, and an accelerometer (detects motion). There is a lot to play with here, but programming it is not an insurmountable challenge. Adafruit has made programming it very accessible, hence our use as an intro tool. + +These boards can be programmed in many different ways. We will be using Circuit Python, but you can use Arduino, and even MakeCode. See the below links for more on those things. + +To make these different environments work, there is a boot loader. If the board doesn't seem to work, try updating that. There is a process for this that is defined [here.](https://learn.adafruit.com/adafruit-circuit-playground-express/updating-the-bootloader) + +There is lots to read up on if you want, but head right to Getting Started if you want to skip the details. + +To dive deeper, check out [this page](https://learn.adafruit.com/adafruit-circuit-playground-express) +If you are curious about the ecosystem, see this [hardware overview page](https://www.adafruit.com/category/965). + + + +## Sources +[GitHub Origin](https://github.com/2491-NoMythic/circuitPython/wiki/2.Hardware) \ No newline at end of file diff --git a/docs/tutorials/circuit-python-tutorial/circuit-python-intro.md b/docs/tutorials/circuit-python-tutorial/circuit-python-intro.md new file mode 100644 index 0000000..43db740 --- /dev/null +++ b/docs/tutorials/circuit-python-tutorial/circuit-python-intro.md @@ -0,0 +1,17 @@ +# Circuit Python: Intro +Welcome to the CircuitPython wiki! + +We will be using CircuitPython, and the Circuit Playground Express from Adafruit to learn the basics of programming. + +Start by getting more information on the hardware device, on the hardware page. + +Then start to learn about programming on the Circuit Playground, see the programming page. + +After getting things set up, we will learn programming step-by-step + +![circuit_playground](./_img/circuit_playground.png) +*A Circuit Playground* + + +## Sources +[GitHub Origin](https://github.com/2491-NoMythic/circuitPython/wiki/1.Home) \ No newline at end of file diff --git a/docs/tutorials/circuit-python-tutorial/circuit-python-programming.md b/docs/tutorials/circuit-python-tutorial/circuit-python-programming.md new file mode 100644 index 0000000..3f8e97f --- /dev/null +++ b/docs/tutorials/circuit-python-tutorial/circuit-python-programming.md @@ -0,0 +1,58 @@ +--- +sidebar_position: 3 +--- + +# Circuit Python: Getting Started +We will be programming in Circuit Python. These devices can be programmed in MakeCode with blocks or Javascript, or using the Arduino IDE even. Here, we will use Circuit Python, which is a subset of Python. A lot of what you can do in Python you can also do in Circuit Python, but a few things needed to change in order to fit onto the tiny bit of memory these devices have. + +## Getting Started +There are a couple of conventions we need to follow to make everything work. First, CircuitPython needs to be installed. If you are getting a board from the Robotics Team, then we have done this for you. Otherwise look [here.](https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-quickstart) + +After the install when you plug in the device, you should get a removable device that shows up called CIRCUITPY. This works just like a removable drive does. The really neat thing is that you can drag and drop files to the device. In fact, that is how you get your code to it! + +The Adafruit docs will all refer to the the Mu editor. Any program that can edit and save text files can be used. The way to make sure your program is read by the device is to use the code.py file name. If you save a file to the device called `code.py`, it will reboot and start to run that python program. It is often best to just open the existing `code.py` file on the device so a save will automatically save back to the device. + +## Libraries +What can we do? Can we turn on a neopixel in our first program? Yes and no. Our first program will be to turn on a neopixel, but first we need to get a library to access the hardware. We installed Circuit Python, so we have the basic language installed. We can do some math, we can do loops and make decisions, but the basic Circuit Python install doesn't know how to access the hardware. We need a library for that. A library is some code that someone else has written that knows how to interact with something else, often some hardware. Just like how physical books have information on specific topics, or how Neo downloaded how to do Kung-Fu in the matrix, we will get a library to help us. We will use CPX. More on that library and the things it can do [here.](https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express) + +When you plugged in your Circuit Playground Express and opened the CIRCUITPY drive you might have noticed a couple of things already there. There was probably a `code.py` file and an empty `lib` folder. This `lib` folder is where we will put any libraries that we need to use. We need to get them first though. You can download libraries at [circuitpython.org](https://circuitpython.org/libraries) The Circuit Python version and the version of the library needs to be the same version. + +All of the libraries available will not fit onto our device. There isn't enough room. You will need to copy files into the lib folder if you want to use them. Fortunately, there is one special library already installed. We just need to use it. With it we can access the basic parts of the hardware. +```python +from adafruit_circuitplayground import cp +``` +Now we can use the "cp" library in our program. + +## First Program +Let's start with the Hello World of hardware devices: a blinky LED. +```python +from adafruit_circuitplayground import cp + +while True: + cp.red_led = True +``` +Let's look at each line of our first program. + We have already looked at the import. Any library we want to use will have a similar statement. We called this `import cp`. + The `while True:` is part of standard python. This sets up a never ending loop. "While the condition is still true, keep repeating the code after the colon." `True` and `False` are called booleans, and can represent on/off or positive/negative or yes/no. + The next line has spaces, or a tab. This is important in python. This is how blocks of code are defined. In our case, this is the block that will keep repeating forever. We just have one line here. It uses the `cp` library to access the red led on the board. Setting it to `True` will turn it on. Setting to `False` would turn it off. + +Now save that code to the `code.py` file on the device. + +That's it. You did it. You made a program run on a piece of hardware. + +## Next Steps +Now, we need to learn about what other hardware we can use, and we need to learn a bit more Python along the way. + +In the next part of this tutorial, I explain how to build a simple program step by step. I suggest you start there and work your way through. + +But if you just want to see some basic code using the hardware, then check out the examples [here.](https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/circuit-playground-express-library) + +You could jump into learning more about the [hardware.](https://learn.adafruit.com/circuitpython-essentials/circuitpython-essentials) + +Just want the [documentation](https://docs.circuitpython.org/en/latest/README.html) for Circuit Python? + +Want to dig into ready to run example code? Look at the library folder you downloaded. Inside each library is example code that you can copy over to your Circuit Playground device. + + +## Sources +[GitHub Origin](https://github.com/2491-NoMythic/circuitPython/wiki/3.Programming-(Getting-Started)) \ No newline at end of file diff --git a/docs/tutorials/circuit-python-tutorial/circuit-python-steps.md b/docs/tutorials/circuit-python-tutorial/circuit-python-steps.md new file mode 100644 index 0000000..3751aee --- /dev/null +++ b/docs/tutorials/circuit-python-tutorial/circuit-python-steps.md @@ -0,0 +1,274 @@ +--- +sidebar_position: 4 +--- + +# Circuit Python: Simple Program Steps +Here we will start with a very basic program and have you build on it step by step. We will mostly use the neopixels here to teach basic programming in Python. + +## Simple Program +If you were at one of our experience the team nights you might remember something similar to this: +```python +from adafruit_circuitplayground import cp + +while True: + if cp.button_a: + cp.pixels[0] = (100, 0, 0) +``` +If you don't understand the import or while, head back to Getting Started. Instead of just turning on the red LED, we check if button_a is pressed, and then turn on a neopixel. if is how we make decisions in Python. If our condition is `True`, then do something after the ":". Our condition is whether the button_a is pressed, so if button_a is pressed, our condition is `True`, and the block therefore is executed. + +Remember, the code block is what is indented, and we have another set of spaces, or another tab for the neopixel line. This time we can't set the neopixel to `True` to turn it on, we need to tell it the combination of red, green and blue needed to make the neopixel, and we have to tell it which pixel we are setting. Do tou see the [0]?. In programming we often start with 0 instead of 1 to mean the first item. Just like in math we assign the right hand side to the lefthand side. So we are using the `cp` library to set pixels[0] to a color of (100, 0, 0) which is a red. Try it. + +When you run this code and press button_a, the neopixel goes red- but it never turns off. Do you know why? + +## Else +If you guessed that we never turned it off, you are correct. How would we turn it off if the button isn't pressed? We write an `else`. Basically it means that if the `if` it is after is `False`, it is `True`. +```python + +from adafruit_circuitplayground import cp + +while True: + if cp.button_a: + cp.pixels[0] = (100, 0, 0) + else: + cp.pixels[0] = (0,0,0) +``` +Notice that there is no "off". We just set the pixel to not show any red or green or blue. Now when you run this, the pixel only lights up while the button is pressed. + +## Double your fun +How about getting two pixels to light up? How about pixels 0 and 1? +```python +from adafruit_circuitplayground import cp + +while True: + if cp.button_a: + cp.pixels[0] = (100, 0, 0) + cp.pixels[1] = (0, 100, 0) +``` +Now we have 2 pixels on. But we are back to not turning off again. Try putting in the else to turn off both pixels. + +How about all the pixels. Can you write a program to get them all to turn on? + +Did you remember to turn them all off? What about colors. Any different colors? Experiment. + +There are two buttons. cp.button_a, and cp.button_b. Try these ideas next. + +* Turn on left side lights with button a. Turn on right side with button b. +* Make all turn on red with button a. Make all turn on green with button b. + +## Lots of Repeated Lines +If you were turning all the light on and off, you may have had code that looked very much like this: +```python +from adafruit_circuitplayground import cp + +while True: + if cp.button_a: + cp.pixels[0] = (100, 0, 0) + cp.pixels[1] = (100, 0, 0) + cp.pixels[2] = (100, 0, 0) + cp.pixels[3] = (100, 0, 0) + cp.pixels[4] = (100, 0, 0) + cp.pixels[5] = (100, 0, 0) + cp.pixels[6] = (100, 0, 0) + cp.pixels[7] = (100, 0, 0) + cp.pixels[8] = (100, 0, 0) + cp.pixels[9] = (100, 0, 0) + else: + cp.pixels[0] = (0,0,0) + cp.pixels[1] = (0,0,0) + cp.pixels[2] = (0,0,0) + cp.pixels[3] = (0,0,0) + cp.pixels[4] = (0,0,0) + cp.pixels[5] = (0,0,0) + cp.pixels[6] = (0,0,0) + cp.pixels[7] = (0,0,0) + cp.pixels[8] = (0,0,0) + cp.pixels[9] = (0,0,0) +``` +Whew. That was a lot of typing. I hope you at least cut and pasted the lines. But you can always make mistakes that way, or maybe forget to increase the pixel id in between the [ ]. + +We want to reduce the amount of code we write. We are doing the same thing over and over with one very small change. Loops are a way we can accomplish this. But first, a quick into to variables. + +## Variables +If you have done any algebra, you know about variables. Look at this math equation and substitution of x: +```python +x = 1 +y = 2(x) + 3 +y = 2(1) + 3 +y = 5 +``` +We used variable x to solve for y. Programming variables work very similarly, but they can be boolean, strings, or numbers (or other things we will get to). Check out this version of turning on one neopixel. Note the very simple change. +```python +from adafruit_circuitplayground import cp + +while True: + index = 0 + if cp.button_a: + cp.pixels[index] = (100, 0, 0) + else: + cp.pixels[index] = (0,0,0) +``` +We didn't say to turn on pixel 0, we said to turn on pixel index. We had set it to zero, so this does the same thing. Can you change this to set neopixel 4 on and off instead of 0? + +If we wanted to update the index we could. +```python +from adafruit_circuitplayground import cp + +while True: + index = 0 + if cp.button_a: + cp.pixels[index] = (100, 0, 0) + index = 1 + cp.pixels[index] = (100, 0, 0) + else: + cp.pixels[index] = (0,0,0) + index = 1 + cp.pixels[index] = (0,0,0) +``` +What did that do? Was that the same as our program that used 0 and 1? Lets change that just slightly to do a bit of math instead. + +from adafruit_circuitplayground import cp +```python +while True: + index = 0 + if cp.button_a: + cp.pixels[index] = (100, 0, 0) + index = index + 1 + cp.pixels[index] = (100, 0, 0) + else: + cp.pixels[index] = (0,0,0) + index = index + 1 + cp.pixels[index] = (0,0,0) +``` +Did that do anything different? It shouldn't. It should work just the same, but we took the 0 and added 1 to it instead. So we still got pixels 0 and 1. Now, how would you get pixels 2 and 4 to light up instead? + +Change the 0 to 2, and instead of + 1, do + 2: + +from adafruit_circuitplayground import cp +```python +while True: + index = 2 + if cp.button_a: + cp.pixels[index] = (100, 0, 0) + index = index + 2 + cp.pixels[index] = (100, 0, 0) + else: + cp.pixels[index] = (0,0,0) + index = index + 2 + cp.pixels[index] = (0,0,0) +``` +Could you keep doing this to light up or turn off all the pixels? Sure, you could, but we have a better way. + +## Loop-de-Loop +Now that we can see how variables work, and that they can change values, we talk about loops. We are going to talk about a `for` loop. Basically we are going to assign a variable several values from a range of values, and execute our formula, or code block for each variable. + +`for x in range(0, 3):` would tell us that our variable x will start out as 0, and run 3 times (0, 1, 2). It doesn't mean 0 to 3. If the statement was `for x in range(4, 3):` then x would start at 4, and still run 3 times (4, 5, 6). Let's turn 5 pixels on and off with a `for` loop. +```python +from adafruit_circuitplayground import cp + +while True: + if cp.button_a: + for index in range(0, 5): + cp.pixels[index] = (100, 0, 0) + else: + for index in range(0, 5): + cp.pixels[index] = (0,0,0) +``` +Woah, that was a lot less code. + +Now try a couple more things like before, but use loops: + +* change to set pixels 5 to 9 instead of 0 to 4. +* do left side with button_a, and right side with button_b + +## Functions + +When we first started talking about variables, we talked about a math function to calculate y, which was y = 2x + 3.We make functions in programming like that too, as repeatable sections of code. They don't need to solve math problems, but they can. Let's write that math problem as a python function: +```python +def calcY(x): + y = x * 2 + 3 + return y +``` +We can now call this function in python code, and it. We will use a function that comes with python called `print` that will print to the console. Check with a mentor if you need help to display your console. +```python +def calcY(x): + y = x * 2 + 3 + return y + +x = 1 +y = calc(x) +print(y) + +print(calcY(3)) +``` +That code defined our function, then showed two ways of printing to the console the output of the function. Do you get what you thought? + +Ok, you are bored. I get that. Let's get back to pixels. Functions don't need to take parameters, and they don't need to return a value. You could write a function like this too: +```python +def showPixelOne(): + cp.pixels[1] = (100, 0, 0) + return +``` +Now when ever we want to set pixel 1 to red, we can call this. But... let's step it up a notch. The last pixel program we wrote used 2 different loops to turn the pixels on or off. Can you re-write that program (we all that refactoring) to use 2 different functions? One that turns on all the pixels, and one that turns off all the pixels? + +This is one way it might look: +```python +from adafruit_circuitplayground import cp + +def turnPixelsOn(): + for index in range(0, 10): + cp.pixels[index] = (100, 0, 0) + return + +def turnPixelsOff(): + for index in range(0, 10): + cp.pixels[index] = (0, 0, 0) + return + +while True: + if cp.button_a: + turnPixelsOn() + else: + turnPixelsOff() +``` +Let's look at this a bit. Is this code any better? There are actually more lines of code, so it might seem to be not a good idea to do this, but there are benefits. If you were to ignore the functions and jump to the while true: part of the code, could you understand what it did? You could because the function names were created to explain what they did. If they were just called doStuff(), or pixels() or on(), the names don't tell you enough. In a longer program that is important. + +## Flexible functions + +Take a look at the functions we wrote. We wrote 2 of them. It was a good start, but could we only write one? What would we need to pass as parameters? Would the function get too complicated? Can you thing of a few different ways it could be done to turn pixels on and off? + +I have a few ideas: + +* Pass a boolean to the function to mean on and off +* Pass a number for the first "red" color. You could then pick any red, or 0 for off. +* Pass the entire color! Mind blown. Remember I said a variable could be more than just a number? Lets try that! + +from adafruit_circuitplayground import cp +```python +def turnAllPixelsToColor(color): + for index in range(0, 10): + cp.pixels[index] = color + return + +red = (100, 0, 0) +off = (0, 0, 0) + +while True: + if cp.button_a: + turnAllPixelsToColor(red) + else: + turnAllPixelsToColor(off) +``` +Was your solution similar? Does the function need to be more flexible? Could we pass in the start and number of pixels to use in the loop? Of course you can. Should you? Ask your self if you need to now. If the goal was to turn all the pixels off and on, then don't make the function more complicated than it needs to be. Don't be afraid to refactor when what the code needs to do changes. + +Now try to write the code and functions so that it uses two buttons. + +* Turn all of the pixels on when button_a is pressed, but only off when button_b is pressed +* Set the left to red when button_a is pressed, and right to green when button_b is pressed + +How did you make the changes? How many functions did you use? Did you need to refactor the functions at all? + +## Tada! +Guess what? You are now a programmer. Most of what we do as programmers we have just discussed. We created functions, we call them, we do a bit of math, we do loops, we call functions in other libraries. Is there more to learn? Of course. Check out the Advanced page. + +## Sources +[GitHub Origin](https://github.com/2491-NoMythic/circuitPython/wiki/4.Step-By-Step) \ No newline at end of file diff --git a/src/css/custom.css b/src/css/custom.css index 427a2b0..3e01550 100644 --- a/src/css/custom.css +++ b/src/css/custom.css @@ -8,7 +8,7 @@ /* :root { --ifm-color-primary: #2e8555; --ifm-color-primary-dark: #29784c; - --ifm-color-primary-darker: #277148; + --ifm-color-prima ry-darker: #277148; --ifm-color-primary-darkest: #205d3b; --ifm-color-primary-light: #33925d; --ifm-color-primary-lighter: #359962; @@ -26,9 +26,15 @@ --ifm-color-primary-lighter: #7f3599; --ifm-color-primary-lightest: #8f3cad; --ifm-code-font-size: 95%; + background-color: rgb(180, 154, 190); --docusaurus-highlighted-code-line-bg: rgba(0, 0, 0, 0.1); + color:#3f1848; + --ifm-menu-color: #3f1848; +} +.menu li:hover{ + --ifm-menu-color: #8f3cad; + --ifm-menu-color-active: #8f3cad; } - /* For readability concerns, you should choose a lighter palette in dark mode. */ /* [data-theme='dark'] { --ifm-color-primary: #25c2a0; @@ -40,7 +46,6 @@ --ifm-color-primary-lightest: #4fddbf; --docusaurus-highlighted-code-line-bg: rgba(0, 0, 0, 0.3); } */ - [data-theme='dark'] { --ifm-color-primary: #c379d7; --ifm-color-primary-dark: #b75fcf; @@ -48,7 +53,7 @@ --ifm-color-primary-darkest: #9a36b5; --ifm-color-primary-light: #cf93df; --ifm-color-primary-lighter: #d4a0e3; - --ifm-color-primary-lightest: #e6c7ee; + --ifm-color-primary-lightest: #3f1848; --ifm-color-primary-lightest: #4fddbf; --docusaurus-highlighted-code-line-bg: rgba(0, 0, 0, 0.3); -} +} \ No newline at end of file