Skip to content

15. Interface and application programming

This week’s assignment was to write an application that interfaces a user with an input &/or output device that I made.

Windows form(Desktop Applicaton)

At first I decided to understand what tool or programming interface I could use. Because I knew C# programming language and I knew that I can made applications and I found tutorial how can I create windows form application and connect it with my board. At first I tested it with Arduino Uno board and after that test it for my board.

I created application with Visual Studio Community 2022 version. Here are the steps. 1. I opened it and select Create a new project option.

  1. After that search windows form and select programming language C# and select Windows Forms App(.NET framework).

  1. Then I changed Project Name and press Create button.

  2. And select framework version.

  3. After that opened window for application designing where I put the elements of my application.

  1. First I added Serial port for communication with board. Serial Port is invisible in GUI.

  1. I changed portName from Serial port properties (the property menu usually on the right bottom side of the window). It need to be the same port that we connect our board(for my example it was COM8).

After that I started programming it. For that I clicked in the icon of SerialPort with mouse right button and select view code option.

I invoke serialPort1.Open(); method that provide connection with Serial Port.

Then I started to make GUI.

For first part I added two buttons and label . One will turn led on and te oter will turn off. For button I changed 2 property text and name. Text visible in GUI and te name we use in proramming.

After that I double clicked the TurnON and it opened page for programming with onButton_Click() event that created automatically when I clicked the button.

When I finished led controll program I also add textbox and button for servo motor control.

After that I added program for controlling RGB led with trackbar. I added 3 trackbars and and 3 buttons for each color.

#include <Servo_megaTinyCore.h>
Servo s1;
String x;
int servoval;
String data;
char d1;
int redPin=7;
int greenPin=0;
int bluePin=1;
int redValue,greenValue,blueValue;
unsigned long  lastDebounceTime = 0;
unsigned long debounceDelay = 5;
int buttonPin=8;
int buttonState;
int lastButtonState = HIGH;

void setup() {
   Serial.begin(9600);
   pinMode(6,OUTPUT);
   pinMode(redPin,OUTPUT);
   pinMode(greenPin,OUTPUT);
   pinMode(bluePin,OUTPUT);
   pinMode(buttonPin,INPUT_PULLUP);
   s1.attach(10);
}

void loop() {
 /*if(Serial.available()){
  data=Serial.readString();
  d1=data.charAt(0);
  if(d1 == 'f'){
    digitalWrite(13,LOW);

  }
  else if(d1=='n'){
    digitalWrite(13,HIGH);
  }

 }*/
 if(Serial.available()){
  data=Serial.readString();
  d1=data.charAt(0);
  switch (d1){
    case 'n':
    digitalWrite(6,HIGH);
    break;
    case 'f':
    digitalWrite(6,LOW);
    break;
    case 's':
    x=data.substring(1);
    servoval=x.toInt();
    s1.write(servoval);
    delay(100);
    break;
    case 'R':
    x=data.substring(1);
    redValue=x.toInt();
    analogWrite(redPin,redValue);
    break;
    case 'G':
    x=data.substring(1);
    greenValue=x.toInt();
    analogWrite(greenPin,greenValue);
    break;
    case 'B':
    x=data.substring(1);
    blueValue=x.toInt();
    analogWrite(bluePin,blueValue);
    break;
  }
 }
   HandleButton();
   HandlePot();

}
void HandleButton(void){
int reading=digitalRead(buttonPin);
if(reading!=lastButtonState){
  lastDebounceTime= millis();
}
if((millis() - lastDebounceTime) > debounceDelay){
  if(reading!= buttonState){
     buttonState= reading;
     if(buttonState==HIGH){
      Serial.println("p");
     }
  }
  }
  lastButtonState = reading;
  }
  void HandlePot(void){
    String val;
    static int old=0;
    int current=0,upper,lower;
    current= analogRead(10);
    upper=current + 2; 
    lower = current -2 ;
    if(current!=old){
      if((old<=lower)||(old>=upper)){
        val=String(current);
        Serial.println(val);
        old=current;
      }
    }
  }

App Inventor Application

#define ledPin 7
int state = 0;
void setup() {
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,LOW);
  Serial.begin(9600);
}

void loop() {
if(Serial.available()>0){
  state=Serial.read();
}
if(state=='0'){
  digitalWrite(ledPin,LOW);
  Serial.println("LED:OFF");
  state=0;
}
else if(state=='1'){
  digitalWrite(ledPin,HIGH);
  Serial.println("LED:ON");
  state=0;
}
}