Raspberry Pi Pico Microcontroller: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
(Created page with "= References = * [https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/0 Getting Started with the Raspberry Pi Pico] * [https://electrocredible.com/raspberry-pi-pico-ds18b20-temperature-sensor/ Raspberry Pi Pico W With DS18B20 Temperature Sensor – MicroPython Guide] * https://www.instructables.com/Raspberry-Pi-Pico-DHT22-AM2302-Temperature-Sensor-/# Raspberry Pi Pico -- DHT22 (AM2302) Temperature Sensor] * [https://www.sarcnet.org/workshops.html R...")
 
 
(2 intermediate revisions by the same user not shown)
Line 4: Line 4:
* https://www.instructables.com/Raspberry-Pi-Pico-DHT22-AM2302-Temperature-Sensor-/# Raspberry Pi Pico -- DHT22 (AM2302) Temperature Sensor]
* https://www.instructables.com/Raspberry-Pi-Pico-DHT22-AM2302-Temperature-Sensor-/# Raspberry Pi Pico -- DHT22 (AM2302) Temperature Sensor]
* [https://www.sarcnet.org/workshops.html Raspberry Pi Pico workshop with Object Oriented Programming]
* [https://www.sarcnet.org/workshops.html Raspberry Pi Pico workshop with Object Oriented Programming]
[[File:Screenshot 2023-10-30 at 6.14.21 am.png | 900px]]
= Object Oriented Programming =
* [https://www.dropbox.com/scl/fi/1ecl6c5qj2ox3wriq7oh3/Object-Oriented-Programming-with-Joe-and-Julie.pdf?rlkey=8l93y11epgvd08ke6sx3yfau2&dl=0 Object Oriented Programming Work Sheet]
[[File:Screenshot 2023-10-30 at 6.13.11 am.png | 900px]]
= Button Class =
<syntaxhighlight lang="c++">
// Button.h
#ifndef BUTTON_H
#define BUTTON_H
class Button {
public:
    Button(int pin);
    bool isPressed();
private:
    int buttonPin;
};
#endif
</syntaxhighlight>
<syntaxhighlight lang="c++">
// Button.cpp
#include "Button.h"
#include "Arduino.h"
Button::Button(int pin) {
    buttonPin = pin;
    pinMode(buttonPin, INPUT);
}
bool Button::isPressed() {
    return digitalRead(buttonPin) == HIGH;
}
</syntaxhighlight>
<syntaxhighlight lang="c++">
</syntaxhighlight>
= LED Class =
<syntaxhighlight lang="c++">
// LED.h
#ifndef LED_H
#define LED_H
class LED {
public:
    LED(int pin);
    void turnOn();
    void turnOff();
private:
    int ledPin;
};
#endif
</syntaxhighlight>
<syntaxhighlight lang="c++">
// LED.cpp
#include "LED.h"
#include "Arduino.h"
LED::LED(int pin) {
    ledPin = pin;
    pinMode(ledPin, OUTPUT);
}
void LED::turnOn() {
    digitalWrite(ledPin, HIGH);
}
void LED::turnOff() {
    digitalWrite(ledPin, LOW);
}
</syntaxhighlight>
= Switch using Button and LED =
<syntaxhighlight lang="c++">
#include "Button.h"
#include "LED.h"
const int buttonPin = 2; // Connect the button to pin 2
const int ledPin = 13;  // Connect the LED to pin 13
Button myButton(buttonPin);
LED myLED(ledPin);
void setup() {
    Serial.begin(9600);
}
void loop() {
    if (myButton.isPressed()) {
        myLED.turnOn();
        Serial.println("Button Pressed!");
    } else {
        myLED.turnOff();
    }
}
</syntaxhighlight>
Explanation:
* '''Button Class:''' The Button class is responsible for handling the button. It has a constructor to initialize the pin and a method (isPressed) to check if the button is pressed.
* '''LED Class:''' The LED class is responsible for handling the LED. It has a constructor to initialize the pin and methods (turnOn and turnOff) to control the LED.
* '''Sketch:'''
** In the sketch, we create instances of the Button and LED classes and connect them to the specified pins.
** In the loop function, we continuously check if the button is pressed using the isPressed method. If it is pressed, the LED is turned on; otherwise, it's turned off.
** Make sure to connect the button to the defined buttonPin and the LED to the defined ledPin. Adjust the pin numbers in the sketch accordingly based on your hardware setup.

Latest revision as of 09:18, 17 November 2023

References

Object Oriented Programming

Button Class

// Button.h
#ifndef BUTTON_H
#define BUTTON_H

class Button {
public:
    Button(int pin);
    bool isPressed();

private:
    int buttonPin;
};

#endif
// Button.cpp
#include "Button.h"
#include "Arduino.h"

Button::Button(int pin) {
    buttonPin = pin;
    pinMode(buttonPin, INPUT);
}

bool Button::isPressed() {
    return digitalRead(buttonPin) == HIGH;
}


LED Class

// LED.h
#ifndef LED_H
#define LED_H

class LED {
public:
    LED(int pin);
    void turnOn();
    void turnOff();

private:
    int ledPin;
};

#endif
// LED.cpp
#include "LED.h"
#include "Arduino.h"

LED::LED(int pin) {
    ledPin = pin;
    pinMode(ledPin, OUTPUT);
}

void LED::turnOn() {
    digitalWrite(ledPin, HIGH);
}

void LED::turnOff() {
    digitalWrite(ledPin, LOW);
}

Switch using Button and LED

#include "Button.h"
#include "LED.h"

const int buttonPin = 2; // Connect the button to pin 2
const int ledPin = 13;   // Connect the LED to pin 13

Button myButton(buttonPin);
LED myLED(ledPin);

void setup() {
    Serial.begin(9600);
}

void loop() {
    if (myButton.isPressed()) {
        myLED.turnOn();
        Serial.println("Button Pressed!");
    } else {
        myLED.turnOff();
    }
}

Explanation:

  • Button Class: The Button class is responsible for handling the button. It has a constructor to initialize the pin and a method (isPressed) to check if the button is pressed.
  • LED Class: The LED class is responsible for handling the LED. It has a constructor to initialize the pin and methods (turnOn and turnOff) to control the LED.
  • Sketch:
    • In the sketch, we create instances of the Button and LED classes and connect them to the specified pins.
    • In the loop function, we continuously check if the button is pressed using the isPressed method. If it is pressed, the LED is turned on; otherwise, it's turned off.
    • Make sure to connect the button to the defined buttonPin and the LED to the defined ledPin. Adjust the pin numbers in the sketch accordingly based on your hardware setup.