ברוכים הבאים לאשף יצירת הקוד עבור רכיבים מסדרת ה-Plug-n-Play!
האשף מסוגל לייצר תוכניות פשוטות המבצעות פעולות שונות כתגובה לאירוע במערכת. לדוגמה: להדליק לד כשלוחצים על כפתור.
הפעולות מתבססות על היכולות של רכיבי ה-Plug-n-Play שתבחרו במהלך הגדרת הפעולה.
החשיבה צריכה להיות מבוססת על פונקציונליות של הרכיבים ולא רעיון כללי לביצוע. למשל: להדליק
לד כשלוחצים על כפתור, ולא לתת חיווי כשמכניסים מכתב לתיבת דואר.
בחירת הפעולה מתבצעת בשלושה שלבים:
- בחירת כיוון פעולה כללי, לדוגמה: להגיב לכפתור, לשלוט על לד
- בחירת רכיב Plug-n-Play המתאים לפעולה שבחרתם בשלב הקודם, לדוגמה: מודול עם כפתורים, לד על כרטיס פיתוח
- בחירת ממשק מדוייק של הרכיב שבחרתם בשלב הקודם, לדוגמה: להגיב ללחיצת כפתור, להדליק לד
האשף יציג לכם רק את הפעולות ורכיבים הרלוונטיים לבחירה שלכם ויבקש ערכים לביצוע הפעולה, לדוגמה: קצב הבהוב הלד.
מה שהאשף לא יודע לעשות, זה להוסיף משתנים, לוגיקה והתנאיות לקוד. את זה תצטרכו להוסיף בעצמכם לפי צורכי הפרוייקט שלכם.
פעולות לביצוע
- לבצע פעולה בצורה מחזורית #0
- לשנות שורה במסך LCD (שורה 0)
- להדפיס מחרוזת טקסט על מסך LCD ("Temp: ")
- להדפיס ערך רכיב אחר על מסך LCD
- להדפיס מחרוזת טקסט לחיבור טורי (Serial Monitor) ("Temperature: ")
- להדפיס ערך רכיב אחר דרך חיבור טורי (Serial Monitor) (שורה חדשה)
- לבצע פעולה לאחר פרק זמן (דורש הפעלה נפרדת של שעון) #1
- לשנות שורה במסך LCD (שורה 1)
- להדפיס מחרוזת טקסט על מסך LCD (" ")
- לשנות צבע רקע של מסך LCD (0xFFFFFF)
- לכבות לד (LED)
- לבצע פעולה כאשר טמפרטורה עולה מעל הסף הגבוה (30°C)
- לשנות שורה במסך LCD (שורה 1)
- להדפיס מחרוזת טקסט על מסך LCD ("!!! TOO HOT !!!")
- לשנות צבע רקע של מסך LCD (0xFF0000)
- להבהב בלד (LED) (100mSec, 100mSec)
- להפעיל מחודש את השעון (Timer) #1
רכיבים בשימוש
- כרטיס פיתוח Plug-n-Play SAMD21 Dev Board
- מודול חיישן טמפרטורה STTS22H עם מתאם QWIIC<-->PnP (MODE_1HZ)
- מסך SerLCD של SparkFun עם מתאם QWIIC<-->PnP
קוד לצריבה על כרטיס פיתוח Plug-n-Play SAMD21 Dev Board
- קוד: בחר הכל
#include <Arduino.h>
#include "PlugAndPlay.h"
// Objects creation
// EBF - Event Based Framework is the engine behind the Plug-n-Play processing
EBF_Core EBF;
// Serial interface via the USB connection. Use IDE Serial Monitor to see the printouts.
PnP_Serial serial;
// Status LED on the development board
PnP_StatusLed statusLed;
// STTS22H temperature sensor module
PnP_Module_STTS22H_TemperatureSensor tempSensor;
// SparkFun's QWIIC SerLCD connected to the PnP system using QWIIC<->PnP adapter with needed EEPROM values
PnP_Module_SparkFun_QWIIC_SerLCD lcd;
// Timer objects. Change the names to something more meaningfull for your project
EBF_Timer timer0;
EBF_Timer timer1;
// This function will be called when timer #0 expires
void onTimer0()
{
// Change the LCD cursor to the beginning of row 0
lcd.SetCursor(0, 0);
// Print the string on the LCD
lcd.print("Temp: ");
// Print device value
lcd.print(tempSensor.GetValueC());
// Print the string
serial.print("Temperature: ");
// Print device value
serial.println(tempSensor.GetValueC());
// Timers are always one-shot, start it for the next run
timer0.Start();
}
// This function will be called when timer #1 expires
void onTimer1()
{
// Change the LCD cursor to the beginning of row 1
lcd.SetCursor(0, 1);
// Print the string on the LCD
lcd.print(" ");
// Change the LCD backlight color
lcd.SetBacklight(0xFFFFFF);
// Turn the LED OFF
statusLed.Off();
// Timers are always one-shot, start it for the next run if needed
}
// This function will be called when temperature sensor will report high temperature
void onHighTemp()
{
// Change the LCD cursor to the beginning of row 1
lcd.SetCursor(0, 1);
// Print the string on the LCD
lcd.print("!!! TOO HOT !!!");
// Change the LCD backlight color
lcd.SetBacklight(0xFF0000);
// Blink the LED for 100 mSec in ON sate, 100 mSec in OFF state
statusLed.Blink(100, 100);
// Restart timer #1 (same as stopping and starting again)
timer1.Restart();
}
// All the setup should be done in that function
void setup()
{
// Uncomment that line if you want to wait for establishment of the serial connection
//while (!serial) {}
// EBF error reporting interface should be set before the initialization
// to see the failures during the init
EBF.SetErrorHandlerSerial(serial);
// EBF is the first thing that should be initialized
// The parameter specifies how many interrupt events can be queued in transition
// between the interrupt execution (ISR) and the normal run loop.
// For light applications without fast events and long execution, number 4 might be enough.
// For applications with fast burst of events, or long execution for some of them, might need a larger number
EBF.Init(16);
// Initialize serial interface object
serial.Init();
// Initialize the status LED
statusLed.Init();
// Initialize the temperature sensor module and set its measurement period to MODE_1HZ
tempSensor.Init();
tempSensor.SetOperationMode(EBF_STTS22H_TemperatureSensor::MODE_1HZ);
// Initialize the LCD module
lcd.Init();
// Clearing the display and setting the background to white
lcd.Clear();
lcd.SetBacklight(0xFFFFFF);
// Initialize timer #0 to 1000 mSec
timer0.Init(onTimer0, 1000);
// Initialize timer #1 to 3000 mSec
timer1.Init(onTimer1, 3000);
// Start timer #0
timer0.Start();
// You need to add timer1.Start() call to start it from the place you need it
// Setting the high temperature threshold to 30 °C
tempSensor.SetThresholdHigh(30);
// Registering the callback function for the high temperature threshold
tempSensor.SetOnThresholdHigh(onHighTemp);
serial.println("Starting executing callbacks...");
}
void loop() {
// Let EBF to do all the processing
// Your logic should be done in the callback functions
EBF.Process();
}
* אף סוכן AI לא נוצל בהכנת תוכנית זו
* מצאתם טעות או משהו שלא עובד כמו שצריך? דווחו לנו.