15. Interface and application programming¶
Introduction¶
During this week I have to make an interface to control output or input device. I havent done anything like that so i take the set up from output week and start making intreface to control pump. The gratest idea was for this week is to name the program Pump it up.
Experience¶
During my research, I realized that I needed a new program to make an interface. I looked at other students’ documentation and some additional sources. The most popular one was Visual Studio.
I downloaded Visual Studio and set it up. Starting a new project in Visual Studio involves a few steps to choose the base for the future application.
After those steps, I can start actual work on the interface of the future application. On the interface, I want to have “Turn On” and “Turn Off” buttons and a slider to control the pumping speed. To understand what I need for that, I started watching tutorials for making C#-based desktop applications, but I couldn’t find one that fully matched my needs, so I chose to ask Shushan. She explained and guided me to create that simple program.
On the left side is the toolbox, from where I can choose what I want to be on the window: buttons, sliders, text boxes, etc. I can also add some text.
After choosing the first button, which will be the “Turn On” button, I can make it bigger or smaller and locate it on the application window.
In the parameters window, which is located in the bottom right corner, I’m setting the parameters of the button: its name, what is written on it, and the name which I will use in the code to call that button. In my case, “ON” for the interface and “onButton” for the code.
I repeated the same for the “OFF” button.
Here is the code for those buttons. What I like about writing code in Visual Studio is that it helps a lot by understanding what I need.
Next, I chose the trackbar and located it on the interface. I also set the names by choosing the text box and sizing the text and fonts.
An interesting part of the trackbar is that it needs to have a button to confirm the slider value.
In the parameters of the slider, I set the maximum value for analog write, which is 255, and the minimum value, which is 0. Another important parameter is the tick frequency; I set it to 10.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PumpInterface
{
public partial class Form1 : Form
{
String pumpVal= "0";
public Form1()
{
InitializeComponent();
serialPort1.Open();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.Write("on");
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Write("off");
}
private void button3_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(pumpVal))
{
pumpVal = "p0";
}
serialPort1.Write(pumpVal);
}
private void pumptrack_Scroll(object sender, EventArgs e)
{
pumpVal = "p" + pumptrack.Value;
}
}
}
If I understand everything correctly, what was done by Visual Studio with the interface is called the front end. That means I also need a back end. The back end needs to be done with Arduino IDE to control the pump. I repeated all the hardware setup from the output week and copied the code from there and changed it to meet this week’s needs.
String message;
int pump_pin = 0;
int percent = 100;
char data;
void setup() {
pinMode(pump_pin, OUTPUT);
Serial.begin(9600);
Serial.println("privet");
digitalWrite(pump_pin, 1);
}
void loop() {
while (Serial.available()) {
message = Serial.readString();
data=message.charAt(0);
if (message.equals("on")) {
//int outputVal = map(percent, 0, 100, 0, 255);
digitalWrite(pump_pin, 1);
} else if (message.equals("off")) {
digitalWrite(pump_pin, 0);
} else if (data == 'p') {
percent = message.substring(1).toInt();
int outputVal = map(percent, 0, 100, 0, 255);
analogWrite(pump_pin, outputVal);
}
if (message.length() > 0) {
Serial.println(message);
}
}
}
Turning on and off part of code was easy to write. The slider part was bit dificult to understand and make things work, so I asked Shushan for help again.
-
Reading the Message: The code checks if the received message starts with the letter ‘p’.
-
Extracting the Speed: If the message starts with ‘p’, it takes the number that follows ‘p’ as the desired speed percentage (e.g., “p75” means 75% speed).
-
Mapping the Percentage: The code converts this percentage (0 to 100) into a value that the Arduino can use for PWM (Pulse Width Modulation), which ranges from 0 to 255.
-
Setting the Speed: Finally, the code uses analogWrite to set the speed of the pump based on the calculated value.
In simple terms, it takes a percentage you send (like “p75”), converts it into a value that the pump understands, and then adjusts the pump’s speed accordingly.
So it is time to merge the front end and back end. To do that, I just need to specify the same COM port in both codes. In my case, it is COM09.
Also, someone had to keep the button pressed because I accidentally connected GND to 5V and burned the voltage regulator. It was giving just 3 volts, and when we pressed the button, it was high enough to make the pump work.
Group assignment:¶
This week, we compared various tools for creating applications and documented our findings. Shushanik Abovyan demonstrated C# application development with Visual Studio, showing how to control an LED with buttons. Rudolf Igityan showed Processing for visual applications, creating a program to adjust LED brightness on an Arduino UNO.
Comparison: C#: Best for professional, large-scale GUI applications on Windows, requiring programming knowledge. Processing: Ideal for quick visual projects by artists and students, easy to learn, suitable for small projects.
Conclusion¶
This week, I created an interface to control a pump using Visual Studio for the front end and Arduino IDE for the back end. With guidance, I successfully set up buttons and a slider to manage the pump’s operations. This project helped me learn about interface design and integrating it with hardware control.