Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Tutorial Series: Serial Port Plugin AGK Arduino Boards

Author
Message
DannyD
7
Years of Service
User Offline
Joined: 29th Aug 2017
Location:
Posted: 8th Jan 2020 00:12 Edited at: 8th Jan 2020 13:44
Since late 2005, I've been using arduino to build hundreds of various automation circuits, and example
projects for arduino. Before arduino I use to work PIC micro processors, which need much more electronic
components and headache's to get something working. Arduino make our life so much easier.

I've decided to do a basic tutorial for using the newly release Serial Port Plugin for AppGameKit with an arduino board ,
This is the Basic Comms between the AppGameKit App and arduino Micro Board, and after this you will be able to
expand for your own use.

This might be usefull for newbies, or anyone who want to get involved in arduino micro processing.

NOTE: This is how I do it, and it's in no way 100% correct. There might be other ways of doing this, but this
will give you a good starting point in AppGameKit and Arduino.

I will discuss some basic arduino commands, aswell as some AppGameKit commands.

Please see https://www.arduino.cc/ to download the arduino IDE and how to setup and use arduino.


The tutorial Series will Cover:

Tutorial: Serial Port Plugin AppGameKit > Arduino (1/4)
In the first tutorial we will build the basic circuit with the arduino, and do the arduino sketch to
test our circuit and make sure all connections/wiring work.

What you will need in this tutorial

* 1 x Arduino Uno/Mega
* 3 x Led's (different or Same Colors)
* 3 x 2.2k Ohm resistors
* 1 x Breadboard / Jumper Wires
* Arduino IDE

Arduino sketch to test the arduino Circuit and make sure it work and all wiring are correct.


Tutorial: Serial Port Plugin AppGameKit > Arduino (2/4)

In this tutorial we will use the Circuit build in the previous tutorial and do a Basic AppGameKit App
to control the LED state (ON/OFF) on the arduino.

You will need.
Circuit Build in Tutorial (1/4)
AGK Classic or AppGameKit Studio


Tutorial: Serial Port Plugin AppGameKit > Arduino (3/4)

In this tutorial we will add 2 switches to our circuit to send commands from Arduino to our AppGameKit App
when we press buttons.

What you will need in this tutorial
Circuit Build in Tutorial (1/4)
* 2 x switches
* 2 x 10K Ohm resistors
* Jumper Wires


Tutorial: Serial Port Plugin AppGameKit > Arduino (4/4)

This tutorial we will combine our circuit from (1/4) and (3/4)
We will also add all the AppGameKit code together for the Main Program...




Get your Arduino's ready... First Tutorial coming soon.....

Cherio
Danny
DannyD
7
Years of Service
User Offline
Joined: 29th Aug 2017
Location:
Posted: 8th Jan 2020 01:00 Edited at: 8th Jan 2020 13:44
Tutorial: Serial Port Plugin AppGameKit > Arduino (1/4)

For this tutorial you will need.
4 x Male to male Jumper Wires
1 x breadboard
1 x Arduino Uno or Arduino Mega
3 x 2.2k or 4.7k resistors

We will use Digital Pins 8,9,10 on the Arduino to connect our LED’s and control the LED status

1. Connect Arduino Pin 8, 9,10 to separate tracks on the bread board. with Jumper Wires
2. Connect the GND wire to the bread board Ground track


3. Connect the 2.2k or 4.7k resistors to each jumper wire on the bread board.


4. Connect the LED’s to the Bread Board.


Note the polarity direction of the LED. The + (Positive – Anode) to the Resistor and the – (Negative – Cathode) to the GND (Negative track on the Bread Board)


5. This is the basic finished circuit.

Let’s open the Arduino IDE and do the Test Program
In AppGameKit we have a DO…..LOOP where the program execute various functions over and over again.



In the Arduino IDE whe have similar code to setup/execute various commands



In Arduino we will use the Digital Pins 8,9,10 to control the 3 LED's

Before we can do anything, we need to tell arduino what these pins will do.
We want these pins, to switch the LED ON/OFF, and to be able to do that, we need these pins as OUTPUT (To go HIGH and give a 5V OUT to the LED's)
Todo that, we will use pinMode to specify the PinMode as Output
https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/

as previously mentioned, we will use pins 8,9,10 as out output PINS to control the LED's
Add the folliwing to the Void Setup() Routine



Now we need to tell arduino what to do with these pins.
For now, we just want to switch the pins on/off to see if the circuit is working.
Add the following in the Void Loop() function.
More Comments in the Code



this will switch the First LED on/OFF with a 10th of a second delay.
We will do the same for the second and third LED connected to pins 9,10



Save the Sketch and Upload to the Arduino

Please see this link on "HOW TO"
https://www.arduino.cc/en/main/howto

Complete Arduino Sketch



When all is well, your leds should be blinking. Any error, check the wiring (+ / -) might be swopped. Incorrect pins used, wires wrong...


You may ask. What is the use of switching Led's on and Off... This can be use in future projects to switch Relays, to activate eg: garage doors, use a timer and start boiling water for a nice cup of coffee in the morning , or any other equipment that need on/off control.

NEXT tutorial, we will look at the AppGameKit APP to switch the LED's on/off by sending a serial string to the Arduino, and the arduino will process the string and do switching according to the string received.

I really hope you find this usefull, as I'm not a author at all...... and also not native English.... so parden any mistakes....

Arduino Sketch Included...

Cherio, Danny

Attachments

Login to view attachments
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 8th Jan 2020 01:16
Wow! This is awesome
DannyD
7
Years of Service
User Offline
Joined: 29th Aug 2017
Location:
Posted: 8th Jan 2020 06:15 Edited at: 8th Jan 2020 13:45
Tutorial: Serial Port Plugin AppGameKit > Arduino (2/4)

In the previous tutorial we build the Arduino circuit and add the Arduino sketch to test our wiring.

This tutorial we will do the code for AppGameKit to send a string to the Arduino, and change the Arduino code to receive the serial string and process as needed.

Let start our AppGameKit Classic or Studio and see what the process will be.

Create a New Project
The first thing to do, is to tell our AppGameKit App, that we will use the Serial Plugin.

#import_plugin SerialPlugin as serial

Next we will create variables for each LED.

LED1 as string = “0”
LED2 as string = “0”
LED3 as String = “0”

Then we will combine the LED1, Led2, Led3 strings to create a combined string to send to the Arduino.
We will add a EndofLine Character to the string to tell Arduino when the string ends, and then process.
Personaly I like to send a “#” character, But you can use any string to terminate (Last Character)
Some People use ‘\n’ as a terminate character. But for this tutorial I use the “#” hash as a Terminate character.
As we progress you will see why I’m doing this…

CurrentString as String
CurrentString = LED1+LED2+LED3+”#” // # tell Arduino to process string,


We will also create a LastString (as a Flag) , to only send the string once it change. This save Processing Power in AppGameKit as well as the Arduino.
Very Helpful when send long strings to Arduino… and prevent Arduino constantly process strings receive…

LastString = CurrentString

Then we need to scan the Serial Port and find available Ports.
If no Ports available the program exit. If there is ports available, use that Port

In The last Tutorial (Cleanup and Spice Up) we will add a CHECK to the Arduino and AppGameKit to monitor on Which comport the Arduino is connected, should there be more than one comport detected.
For now we assume it is only one comport and we use comport = 0

We will finish the AppGameKit Code and test with the PRINT function to see if the String to SEND to the Arduino is correct.
Once we know that is correct, we can add the code for sending in AppGameKit and then Arduino for receiving.


Code for previous Explanation.
Most comment in the code





This code will tell if there is a comport available or Not, and display relevant information.

Next we want to add 3 Buttons, to Switch the 3 LEDs on the Arduino.

As you can see, the Variables led1, led2, led3 is = 0. This will tell Arduino the Led is Turned OFF
If we click the Button, we change the Variable to “1”, and this tell Arduino to switch it ON
If we press the button again, we change back to “0” to switch OFF.
This will be done for all 3 LED’s

Example Strings Arduino can receive
000#, all leds are OFF
100#, LED 1 ON, Led2 OFF, LED 3 OFF
111#, LED1 ON, LED 2 ON, LED3 ON etc…

To be able to do this, we will use the Buttons as Switches, and manipulate the CurrentString

Create 3 Virtual Buttons , and add the Text “OFF” to each button
Add the following below portindex = 0



In the do loop, we will change the currentstring according to the button presses, for ON/OFF Switching
Add the following code in the DO Loop



When you test this code in AppGameKit, your Buttons text will change according to the Led1, Led2, Led3 State.
We need to update the CurrentString and Compare with the LastString to know if we need to send New information to the Arduino

Before the Sync() in the Loop, add the following



Now that we know the string is working when press the buttons, we can add the code to communicate with the Arduino
First we need to Open the Port, to be able to read and write
We use the OpenSerialPort Function, specify the Baudrate (Which we can set in the Arduino) and portindex

Add the following code before the DO loop


And this code after the LOOP



Once everything OK and No connect errors, al we need to do is to add the Command to Send the Currentstring to the serial port
We will use
success = WriteSerialData( instanceID, String, length ) -- length can be -1 to use the string length
Add the BOLD lines to the If statement



Here is the complete AppGameKit code



When you compile the AppGameKit Project, It should work like the following Video



This is all we need for now in the AppGameKit APP. Next we will change our Arduino Sketch to Receive the String from our AppGameKit APP

Open the Arduino IDE, and load our previous Testing Connections example Sketch
Delete/comment the following Lines



Your Arduino Sketch must look like this:



First we need to tell Arduino to read the Serial port for any incoming data
More Comment in the Sketch/Code

Before Void Setup() add the following 2 lines



in the void setup() after pinMode(10,HIGH) add the following 2 lines



Now we need to create a Event to “listen” for any incoming serial data.
We will use a Function SerialEvent, to monitor any incoming data


Add the following after the Void Loop Function



We have now a complete string, because we received the “#”|
NOTE. We havnt done any error checking code. This will come in a later tutorial

In the Main Loop add the following:



Here is the complete arduino Sketch



Upload the sketch onto the arduino ,
If all goes according to plan, you AppGameKit app will be able to switch the Led;s ON/OFF

Attachments

Login to view attachments
DannyD
7
Years of Service
User Offline
Joined: 29th Aug 2017
Location:
Posted: 8th Jan 2020 13:03 Edited at: 8th Jan 2020 13:46
Tutorial: Serial Port Plugin AppGameKit > Arduino (3/4)

In the last Tutorial we finish the AppGameKit APP and Android Sketch, to control the LED's on the arduino with our AppGameKit APP.

In this tutorial we will add 2 switches to our arduino circuit, and create a AppGameKit App, to read the serial data from the Arduino

You will need.
Previous ARduino circuit
2 x Push Buttons

NOTE: there are many ways to read inputs on the arduino.
We can use analog or digital pins to read the Switch positions.
When using analog, we will also add a 10K Ohm pullup resistor.
When using digital we can use the internal Pull Up resistors.
For this tutorial and simplicty I will use Digital Pin 6 and Pin 7 to add our 2 switches to the circuit

Connect the 2 Switches as follows.
One pin of the switch to GND and the other pin (Switch 1) to Digital 6, and Pin for Switch 2 (Digital 7)
See attached Image



First we will do the Arduino Sketch to send the Buttons State to the Serial Port
This is very straight forward and only a few Lines of code
See Sketch for more comments



This is the basic Sketch we need to detect switch state
There is diffrent ways of doing this, but this is the very basic
You can upload the Sketch to the arduino Uno

Example values that will be send according to the above sketch:

@11$ // BTN1 and BTN2 NOT PRESSED
@01$ // BTN 1 Pressed, BTN2 Not Pressed
@00$ // BTN 1 and BTN 2 PRESSED
@10$ // BTN 1 NOT PRESSED, BTN2 PRESSED

We can now do the AppGameKit App, to read the values from the serial port

We will do the basic Serial Port initialize exactly the same as in Tutorial 1
To make this easier, I've create a Basic Serial Template to do all the wanted Port Commands



As we seen before, the Arduino will send Serial data in the following format

@ - Start Character
1 - Switch 1 NOT PRessed
1 - Switch 2 NOT PRessed
$ - End character


The AppGameKit App need to parse this information and display in the APP
For this we will create 2 Text String for Switch 1 and Switch 2

We need to create String variables to save the receive string and the string to be Parsed

Create the following 2 Variables



We will also create 2 Text Fields to display the Switch Status

Add the following Lines before the DO loop



We need to see if there is an y available data in the serial buffer
IF there is any, The Serial Plugin will read all this data into a String, using the Lenght of the receive bytes

After the DO Loop add the following Lines, to read Serial Data from the Arduino
See more comments in the code



Now we know the RECV string contains serial data we can parse the Recv String

We know from the Arduino sketch that the Start Character is "@" and then 2 characters for Switch 1 and Switch 2
We also add a End Character "$" but will not use it now to make things easier.

This is the easier (BUt not always the correct) way to read Serial Data from the Arduino, because the receive string we need to read will ALWAYS contain only 3 Characters

NOTE
Longer Strings will get more complicated, and most times have a checksum at the end of the String to confirm that data has been send Correctly.
IF the Checksum is wrong, the Program will discard the string and read the new string.


For This purpose we will use just the very basic as an example

Ok, we know that our ARduino Send 3 Character, starting with "@"
When we receive the String, we can first look for the "@" character, and then we know the next 2 characters will be the status of Switch 1 and Switch 2.

For this we can use the "Left" command in AGK

if left(recv,1)="@"

This will detect if the character in the string is a "@"
We will also use a tempstring to add 3 Characters after we received the "@" character using the left command again

tempstring=""
tempstring= left(recv,3)

Now that we know tempstring will contain 3 Characters, starting with @, and 11 , or 01, or 00, or 10, we can parse the string and display the text according on the screen

For this we can use the MID command
Lets read the character for Switch 1



We will do the Same for Switch 2



We will then Clear the recv variable and wait for the next Incoming data on the Serial Port

Complete AppGameKit Source



Compiling the APP, conenct the Arduino and if all goes well, you will see something like this...



In the next Tutorial we will combine the arduino Code and AppGameKit Code together to read and write Serial Data at the Same Time.

Attachments

Login to view attachments
DannyD
7
Years of Service
User Offline
Joined: 29th Aug 2017
Location:
Posted: 8th Jan 2020 13:42 Edited at: 8th Jan 2020 13:46
Tutorial: Serial Port Plugin AppGameKit > Arduino (4/4)

Nothing much in this Tutorial. It's Only the Combined AppGameKit App and Arduino Sketch. No coding changes.
If you followed the tutorial from the Beginning nothing should be new.

Arduino Sketch



Complete AGk APP



Here is a Video of the End Result

DannyD
7
Years of Service
User Offline
Joined: 29th Aug 2017
Location:
Posted: 8th Jan 2020 13:47
That's it

This is very Basic, and I'm sure there is other, better ways to do this. But atleast I think it should help someone to get Started...

Enjoy
Danny
psychoanima
6
Years of Service
User Offline
Joined: 19th Jun 2018
Location:
Posted: 8th Jan 2020 18:01
wow! just... wow!
DannyD
7
Years of Service
User Offline
Joined: 29th Aug 2017
Location:
Posted: 9th Jan 2020 06:35 Edited at: 9th Jan 2020 12:48
Made some changes to the arduino code aswell as AppGameKit code.

By adding a new "start" character, in this case a "*", the AppGameKit APP can search all Serial Ports, if more than one, and can detect which port run the "Firmware" on the arduino to communcate with the APP.
If only one port detected, it will search for the start "*" character.

Adding "Serial.print('*'); // Send as first character to identify AppGameKit Firmware " to arduino Code

Complete Arduino Code



I've also add some Graphics and ported to AppGameKit Studio (With scene designer)

Visual LED's will display the Status.
Pressing Button 1 will switch on Visual Light
Pressing Button 2 will make Johnny walk. (This can be handy to build your own game controller)

This is the Main Code for AppGameKit Studio, but can easily bee modified foe AppGameKit Classic



The video will demonstrate the new functions



Attached, find the AppGameKit Studio Project, but it work 100% in AppGameKit Classic.

Enjoy!

Attachments

Login to view attachments
kaband
21
Years of Service
User Offline
Joined: 22nd May 2003
Location: Chicago
Posted: 9th Jan 2020 15:28
This tutorial is awesome. Thanks!
Desslok
9
Years of Service
User Offline
Joined: 9th Feb 2015
Location:
Posted: 9th Jan 2020 20:32
Excellent tutorial! I've been planning to build a custom enclosed cockpit style flight/space fighter game with real buttons, rocker switches, toggles lights using AppGameKit on a PI. Good to know a pc and Arduino is also an option.
DannyD
7
Years of Service
User Offline
Joined: 29th Aug 2017
Location:
Posted: 10th Jan 2020 02:26
@Desslok

I'm actually busy now with a short "addon" to this tutorial using a Raspberry Pi3B

Desslok
9
Years of Service
User Offline
Joined: 9th Feb 2015
Location:
Posted: 10th Jan 2020 19:10
Looking forward to it! The PC/Arduino option allows me to bring more computing power to bear if needed.
Desslok
9
Years of Service
User Offline
Joined: 9th Feb 2015
Location:
Posted: 10th Jan 2020 20:27
I suppose a Pi Zero would make an inexpensive hardware interface. No need to run the game on the Pi itself. This serial plugin is awesome.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 24th Jun 2023 01:54
@DannyD,

THIS post lead me to do a little research and come across this great tutorial.


better late than never

Login to post a reply

Server time is: 2024-11-21 08:41:25
Your offset time is: 2024-11-21 08:41:25