In this example we use MegunoLink’s Interface Panel visualiser to create a user interface (UI) which allows you to send a serial message to update what IO pin you want to output a signal on. For example if you are flashing and LED on digital pin 1 you can reconfigure it after programming to be on digital pin 2. The Arduino processes the message using our Command Handler.
Downloads
This example is available as part of our Arduino Library but you can also download the MegunoLink interface and Arduino program here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
/* ************************************************************************ Configurable Output Pin This program demonstrates how MegunoLink's Interface Panel and our command handler Arduino library can be used to reconfigure which IO pin your device is using at runtime. The example folder also contains a MegunoLink project, with an Interface Panel to control the IO Pin. Visit: * http://www.MegunoLink.com to download MegunoLink. ************************************************************************ */ #include "MegunoLink.h" #include "CommandHandler.h" int IOPort = 9; //initial IO pin number long LastToggleTime = 0; // Time we last toggled the IO int OnTime = 10; // Amount of time the IO remains on [milliseconds] int OffTime = 100; // Amount of time the IO remains off [milliseconds] CommandHandler<> SerialCommandHandler; void Cmd_SetIOPin(CommandParameter &Parameters) { Serial.print("Setting IO Port to:"); IOPort = Parameters.NextParameterAsInteger(); pinMode(IOPort, OUTPUT); Serial.println(IOPort); } void setup() { Serial.begin(9600); Serial.println("Configurable IO Pin Demo"); Serial.println("-----------------------------"); pinMode(IOPort, OUTPUT); SerialCommandHandler.AddCommand(F("SetIOPin"), Cmd_SetIOPin); } void loop() { SerialCommandHandler.Process(); // Toggle the IO Pin uint32_t uNow = millis(); if (uNow - LastToggleTime < OnTime) digitalWrite(IOPort, HIGH); else digitalWrite(IOPort, LOW); if (uNow - LastToggleTime > OnTime + OffTime) LastToggleTime = uNow; } |