You are on page 1of 16

ÔN TẬP C#

BÀI 1: ÁP SUẤT

CHƯƠNG TRÌNH:
- ARDUINO:
int temp = 0;
char nut;
int adc;
void truyenlenpc( int x ){
if(x<10)
{
Serial.print("000");
Serial.print(x);
}
if((x>=10)&&(x<100))
{
Serial.print("00");
Serial.print(x);
}
if((x>=100)&&(x<1000))
{
Serial.print("0");
Serial.print(x);
}
if(x>=1000)
{
Serial.print(x);
}
delay(1000);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
nut = Serial.read();
if(nut == '1'){
temp =1;
}
if(nut == '0'){
temp =0;
}
}
if(temp == 1 ){
adc = analogRead(A0);
truyenlenpc(adc);
delay(500);
}
}
- C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string data;
int adc;
public Form1()
{
InitializeComponent();
}

private void label2_Click(object sender, EventArgs e)


{

private void Form1_Load(object sender, EventArgs e)


{
serialPort1.Open();
timer1.Enabled = true;
}

private void serialPort1_DataReceived(object sender,


System.IO.Ports.SerialDataReceivedEventArgs e)
{
data = serialPort1.ReadExisting();
}

private void timer1_Tick(object sender, EventArgs e)


{
Int32.TryParse(data, out adc);
float so = (float)adc * 30 / 1023;
label6.Text = so.ToString();
if (so > 28)
{
panel2.BackColor = Color.Red;
} else
panel2.BackColor = Color.WhiteSmoke;
}

private void button1_Click(object sender, EventArgs e)


{
serialPort1.WriteLine("1");
}

private void button2_Click(object sender, EventArgs e)


{
serialPort1.WriteLine("0");
data = "0";
}
}
}

BÀI 2: NHIỆT ĐỘ
- Arduino:
// Khai bao bien check
int check = 0;
// Khai bao bien adc
int adc;
// Khai bao bien nhan du lieu
char data;
void truyen(int x){
if(x<10){
Serial.print("000");
Serial.print(x);
}
if((x>=10)&&(x<100)){
Serial.print("00");
Serial.print(x);
}
if((x>=100)&&(x<1000)){
Serial.print("0");
Serial.print(x);
}
if(x>=1000){
Serial.print(x);
}
delay(1000);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
data = Serial.read();
if(data=='1'){
check =1;
}
if(data=='0'){
check =0;
}
}
if(check==1){
adc=analogRead(A0);
truyen(adc);
delay(500);
}
}
- C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string data;
int adc;
public Form1()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)


{

private void label2_Click(object sender, EventArgs e)


{

}
// Chay Ham nay dau tien
private void Form1_Load(object sender, EventArgs e)
{
serialPort1.Open();
timer1.Enabled = true;
}
private void label5_Click(object sender, EventArgs e)
{

private void label3_Click(object sender, EventArgs e)


{

private void panel2_Paint(object sender, PaintEventArgs e)


{

}
// Ham Ket Noi
private void button1_Click(object sender, EventArgs e)
{
serialPort1.WriteLine("1");

private void button2_Click(object sender, EventArgs e)


{
serialPort1.WriteLine("0");
data = "0";
}
// Ham doc du lieu
private void serialPort1_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
data = serialPort1.ReadExisting(); // Doc du lieu Uart
}

private void timer1_Tick(object sender, EventArgs e)


{
Int32.TryParse(data, out adc);
float temp = (float)adc*500 / 1024;
label6.Text = temp.ToString();
if (temp > 38)
{
panel2.BackColor = Color.Red;
panel3.BackColor = Color.WhiteSmoke;
}
else
{
panel2.BackColor = Color.WhiteSmoke;
panel3.BackColor = Color.Green;
}

private void label9_Click(object sender, EventArgs e)


{

private void label10_Click(object sender, EventArgs e)


{
}
}
}

BÀI 3: MỨC NƯỚC

- Arduino:
int adc;
int check;
char data;
void truyen(int x){
if(x>10){
Serial.print("000");
Serial.print(x);
}
if((x>=10)&&(x<100)){
Serial.print("00");
Serial.print(x);
}
if((x>=100)&&(x<1000)){
Serial.print("0");
Serial.print(x);
}
if(x>=1000){
Serial.print(x);
}
delay(1000);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0,INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
data = Serial.read();
if(data=='1'){
check =1;
}
if(data=='0'){
check =0;
}
}
if(check ==1){
adc = analogRead(A0);
truyen(adc);
delay(500);
}
}
- C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int adc;
string data;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
serialPort1.Open();
timer1.Enabled = true;
}

private void button1_Click(object sender, EventArgs e)


{
serialPort1.WriteLine("1");
}

private void button2_Click(object sender, EventArgs e)


{
serialPort1.WriteLine("0");
data = "0";
}

private void serialPort1_DataReceived(object sender,


System.IO.Ports.SerialDataReceivedEventArgs e)
{
data = serialPort1.ReadExisting();
}

private void timer1_Tick(object sender, EventArgs e)


{
Int32.TryParse(data, out adc);
float temp = (float)adc*3/1023;
label3.Text = temp.ToString();
if (temp > 2.5)
{
panel1.BackColor = Color.Red;
panel2.BackColor = Color.WhiteSmoke;
}else
{
panel1.BackColor = Color.WhiteSmoke;
panel2.BackColor = Color.Green;
}
}

private void label4_Click(object sender, EventArgs e)


{

}
}
}

BÀI 4: TỐC ĐỘ ĐỘNG CƠ


- Arduino:
int check=0;
char data;
int count=0;
static uint32_t Pre_time;
int tocdo;
void truyen(int x){
if(x<10){
Serial.print("000");
Serial.print(x);
}
if((x>=10)&&(x<100)){
Serial.print("00");
Serial.print(x);
}
if((x>=100)&&(x<1000)){
Serial.print("0");
Serial.print(x);
}
if(x>1000){
Serial.print(x);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2,INPUT_PULLUP);
attachInterrupt(0,COUNT,RISING);
}

void COUNT(){
count++;
}
void loop() {
// put your main code here, to run repeatedly:

if(Serial.available()){
data=Serial.read();
if(data=='1'){
check = 1;
}
if(data=='0'){
check = 0;
}
}
if(check == 1){
if(millis()-Pre_time>=1000)
{
Pre_time = millis();
tocdo = (count*1.01/100)*60;
count = 0;
truyen(tocdo);
}
}
}
- C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string data;
int tocdo;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
serialPort1.Open();
timer1.Enabled = true;
}

private void button1_Click(object sender, EventArgs e)


{
serialPort1.WriteLine("1");
}

private void button2_Click(object sender, EventArgs e)


{
serialPort1.WriteLine("0");
data = "0";

}
private void serialPort1_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
data = serialPort1.ReadExisting();
}

private void timer1_Tick(object sender, EventArgs e)


{
Int32.TryParse(data, out tocdo);
label3.Text = tocdo.ToString();
if (tocdo > 60)
{
panel2.BackColor = Color.Red;
panel3.BackColor = Color.WhiteSmoke;

}
else
{
panel3.BackColor = Color.Green;
panel2.BackColor = Color.WhiteSmoke;
}

}
}
}

You might also like