Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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!
Original file line number Diff line number Diff line change
@@ -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!

Original file line number Diff line number Diff line change
@@ -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!
Original file line number Diff line number Diff line change
@@ -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.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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

<Accordion>
<AccordionItem header="Full Code (C#)">

```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();
```

</AccordionItem>
</Accordion>

---

# 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)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading