Exercise: Setting an oil change reminder with the Scheduler feature.
Scheduler Configuration
Create two tags. One will be named “Oil_Level,” and the other will be called “Oil_consumption.” Oil_Level will be an INT (INT16) with an Initial Value of 50. Oil_consumption will be a BOOL with the starting value as False.
Create a lamp on the page. To create a Switch/Lamp object, go to Insert > Switch/Lamp or left-click the Insert Switch/Lamp icon in the Toolbar. Then, click and drag on the screen to place the lamp. Once the Switch/Lamp object is created, drag the Oil_consumption tag to the lamp. This will bind the Value field to the Tag Oil_consumption.
Create a Rectangle and a Fillable Rectangle. Create a Rectangle by going to Insert > Rectangle or left-clicking the Insert Rectangle icon in the Toolbar. Click and drag across the screen to place the rectangle. While selecting the rectangle, go to Actions and create a new Command in the Under Press section. Create a set Tag Value command. Make sure the command will set Oil_Level to 100 when Pressed. To insert the Fillable Rectangle, click on Insert > Fillable Rectangle or by left-clicking the Insert Fillable Rectangle icon in the Toolbar. Click and drag across the screen to place the Fillable Rectangle.
Bind Oil_Level to the Fillable Rectangle. This can be achieved by clicking and holding the Oil_level tag and dragging it into the Fillable Rectangle. This will bind the current level property of the Fillable Rectangle to the Oil_level tag.
Click on Tools > Script Editor. Click the + icon to create a new script; on the popup window, click OK. Copy and paste the following code into the new script:
Code Block |
---|
thread.msleep(500); // sleeps for 500 ms while(tag.read("Oil_consumption")){ // while Oil_consumption is true thread.sleep(2); // pauses for 2 seconds tag.write("Oil_Level", tag.read("Oil_Level") - 1); // deducts 1 from the oil level. } |
Once pasted in, close the Script Editor by clicking OK. Select the lamp and go to the Actions tab. If Actions does not display, go to View > Properties > Actions and check the box. Under On Press, click the Action field and Create New Command. Click on Add New Command and select Toggle Tag Value. In the Tag field, select the Oil_consumption tag. Then, in the top left of the Command Editor, click the + icon to create a new command. Change the new command to Call script and select the created script.
Create a new Schedule. To achieve this, go to Tools > Schedule Editor. Click the + to create a new schedule; set the Schedule Title to “Oil_Refill.” Set the Start Date to the current date and the Start Time to 5 minutes from the present time. For example, if the current time is 2:30, the Start Time should be 2:35. Set the schedule to repeat every hour. Click the Action Field > Create New Commands. Create a Send Screen Notification Command; the message for this notification should be “Refill the oil.” Click OK to save and exit, and click OK again to exit the Schedule Editor.
Scheduler Runtime
Click Tools >Launch Simulator to launch the Canvas Simulator.
Click the lamp to turn Oil_consumption to True. This will start decreasing Oil_Level every two seconds. If the oil runs out while waiting for the schedule to appear, click the rectangle below the lamp to refill the oil.
After the time has been reached for the schedule to take effect, the screen notification set from the schedule should appear with the message “Refill the Oil.”