Lab2 Sei
Lab2 Sei
Lab2 Sei
AL REPUBLICII MOLDOVA
Universitatea Tehnică a Moldovei
Facultatea Calculatoare, Informatică şi Microelectronică
Departamentul Microelectronică și Inginerie Biomedicală
Raport
pentru lucrarea de laborator Nr.2
A verificat:
Chișinău – 2024
2. Librarile necesare:
Button.h si Button.cpp
Button.h:
// Button.h
#ifndef BUTTON_H
#define BUTTON_H
#include <Arduino.h>
class Button {
private:
int pin;
unsigned long lastPressTime;
unsigned long debounceDelay;
bool pressed;
public:
Button(int pin, unsigned long debounceDelay = 50);
void begin();
bool isPressed();
};
#endif
Button.cpp:
// Button.cpp
#include "Button.h"
void Button::begin() {
pinMode(pin, INPUT_PULLUP); // Assuming the button is connected between the pin and
ground
}
bool Button::isPressed() {
unsigned long currentTime = millis();
bool currentState = digitalRead(pin) == LOW; // LOW when button is pressed due to
INPUT_PULLUP
return false;
}
RGB_LED.h
#ifndef RGB_LED_H_
#define RGB_LED_H_
#include "Arduino.h"
class RGB_LED {
public:
RGB_LED(int redPin, int greenPin, int bluePin, PinController* pinController) :
_redPin(redPin), _greenPin(greenPin), _bluePin(bluePin),
_pinController(pinController) {
_pinController->setPin(_redPin, LOW);
_pinController->setPin(_greenPin, LOW);
_pinController->setPin(_bluePin, LOW);
}
private:
int _redPin;
int _greenPin;
int _bluePin;
PinController* _pinController;
};
#endif /* RGB_LED_H_ */
Comunication.h si Comunication.cpp
Comunication.h
#ifndef COMUNICATION_H
#define COMUNICATION_H
#include <Arduino.h>
#include <stdio.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Change to LiquidCrystal_I2C.h for I2C display support
#include <Keypad.h>
#include "config.h"
void stdio_serial_setup(void);
void stdio_lcd_and_keypad(void);
void stdio_lcd_and_serial(void);
#endif
Comunication.cpp
#include "Comunication.h"
#include <config.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
void stdio_serial_setup(void) {
Serial.begin(BAUDRATE);
fdev_setup_stream(
&stream,
stdio_serial_putchar,
stdio_serial_getchar,
_FDEV_SETUP_RW
);
stdin = &stream;
stdout = &stream;
}
void stdio_lcd_and_keypad(void) {
void stdio_lcd_and_serial(void) {
// Initialize the LCD
lcd.init();
lcd.backlight();
Serial.begin(BAUDRATE);
fdev_setup_stream(
&stream,
stdio_lcd_and_serial_putchar,
stdio_keypad_getchar,
_FDEV_SETUP_RW
);
stdin = &stream;
stdout = &stream;
}
config.h
#ifndef CONFIG_H
#define CONFIG_H
#define BAUDRATE 9600
// Define pin mappings
#define LCD_VCC 26
#define LCD_GND 2
#define LCD_SDA 20
#define LCD_SCL 21
#define RGB_COM 53
#define RGB_R 13
#define RGB_G 12
#define RGB_B 11
#define BTN1_PIN 22
#define BTN2_PIN 23
#define BTN3_PIN 24
#define KEYPAD_R1 27
#define KEYPAD_R2 26
#define KEYPAD_R3 29
#define KEYPAD_R4 28
#define KEYPAD_C1 31
#define KEYPAD_C2 30
#define KEYPAD_C3 33
#define KEYPAD_C4 32
#endif
main.cpp SO Secvential
#include <Arduino.h>
#include "Button.h"
#include "config.h"
#include "RGB_LED.h"
#include "Comunication.h"
#include <TaskScheduler.h>
Button button_r (BTN1_PIN,150);
Button button_g (BTN2_PIN,150);
Button button_b (BTN3_PIN,150);
Scheduler scheduler;
Task t1(300, TASK_FOREVER, [](){
if (button_r.isPressed()) {
redLedState=!redLedState;}
if (redLedState==true) {
rgbLed.setColor(1, 0, 0);
}});
int freq = 1;
void setup() {
button_b.begin();
button_g.begin();
button_r.begin();
//stdio_serial_setup(); // Initialize Serial setup
stdio_lcd_and_keypad();
scheduler.addTask(t1);
scheduler.addTask(t2);
scheduler.addTask(t3);
scheduler.addTask(tIdle);
t1.enable();
t2.enable();
t3.enable();
tIdle.enable();
scheduler.startNow(); // Start the scheduler
}
void loop() {
scheduler.execute();
}
Main.cpp FreeRTOS
#include <Arduino_FreeRTOS.h>
#include <Arduino.h>
#include "Button.h"
#include "config.h"
#include "RGB_LED.h"
#include "Comunication.h"
void loop() {
// This function should remain empty when using FreeRTOS tasks
}
for (;;) {
if (button_g.isPressed()) {
freq++; // Increment frequency
}
if (button_b.isPressed()) {
freq = max(1, freq - 1); // Decrement frequency, ensuring it's not less than 1
}
}
}
void idle_task (void *pvParameters){
(void)pvParameters;
for(;;){
printf("\f");
if (redLedState==true) {
printf("Task1\n\r");
printf("Led On (RED)\n\r");
//delay(100);
} else {
printf("Task2 \n\r");
printf("Blink \n\r");
printf("freq: %d", freq);
}
}
}