0% found this document useful (0 votes)
44 views

UAS Pemrograman - GIOVANI CHRISANTUS - 20F10005

The document contains code for controlling LEDs and servos using an Arduino and C#. The C# code contains two forms, one for controlling LEDs and another for controlling a servo. Form1 controls LEDs by writing "1" or "0" to the serial port. Form2 controls a servo by writing the trackbar value to the serial port. The Arduino code receives values from the serial port to control the LEDs and servo. It blinks the LEDs when it receives "1" and turns them off for "0". It moves the servo to the position of the received value.

Uploaded by

Aldi Aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

UAS Pemrograman - GIOVANI CHRISANTUS - 20F10005

The document contains code for controlling LEDs and servos using an Arduino and C#. The C# code contains two forms, one for controlling LEDs and another for controlling a servo. Form1 controls LEDs by writing "1" or "0" to the serial port. Form2 controls a servo by writing the trackbar value to the serial port. The Arduino code receives values from the serial port to control the LEDs and servo. It blinks the LEDs when it receives "1" and turns them off for "0". It moves the servo to the position of the received value.

Uploaded by

Aldi Aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

SOURCE CODE C#

A. CONTROL LED
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;
using System.IO.Ports;

namespace UAS_FINAL_PROJECT_GIOVANI_CHRISANTUS_A
{
public partial class Form1 : Form
{
SerialPort port = new SerialPort();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
port.PortName = "COM2";
port.BaudRate = 9600;
port.Open();
}

private void btnShowform2_Click(object sender, EventArgs e)


{
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
f2 = null;
this.Show();
}

private void button1_Click(object sender, EventArgs e)


{
port.Write("1");
}

private void button2_Click(object sender, EventArgs e)


{
port.Write("0");
}
}
}

B. CONTROL SERVO
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;
using System.IO.Ports;

namespace UAS_FINAL_PROJECT_GIOVANI_CHRISANTUS_A
{
public partial class Form2 : Form
{
SerialPort port;
public Form2()
{
InitializeComponent();
Init();
}

private void Init()


{
port = new SerialPort();
port.PortName = "COM4";
port.BaudRate = 9600;

try
{
port.Open();
}
catch (Exception el)
{
MessageBox.Show(el.Message);
}
}

private void val_trackBar1_Scroll(object sender, EventArgs e)


{
if (port.IsOpen)
{
port.Write(val_trackBar1.Value.ToString());
Degree_label.Text = "Degree =" + val_trackBar1.Value.ToString();
}
}
}
}

2. SOURCE CODE ARDUINO

A. CONTROL LED
void setup() {
//LED CONTROL
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);

Serial.begin(9600)
}

void loop() {
if(Serial.available()>0);
{
String temp = Serial.readString();
if(temp == "1")
for (int x=1; x<=5; x++)
{
digitalWrite(10, HIGH);
delay(200);
digitalWrite(11, HIGH);
delay(200);
digitalWrite(12, HIGH);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(10, LOW);
delay(100);
digitalWrite(11, LOW);
delay(100);
digitalWrite(12, LOW);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(10, HIGH);
delay(200);
digitalWrite(11, HIGH);
delay(200);
digitalWrite(12, HIGH);
delay(200);
digitalWrite(13, HIGH);
delay(200);
}
else if(temp == "0")
{
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
}

B. CONTROL SERVO
#include <Servo.h>
Servo myservo;
int val;
void setup() {
Serial.begin(9600);
myservo.attach(9);
}

void loop() {

}
void serialEvent(){
val=Serial.parseInt();
if(val!=0){
myservo.write(val);
}
}
3. SCREEN CAPTURE PROGRAM

A. CONTROL LED
B. CONTROL SERVO

You might also like