diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-0-raspberry-pi.mdx b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-0-raspberry-pi.mdx new file mode 100644 index 00000000..53295aba --- /dev/null +++ b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-0-raspberry-pi.mdx @@ -0,0 +1,27 @@ +--- +title: Control Flow in Raspberry Pi +--- + +# Control Flow in Raspberry pi + +Welcome to the Control Flow chapter! In this section, you’ll discover how to make your Raspberry Pi projects interactive by learning how to control the flow of your programs. Control flow is all about making decisions and reacting to inputs—turning your simple sensor readings into dynamic, real-world actions. + +**What’s Fun About This Chapter?** + +- **Interactive Projects:** Watch your code come alive as it reads sensors and controls outputs. +- **Real-World Impact:** Use sensors like buttons, potentiometers, and more to build smart devices. +- **Step-by-Step Learning:** Each guide builds on the previous one, helping you master both programming concepts and hardware interactions. + +To dive deeper, explore these guides: + +- **[Raspberry Pi GPIO Functions](/book/part-1-instructions/3-control-flow/0-panorama/10-1-gpio-functions/)** + Learn the basics of initializing, reading, writing, and cleaning up GPIO pins on your Raspberry Pi using SplashKit. + +- **[Introduction to ADC (Analog-to-Digital Converter)](/book/part-1-instructions/3-control-flow/0-panorama/10-2-adc/)** + Discover what an ADC is, why you need it for reading analog sensors, and how it converts real-world signals into digital data. + +- **[Exploring the Potentiometer](/book/part-1-instructions/3-control-flow/0-panorama/10-3-exploring-potentiometer/)** + Understand how a potentiometer works, how to wire it with an ADC, and how to use it to control your projects. + *Tip:* Try using the `rnd()` function to add a fun twist to your projects by generating random target values. + +Embark on this journey of control flow, where each concept you learn empowers you to create something smarter. Happy coding! diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-1-gpio-functions.mdx b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-1-gpio-functions.mdx new file mode 100644 index 00000000..39034075 --- /dev/null +++ b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-1-gpio-functions.mdx @@ -0,0 +1,52 @@ +--- +title: GPIO Functions to use +sidebar: + label: "- GPIO Functions to use" + attrs: + class: pi +--- + +# Raspberry Pi GPIO Functions + +Welcome to the guide on Raspberry Pi GPIO functions with SplashKit! In this guide, you’ll learn about the basic functions that let you interact with your Raspberry Pi’s pins. These functions help you initialize the GPIO system, read sensor values, write outputs, and clean up afterward. + +## Key Functions + +- **RaspiInit()** + Initializes the GPIO system. Always call this at the very start of your program to prepare the pins for use. + +- **RaspiCleanup()** + Resets and cleans up the GPIO settings when your program ends. This is essential to prevent any unintended behavior. + +- **RaspiGetMode(pin)** + Retrieves the current mode (input, output, etc.) of a specific GPIO pin. + +- **RaspiRead(pin)** + Reads the current value from a GPIO pin (typically HIGH or LOW). This is used for sensors like push buttons. + +- **RaspiSetMode(pin, mode)** + Sets the mode of a GPIO pin. For example, you might set a pin to input mode for a sensor or output mode for an LED. + +- **RaspiSetPullUpDown(pin, pull)** + Configures the internal pull-up or pull-down resistors for a GPIO pin, which helps ensure a stable signal when no input is provided. + +- **RaspiSetPwmDutycycle(pin, duty)** + Adjusts the duty cycle of a PWM signal on a pin. This is useful for controlling things like motor speed or LED brightness. + +- **RaspiSetPwmFrequency(pin, frequency)** + Sets the frequency for PWM output on a GPIO pin. + +- **RaspiSetPwmRange(pin, range)** + Defines the range (or resolution) for PWM values. + +- **RaspiSpiOpen(...)**, **RaspiSpiTransfer(...)**, **RaspiSpiClose()** + These functions help open an SPI connection, transfer data, and close the connection, respectively. + +- **RaspiWrite(pin, value)** + Writes a digital value (HIGH or LOW) to a GPIO pin. This function is how you control outputs like LEDs. + +> **Note:** +> In future lessons, we will add a few more functions for managing pin types, setting different pin modes, and even working with I²C functions. For now, focus on these core functions to get comfortable with basic GPIO operations. + +Enjoy experimenting with these functions and start creating your own interactive projects! + diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-2-ADC.mdx b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-2-ADC.mdx new file mode 100644 index 00000000..02e73b79 --- /dev/null +++ b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-2-ADC.mdx @@ -0,0 +1,53 @@ +--- +title: Introduction to ADC +sidebar: + label: "- Introduction to ADC" + attrs: + class: pi +--- + +# Unlocking Analog Potential: ADC, Digital vs. Analog Sensors, and Beyond + +Welcome to the exciting world of Control and Flow! In this chapter, we’re expanding our horizons by moving beyond simple digital signals. As you may know, the Raspberry Pi doesn’t have a built-in ADC (Analog-to-Digital Converter). That’s why we’re introducing the ADS7830 module to bridge the gap and let us work with analog sensors. Let’s break it down in a fun and friendly way. + +--- + +## What is an ADC? + +An **ADC (Analog-to-Digital Converter)** is like a magical translator. It takes continuously varying analog signals—think of them as smooth, flowing waves—and converts them into digital numbers that your Raspberry Pi can understand and process. +- **Why use an ADC?** + Because while digital signals are like on/off switches, many real-world signals (like light, sound, or temperature) vary continuously. Without an ADC, our Pi would miss out on all that rich, detailed information! + +--- + +## Digital Sensors vs. Analog Sensors +![DigitalvsAnalog](./images/digital-vs-analog-sensor.png) +### Digital Sensors +- **What are they?** + Digital sensors output one of two states: HIGH or LOW (think binary 1 or 0). +- **Examples:** + - **Push Buttons:** When pressed, they switch from LOW to HIGH. + - **Digital Temperature Sensors:** They might simply indicate if the temperature is above or below a set threshold. + +### Analog Sensors +- **What are they?** + Analog sensors produce a range of values. Instead of just “on” or “off,” they can tell you exactly how bright it is, how loud it is, or even how hot it is! +- **Examples:** + - **Light Sensors:** Measure varying light intensities. + - **Potentiometers:** Detect changes in position or rotation. + - **Analog Temperature Sensors:** Provide a continuous range of temperature readings. + +--- + +## Why Are We Moving to Analog Sensors? + +Our journey so far has been powered by digital sensors, which are great for simple tasks—but they can be limiting because they only tell us yes or no. With analog sensors, we can capture much more detail. Imagine being able to measure subtle changes in light or temperature; that opens up a whole new world of possibilities for creative projects and deeper control logic. + +Using an ADC like the ADS7830, we can convert these nuanced analog signals into digital data that our Raspberry Pi can work with. This upgrade lets us think bigger and design more sophisticated projects that can react to a rich variety of real-world inputs. + +:::caution +Currently, our SplashKit setup doesn’t include an ADC connection. We’re laying the groundwork for future lessons where we’ll dive into interfacing with the ADS7830 and making full use of analog sensors. Stay tuned! +::: + + +By unlocking analog sensor capabilities, we’re not just overcoming a limitation—we’re opening the door to a broader, more dynamic world of programming and electronics. Enjoy this new adventure in Control and Flow, and get ready to explore a whole new level of creativity with your Raspberry Pi! \ No newline at end of file diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-3-exploring-potentiometer.mdx b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-3-exploring-potentiometer.mdx new file mode 100644 index 00000000..3a53a8c8 --- /dev/null +++ b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/10-3-exploring-potentiometer.mdx @@ -0,0 +1,43 @@ +--- +title: Exploring the Potentiometer +sidebar: + label: "- Exploring Potentiometer" + attrs: + class: pi +--- + +# Exploring the Potentiometer + +In this guide, we’ll explore how a potentiometer works and how you can use it in your Raspberry Pi projects. A potentiometer is a variable resistor that produces a variable voltage as you turn its knob, making it a versatile sensor for many applications. + +![Potentiometer](./images/Raspberry-Pi-analog-Input-Potentiometer.webp) + +## What Is a Potentiometer? + +A potentiometer has three terminals: +- **Two Outer Terminals:** These connect to a fixed voltage (e.g., one to 3.3V and the other to Ground). +- **Middle Terminal (Wiper):** This provides a variable voltage that changes as you rotate the knob. + +## How to Use a Potentiometer + +1. **Wiring the Potentiometer:** + - Connect one outer terminal to the 3.3V supply. + - Connect the other outer terminal to Ground (GND). + - Connect the middle terminal to an ADC channel (such as on the ADS7830) so that the variable voltage can be read by your Raspberry Pi. + +2. **Reading the Potentiometer:** + - The ADC converts the analog voltage from the potentiometer into a digital value. + - This value can be scaled (for example, 0 to 100) to represent a percentage. + +3. **Practical Application:** + - Create a project where you adjust the potentiometer to match a target percentage. + - **Tip:** Use the random number function `rnd()` as a fun way to generate target values or simulate sensor noise. Checkout + +## Example Scenario + +Imagine a game where your goal is to adjust the potentiometer so that the digital reading comes as close as possible to a randomly generated target percentage. Even though we haven’t yet implemented ADC functionality in our SplashKit setup, this guide prepares you for working with analog inputs and designing interactive projects. + +--- + +This guide helps you understand the role of the potentiometer in embedded systems. By learning how to wire and read a potentiometer, you unlock a key component for more dynamic and responsive projects on your Raspberry Pi. + diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/images/Raspberry-Pi-analog-Input-Potentiometer.webp b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/images/Raspberry-Pi-analog-Input-Potentiometer.webp new file mode 100644 index 00000000..37c8eb95 Binary files /dev/null and b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/images/Raspberry-Pi-analog-Input-Potentiometer.webp differ diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/images/Raspberry-Pi-control-LED-with-Potentiometer.webp b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/images/Raspberry-Pi-control-LED-with-Potentiometer.webp new file mode 100644 index 00000000..6a020f5c Binary files /dev/null and b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/images/Raspberry-Pi-control-LED-with-Potentiometer.webp differ diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/images/digital-vs-analog-sensor.png b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/images/digital-vs-analog-sensor.png new file mode 100644 index 00000000..67a56593 Binary files /dev/null and b/src/content/docs/book/part-1-instructions/3-control-flow/0-panorama/images/digital-vs-analog-sensor.png differ diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/1-tour/4-00-potentiometer.mdx b/src/content/docs/book/part-1-instructions/3-control-flow/1-tour/4-00-potentiometer.mdx new file mode 100644 index 00000000..c6eef726 --- /dev/null +++ b/src/content/docs/book/part-1-instructions/3-control-flow/1-tour/4-00-potentiometer.mdx @@ -0,0 +1,152 @@ +--- +title: Read a Potentiometer +sidebar: + attrs: + class: pi +--- + +import { Accordion, AccordionItem, Steps } from "accessible-astro-components"; + +# Overview + +In this guide, we’ll explore how to use SplashKit’s new ADC functions to read a potentiometer on your Raspberry Pi. A potentiometer is a variable resistor whose wiper outputs a voltage you can adjust—perfect for dynamic inputs. With the ADS7830 ADC module, SplashKit can now convert that analog voltage into digital values. Let’s dive in! + +# Hardware Setup + +You’ll need: + +- **Potentiometer** (3.3 V ↔ GND with wiper) +- **ADS7830 ADC Module** (I²C) +- **Breadboard & Jumper Wires** +- **Raspberry Pi** (with I²C enabled) + +### Wiring + +1. **Potentiometer:** + - Outer legs → 3.3 V and GND + - Middle (wiper) → ADC input channel A0 +2. **ADS7830 Module:** + - Connect SDA, SCL, VCC (3.3 V) and GND to the Pi + - Connect wiper to A0 on ADS7830 + +--- + +# 1. Initialization + +```csharp +using System; +using SplashKitSDK; +using static SplashKitSDK.SplashKit; + + +// Prepare GPIO and I²C +RaspiInit(); + +// Open the ADS7830 on I2C bus 1, address 0x48 +AdcDevice adc = OpenAdc("ADC1", 1, 0x48, AdcType.Ads7830); +```` + +--- + +# 2. Reading the Potentiometer + +We’ll take three readings—each after you adjust the knob and press Enter: + +```csharp +// First reading +WriteLine("Turn the knob, then press Enter to record reading 1:"); +ReadLine(); +int reading1 = ReadAdc(adc, AdcPin.AdcPin0); +WriteLine($"Reading 1: {reading1}"); + +// Second reading +WriteLine("Adjust again, then press Enter for reading 2:"); +ReadLine(); +int reading2 = ReadAdc(adc, AdcPin.AdcPin0); +WriteLine($"Reading 2: {reading2}"); + +// Third reading +WriteLine("One more time—press Enter for reading 3:"); +ReadLine(); +int reading3 = ReadAdc(adc, AdcPin.AdcPin0); +WriteLine($"Reading 3: {reading3}"); +``` + +--- + +# Full Code Example + + + + +```csharp +using System; +using SplashKitSDK; +using static SplashKitSDK.SplashKit; + + +// Initialize GPIO & I�C + +RaspiInit(); +// Open ADC device on bus 1, address 0x48 +AdcDevice adc = OpenAdc("ADC1", 1, 0x48, AdcType.Ads7830); +// Three separate readings +WriteLine("Turn the knob, then press Enter to record reading 1:"); +ReadLine(); + +int r1 = ReadAdc(adc, AdcPin.AdcAdcPin0); +WriteLine($"Reading 1: {r1}"); +WriteLine("Adjust again, then press Enter for reading 2:"); +ReadLine(); + +int r2 = ReadAdc(adc, AdcPin.AdcAdcPin0); +WriteLine($"Reading 2: {r2}"); +WriteLine("One more time, press Enter for reading 3:"); +ReadLine(); + +int r3 = ReadAdc(adc, AdcPin.AdcAdcPin0); +WriteLine($"Reading 3: {r3}"); + +// Clean up +CloseAdc(adc); +RaspiCleanup(); +``` + + + + +--- + +# Build and Run + +1. **Create Project** + + ```bash + mkdir ReadPotentiometer + cd ReadPotentiometer + dotnet new console + dotnet add package SplashKit + code . + ``` + +2. **Paste the Code** into `Program.cs`. + +3. **Build** + + ```bash + dotnet build + ``` + +4. **Run** + + ```bash + dotnet run + ``` + +--- + +# Expected Outcome + +When you run the program, you’ll be prompted three times to adjust the potentiometer and press Enter. Each prompt will display the actual ADC reading from channel A0 (0–255). Enjoy exploring analog inputs with SplashKit’s new ADC support! + +![Terminal output of the potentiometer](./images/potentiometer_output.png) diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/1-tour/images/potentiometer_output.png b/src/content/docs/book/part-1-instructions/3-control-flow/1-tour/images/potentiometer_output.png new file mode 100644 index 00000000..2d4d05b6 Binary files /dev/null and b/src/content/docs/book/part-1-instructions/3-control-flow/1-tour/images/potentiometer_output.png differ diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/3-explore/3-0-test-knowledge.md b/src/content/docs/book/part-1-instructions/3-control-flow/3-explore/3-0-test-knowledge.md index 44bcbb05..0ee5d3e7 100644 --- a/src/content/docs/book/part-1-instructions/3-control-flow/3-explore/3-0-test-knowledge.md +++ b/src/content/docs/book/part-1-instructions/3-control-flow/3-explore/3-0-test-knowledge.md @@ -7,3 +7,4 @@ Having explored this leg of the journey you are now approaching a rest stop and - [Simple Stats](/book/part-1-instructions/3-control-flow/3-explore/3-1-calculator) - a program that lets the user enter values and receive running statistics. - [Simple music player](/book/part-1-instructions/3-control-flow/3-explore/3-2-music) - a program that lets the user load and play music. - [Explore event loops](/book/part-1-instructions/3-control-flow/3-explore/3-3-events) - a program to let you explore event loops +- [Judgement Game](/book/part-1-instructions/3-control-flow/3-explore/3-4-judgement-game/) - a program to design a judgement game using a potentiometer diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/3-explore/3-4-judgement-game.mdx b/src/content/docs/book/part-1-instructions/3-control-flow/3-explore/3-4-judgement-game.mdx new file mode 100644 index 00000000..0a8ee896 --- /dev/null +++ b/src/content/docs/book/part-1-instructions/3-control-flow/3-explore/3-4-judgement-game.mdx @@ -0,0 +1,65 @@ +--- +title: Judgement Game +sidebar: + attrs: + class: pi + label: " - Judgement Game" +--- + + +import { Accordion, AccordionItem, Steps } from 'accessible-astro-components' + +# Task Overview + +Your challenge is to build a simple judgement game using a potentiometer and an ADC (such as the ADS7830). In this game, the program will display a target percentage, and your goal is to adjust the potentiometer so that its output is as close as possible to that target. When you think you've matched the target, press Enter to record your reading. The program will then calculate and display how close your reading is to the target. + +**Expected Outcome:** +- A target percentage (e.g., 65%) is displayed. +- You adjust the potentiometer and, when ready, press Enter. +- The ADC converts the potentiometer's analog voltage into a digital value (assumed to be between 0 and 100). +- The program calculates the absolute difference between the target percentage and your potentiometer reading. +- The final output shows the target, your reading, and the difference. + +![Example Output](./images/judgement_game_output.png) + +# What You Need to Do + +1. **Initialize the ADC and Potentiometer:** + - Set up your ADC to read the value from the potentiometer. + - Ensure you convert the raw ADC value to a percentage (assume a scale from 0 to 100). + +2. **Generate or Display a Target Value:** + - Display a fixed target percentage (for example, 65%). + +3. **Prompt the User:** + - Ask the user to adjust the potentiometer until they believe the value is close to the target. + - Instruct the user to press Enter when ready to record the reading. + +4. **Calculate the Difference:** + - Compute the absolute difference between the target percentage and the recorded potentiometer percentage. + +5. **Display the Outcome:** + - Print the target percentage, the potentiometer reading, and the difference (i.e., how close or far the reading is from the target). + +# Hints + +- **Reading the Potentiometer:** + For details on how to read a potentiometer, refer to: + [here](/book/part-1-instructions/3-control-flow/1-tour/4-00-potentiometer/) + +- **Data Conversion:** + Consider how to map the ADC's raw value into a percentage (0–100). + +- **Arithmetic Calculation:** + Use basic arithmetic to compute the absolute difference between your target and the reading. + +# Important Note + +At this stage, focus on basic control and flow. **Do not use loops or conditionals.** Your program should execute the steps in sequence: +1. Display the target. +2. Wait for the user to press Enter. +3. Read and convert the potentiometer value. +4. Immediately calculate and display the result. + + +Design your program step by step, test each individual part, and then integrate them to complete your judgement game. This task helps you practice working with analog inputs and simple arithmetic, paving the way for more advanced control and logic in the future. Happy coding and good luck! \ No newline at end of file diff --git a/src/content/docs/book/part-1-instructions/3-control-flow/3-explore/images/judgement_game_output.png b/src/content/docs/book/part-1-instructions/3-control-flow/3-explore/images/judgement_game_output.png new file mode 100644 index 00000000..a725715d Binary files /dev/null and b/src/content/docs/book/part-1-instructions/3-control-flow/3-explore/images/judgement_game_output.png differ