14. Interface and application programming¶
This week we had to:
- Compare as many tool options as possible.
- Document your work on the group work page and reflect on your individual page what you learned.
This week’s group assignment was conducted by student Shushanik Abovyan, who demonstrated creating an application in C#, and instructor Rudolf Igityan, who demonstrated creating applications in Processing.
Creating an application in C¶
The development environment Visual Studio is suitable for creating applications in C#. From the beginning we downloaded this application to the computer.
Then, opening the application, we created a new project based on the Windows Forms Application (.NET) template.
After which the Form1 window opens, which shows the screen of our future application. In the left menu there are tools (for example, a button) with the help of which the visual part of the application is created:
Contact between the application and the board occurs through Serial Port. To do this, you need to look at what port number the board is located under (for example, COM5) and specify it in the settings.
And then going to the code in public Form1() write the following:
serialPort1.Open();
This way we can establish communication between the application and the board.
Shushanik decided to show us an example of how you can control the lighting of an LED using an application. To do this, she created 2 buttons TurnON and TurnOFF.
When the buttons are clicked, the onButton_Click() event is generated.
In the onButton_Click() method we pass the following information to Serial Port:
In the case of pressing the TurnON button we transmit the letter n, and in the case of pressing TurnOFF we transmit the letter f.
Going to the Arduino IDE program after setting up and initializing the pins in the loop() function, we check the condition for the presence of information in the Serial Port:
if(Serial.available())
If the information is present, then we read the line and write it and assign this value to the data variable:
data = Serial.readString();
Since it is a string value, using the charAt(0) method we get the first letter from the string and assign this value to the charData variable:
charData = data.charAt(0);
Then we check with the value n and turn on the LED, and with the value f turn it off:
Also Shushanik showed us how to control the location of the servomotor using this application
Creating an application using Processing¶
First of all, we downloaded the Processing application.
The G4P add-on, which can be found in Tools, is very suitable for creating the visual part of the application:
In the top menu there are tools (red frame) for creating components (for example, buttons).
In our example, we created 2 buttons (green frame). On the right side there is a menu (blue frame) for setting the button color, size, location, text, variable name and event name.
When the window is closed, button click methods are created in the main code, where we can specify the action that should occur when they are clicked.
The Processing syntax is very similar to the Arduino IDE syntax. It basically consists of two required functions public void setup() and public void draw(). The first function is executed once at startup, and the second is always repeated.
In the public void setup() function we created a window with dimensions of 720px by 480px. Also initialized Serial Port.
arduino = new Serial(this, Serial.list()[0], 9600);
Unlike C#, the program itself will find what port number the board is located under.
Rudolf decided to make an application that controls the brightness of the LED built into the Arduino UNO, which is located under PINO 13.
To light the LED, we created a position variable, which will be responsible for the brightness of the LED. Then we transmit its value via Serial Port:
arduino.write(byte(position));
And in the Arduino IDE program we must do the following:
First of all, we create a global variable
int position;
Then in the void setup() function we initialize Serial Port, and also initialize the 13th PIN as OUTPUT and turn it off:
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
Then in the void loop() function we check the condition, if there is a value in Serial Port, then we assign this value to the position variable:
if (Serial.available() > 0) {
position = Serial.read();
}
Since this PIN does not have PWM capabilities, we can achieve this effect using a code by quickly turning the LED on and off:
for (int i = 0; i <= 100; i++) {
digitalWrite(13; HIGH);
delayMicroseconds(10*position);
digitalWrite(13; LOW);
delayMicroseconds(10*(21-position));
}
Comparison of C# and Processing¶
1. Target audience and use:
-
C#: Suitable for professional developers creating commercial and large-scale GUI applications on Windows.
-
Processing: Ideal for artists, designers and students creating interactive art projects and visualizations.
2. Difficulty:
-
C#: Requires a deep understanding of programming and application architecture.
-
Processing: Easy to get started, especially if you need to quickly create a visual project.
3. Integration and scalability:
-
C#: Easily integrates with other Microsoft products and scales well for large projects.
-
Processing: Suitable for small projects and prototypes, but less suitable for large commercial applications.
Ultimately, the choice of language will depend on the developer’s goals, skill level, and type of project being developed. If you need a powerful and flexible tool for creating complex interfaces, C# is the best choice. If you need to create visual projects with minimal code, Processing is a great solution.