Professional Documents
Culture Documents
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;
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;
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");
}
}
else
{
}
data = "";
j++;
}
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;
}
void drawSinus()
{
int xMax2;
double y;
void checkGraphSize()
{
int average = 5;
int amplitude = 0;
int amplitude2 = 0;
int distance_;
String mode_= "1";
String mode1_;
bool exist;
unsigned long timeser;
bool bell,switc;
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();
}