You are on page 1of 8

ANALISA PROGRAM VISUAL STUDIO

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 ATKP_2018_VoltageDetector
{
public partial class Form1 : Form
{
String dataFromArduino;
String R1, R2;
String dataHyst;
String first;
int j;

private double k = 0.0;


private int l = 0;
private int xMax;

private int amp = 1;


private int distance = 1;
private int distance2 = 1;
private int mode;
private int plnFreq = 50;

public Form1()
{
InitializeComponent();
getAvailablePorts();
}

void getAvailablePorts()
{
String[] ports = SerialPort.GetPortNames();
portName.Items.AddRange(ports);
chart1.ChartAreas[0].AxisY.Maximum = 25;
chart1.ChartAreas[0].AxisY.Minimum = -25;
//chart1.ChartAreas[0].AxisY.Interval = 11;
chart1.ChartAreas[0].AxisX.ScaleView.Size = 50;
chart1.ChartAreas[0].CursorX.AutoScroll = true;
//chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = false;

private void buttonConnect_Click(object sender, EventArgs e)


{
try
{
if (portName.Text == "")
{
MessageBox.Show("Please Insert port Settings");
}
else
{
//serialPort1 = new
SerialPort(portName.Text,9600,Parity.None,9,StopBits.One);
serialPort1 = new SerialPort();
serialPort1.PortName = portName.Text;
serialPort1.BaudRate = 9600;

serialPort1.DataReceived = terima;
serialPort1.Open();
buttonConnect.Enabled = false;
buttonDisconnect.Enabled = true;
timer1.Enabled = true;
timer2.Enabled = true;
timer3.Enabled = true;
//timer4.Enabled = true;
}
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("Unauthorized Access");
}
}

// Gate to received All in data from serialport


void terima(Object sender, SerialDataReceivedEventArgs e)
{
String data = serialPort1.ReadLine();//.ToString();
dataFromArduino = data;

first = dataFromArduino.Substring(0, 1);

if (first == " ")


{
dataHyst = dataFromArduino;

String[] dataSharp = dataHyst.Split('|');


R1 = (dataSharp[1]);
R2 = (dataSharp[2]);

int num1, num2;


bool res1
bool res2
if (res1 == true)
{
// String is not a number.
if (R1 != "") amp = int.Parse(dataSharp[1]);
else amp = 30;
}
if (res2 == true)
{
// String is not a number.
if (R2 != "") distance = int.Parse(dataSharp[2]);
else distance = 20;
}
}

else
{

}
data = "";
j++;
}

private void timer1_Tick(object sender, EventArgs e)


{

private void timer2_Tick(object sender, EventArgs e)


{
//amplitudeBox.Text = amp.ToString();
distanceBox.Text = distance.ToString() + " cm";
}

private void timer3_Tick(object sender, EventArgs e)


{
//checkGraphSize();
drawSinus();

if (plnFreq <= 5)
{
k = k + 0.1;
timer3.Interval = 50;
chart1.ChartAreas[0].AxisX.ScaleView.Size = 100;
chart1.ChartAreas[0].AxisX.ScaleView.Position = l++ - 80;
}

else if (plnFreq > 5 && plnFreq <= 15)


{
k = k + 2;
timer3.Interval = 1;
chart1.ChartAreas[0].AxisX.ScaleView.Size = 350;
chart1.ChartAreas[0].AxisX.ScaleView.Position = l++ - 250;
}
else
{
k = k + 0.1;
timer3.Interval = 1;
chart1.ChartAreas[0].AxisX.ScaleView.Size = 500;
chart1.ChartAreas[0].AxisX.ScaleView.Position = l++ - 480;
}

void drawSinus()
{
int xMax2;
double y;

if (amp >= 20 && plnFreq >= 5)


{
xMax = 1000 / 100;
xMax2 = xMax / 2;
y = Math.Sin(k * Math.PI / xMax2) * amp;
mode = 1;
}
else if (amp >= 10 && amp < 20 && plnFreq >= 5)
{
xMax = 1000 / 130;
xMax2 = xMax / 2;
y = Math.Sin(k * Math.PI / xMax2) * amp;
mode = 1;
}
else if (amp > 0 && amp < 10 && plnFreq >= 5)
{
xMax = 1000 / 170;
xMax2 = xMax / 2;
y = Math.Sin(k * Math.PI / xMax2) * amp;
mode = 1;
}
else
{
y = 0;
mode = 0;
}
this.chart1.Series["Amplitude"].Points.AddXY((int)k, y);
this.chart1.Series["Center Line"].Points.AddXY((int)k, 0);
}

void checkGraphSize()
{

if (amp < 10)


{
//chart2.Series[0].Points.Clear();
chart1.ChartAreas[0].AxisY.Maximum = 10;
chart1.ChartAreas[0].AxisY.Minimum = -10;
}
else if (amp < 20)
{
//chart2.Series[0].Points.Clear();
chart1.ChartAreas[0].AxisY.Maximum = 20;
chart1.ChartAreas[0].AxisY.Minimum = -20;
}
else if (amp < 30)
{
chart1.ChartAreas[0].AxisY.Maximum = 30;
chart1.ChartAreas[0].AxisY.Minimum = -30;
}
}

private void buttonDisconnect_Click(object sender, EventArgs e)


{
buttonConnect.Enabled = true;
buttonDisconnect.Enabled = false;
timer1.Enabled = false;
timer2.Enabled = false;
timer3.Enabled = false;
serialPort1.Close();
}
}
}

ANALISA PROGRAM ARDUINO


#define sample 1000
float val;
int pin3 = 3;

int average = 5;
int amplitude = 0;
int amplitude2 = 0;
int distance_;
String mode_= "1";
String mode1_;
bool exist;
unsigned long timeser;
bool bell,switc;

////////////////////////// for timer2 ////////////////////////////


int data = 0;
unsigned int toggle = 0; //used to keep the state of the LED
unsigned int count = 0; //used to keep count of how many interrupts were fired

//Timer2 Overflow Interrupt Vector, called every 1ms


ISR(TIMER2_OVF_vect) {
if(Serial.available()>0)
{
char a = Serial.read();
if(a == '\n')
{
mode_ = mode1_;
mode1_ = "";
}
else
{
mode1_ += (String)a;
}
}
if(mode_ == "1") digitalWrite(13,HIGH);
else if(mode_ == "2") digitalWrite(13,LOW);

TCNT2 = 130; //Reset Timer to 130 out of 255


TIFR2 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
};

void setup() {
// Print a message to the LCD
////////////////////////////////////////// for timer2 ////////////////////////////
TIMSK2 = 0x00; //Timer2 INT Reg: Timer2 Overflow Interrupt Enable
TCCR2A = 0x00; //Timer2 Control Reg A: Normal port operation, Wave Gen Mode normal
//Setup Timer2 to fire every 1ms
TCCR2B = 0x00; //Disbale Timer2 while we set it up
TCNT2 = 130; //Reset Timer Count to 130 out of 255
TIFR2 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
TIMSK2 = 0x00; //Timer2 INT Reg: Timer2 Overflow Interrupt Enable
TCCR2A = 0x00; //Timer2 Control Reg A: Wave Gen Mode normal
TCCR2B = 0x05; //Timer2 Control Reg B: Timer Prescaler set to 128
/////////////////////////////////////////////////////////////////////////////////////////
//

Serial.begin(9600);
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
pinMode(7,OUTPUT);
digitalWrite(7,HIGH);
pinMode(6,OUTPUT);
digitalWrite(6,LOW);

void readDistance()
{
long average = 0;
float c, s;
while (1) {
int16_t adc0;
long sum, t, top;
double amplitude, cp, sp, phi;
int i;
sum = 0;
i = 0;
c = 0;
s = 0;
while (i < sample) {
adc0 = analogRead(A1) - 1023 * 3 / 5;
t = micros();
phi = 6.2831853 * t / 1.0e6 * 50.0; //Replace 50.0 by 60.0 if your mains is 60 Hz
c += (adc0 - average) * cos(phi);
s += (adc0 - average) * sin(phi);
sum += adc0;
i++;
}
average = (sum / sample) ;
i = 0; cp = c / sample; sp = s / sample;
amplitude = 2 * sqrt(cp * cp + sp * sp);

amplitude = amplitude*100;
if(amplitude <=80)
{
distance_ = map(amplitude, 40, 0, 0, 20);
amplitude2 = map(amplitude, 40, 0, 20, 0);
}
// Value Safety
if(distance_ <= 0) distance_ = 0;
if(amplitude2 >= 20) amplitude2 = 20;

// Alarm check
if(amplitude2 > 2 && amplitude2 <= 20) alarmON();
else{
digitalWrite(7,HIGH);
digitalWrite(6,LOW);
}

/* // Mode check
if(mode_ == "1"){
distance_ = distance_;
amplitude2 = amplitude2;
}
else if(mode_ == "2"){
distance_ = distance_ + 2;
amplitude2 = amplitude2 + 2;
}
*/
//Serial.println(amplitude); //For test and scaling
Serial.println("@|" + String(amplitude2) + "|" + String(distance_) + "|");
Serial.println(String(amplitude));
delay(500);
//val = amplitude; //here the program takes the sum of all numbers
in array1, and divides by the number of elements "sample"
//val = constrain(val, 0, 20); //tweak it from experience, 10 works for me
//analogWrite(pin3, (val / 20.0 * 255.0));
}
}

void alarmON()
{
// if(millis() > timeser+250)
// {
// if(bell == true)
// {
if(switc == false)
{
digitalWrite(7,LOW);
digitalWrite(6,HIGH);
}
else if(switc == true)
{
digitalWrite(7,HIGH);
digitalWrite(6,LOW);
}
switc = !switc;
// }
// timeser = millis();
// }
}

void loop(void)
{
readDistance();
}

You might also like