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

serialArduino

Uploaded by

Kenan Recep
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)
3 views

serialArduino

Uploaded by

Kenan Recep
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/ 13

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

import jssc.SerialPort;

import jssc.SerialPortEvent;

import jssc.SerialPortEventListener;

import jssc.SerialPortException;

import java.awt.BorderLayout;

import java.util.Scanner;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;

import org.jfree.chart.JFreeChart;

import org.jfree.data.xy.XYSeries;

import org.jfree.data.xy.XYSeriesCollection;

import java.util.concurrent.TimeUnit;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Date;

import java.sql.*;
/**

* @author Kenan

*/

public class testFrameSerial extends javax.swing.JFrame {

SerialPort serialport;

/**

* Creates new form testFrameSerial

*/

public testFrameSerial() {

int x=0;

initComponents();

portac();

void portac() {

String userName="root";

String password="12345";

String dbUrl="jdbc:mysql://localhost:3306/testaurdinoserial";

ResultSet resultSet;

Date date = new Date();

System.out.println(date.toString());
jpanel1.setLayout(new BorderLayout());

JPanel topPanel = new JPanel();

jpanel1.add(topPanel, BorderLayout.NORTH);

XYSeries series = new XYSeries("Temperature");

XYSeriesCollection dataset = new XYSeriesCollection(series);

JFreeChart chart = ChartFactory.createXYLineChart("Temperature Reading and Light


Control", "Time (seconds)", "LM35 Reading", dataset);

jpanel1.add(new ChartPanel(chart), BorderLayout.CENTER);

jpanel1.setVisible(true);

try {

serialport=new SerialPort("COM6");

serialport.openPort();

serialport.setParams(SerialPort.BAUDRATE_9600,

SerialPort.DATABITS_8,

SerialPort.STOPBITS_1,

SerialPort.PARITY_NONE);

serialport.addEventListener(new SerialPortEventListener(){

@Override

public void serialEvent(SerialPortEvent event){

if(event.isRXCHAR() && event.getEventValue() > 0){

try{

String data=serialport.readString(event.getEventValue());

Double temp;

temp = ((Double.valueOf(data)*500)/1024);

watchTemp.setText(temp.toString());

Connection connection =null;

PreparedStatement statement = null;


try{

connection = DriverManager.getConnection(dbUrl,userName,password);

String sql = "insert into tum_datalar (aurdino,zaman) values(?,?)";

statement = connection.prepareStatement(sql);

statement.setString(1,temp.toString());

statement.setString(2,date.toString());

//statement.setCharacterStream(1,zaman);

statement.executeUpdate();

System.out.println("kayit edildi" );

catch(SQLException exception){

System.out.println("kayit edilmedi" );

Thread thread;

thread = new Thread(){

@Override public void run() {

int x=0;

while(true) {

try {

series.add(x++,temp);

jpanel1.repaint();

TimeUnit.SECONDS.sleep(1);

} catch(Exception e) {}

};
thread.start();

}catch(SerialPortException ex) {

java.util.logging.Logger.getLogger(testFrameSerial.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

});

} catch (SerialPortException ex) {

java.util.logging.Logger.getLogger(testFrameSerial.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

/**

* This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();


jLabel1 = new javax.swing.JLabel();

watchTemp = new javax.swing.JLabel();

jpanel1 = new java.awt.Panel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("KARASİMSEK LED MODU");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

});

jButton2.setText("ÇAKAR LED MODU");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

});

jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

jLabel1.setText("Hava Sıcaklığı :");

watchTemp.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

watchTemp.setText("---");

javax.swing.GroupLayout jpanel1Layout = new javax.swing.GroupLayout(jpanel1);

jpanel1.setLayout(jpanel1Layout);

jpanel1Layout.setHorizontalGroup(

jpanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 0, Short.MAX_VALUE)
);

jpanel1Layout.setVerticalGroup(

jpanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 495, Short.MAX_VALUE)

);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(jpanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addContainerGap())

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(watchTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 123,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(29, 29, 29)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 239,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 245,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(0, 77, Short.MAX_VALUE))))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jpanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1)

.addComponent(watchTemp, javax.swing.GroupLayout.PREFERRED_SIZE, 44,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 44,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(47, 47, 47))

);

pack();

}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

try {

serialport.writeString("k");

} catch (SerialPortException ex) {

java.util.logging.Logger.getLogger(testFrameSerial.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} }

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

try {

serialport.writeString("c");

} catch (SerialPortException ex) {


java.util.logging.Logger.getLogger(testFrameSerial.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(testFrameSerial.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {


java.util.logging.Logger.getLogger(testFrameSerial.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(testFrameSerial.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(testFrameSerial.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new testFrameSerial().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private javax.swing.JLabel jLabel1;

private java.awt.Panel jpanel1;

private javax.swing.JLabel watchTemp;

// End of variables declaration

}
ARDUİNO İDE KODLARI

int lamb[]= {2,3,4,5,6,7,8,9};

int lm35Pin = A0;

int okunan_deger = 0;

String mode=""; //karaşimşek

void setup()

Serial.begin(9600);

for(int i=0;i<10 ;i++){

pinMode(lamb[i],OUTPUT);

void loop(){

okunan_deger = analogRead(lm35Pin);

Serial.println(okunan_deger);

delay (200);

mode=Serial.readString();

mode.trim();

if(mode.equals("k")){

for(int j=0;j<5;j++){

for(int m=0;m<8;m++){
digitalWrite(lamb[j],HIGH);

delay(40);

digitalWrite(lamb[j],LOW);

for(int l=8;l>0;l--){

digitalWrite(lamb[l],HIGH);

delay(40);

digitalWrite(lamb[l],LOW);

if(mode.equals("c")){

for(int k=0;k<50;k++){

digitalWrite(lamb[0],HIGH);

digitalWrite(lamb[1],HIGH);

digitalWrite(lamb[2],HIGH);

digitalWrite(lamb[3],HIGH);

digitalWrite(lamb[4],HIGH);

digitalWrite(lamb[5],HIGH);

digitalWrite(lamb[6],HIGH);

digitalWrite(lamb[7],HIGH);

digitalWrite(lamb[8],HIGH);

delay(40);{

digitalWrite(lamb[0],LOW);

digitalWrite(lamb[1],LOW);

digitalWrite(lamb[2],LOW);

digitalWrite(lamb[3],LOW);

digitalWrite(lamb[4],LOW);

digitalWrite(lamb[5],LOW);

digitalWrite(lamb[6],LOW);

digitalWrite(lamb[7],LOW);
digitalWrite(lamb[8],LOW);

delay(40);

mode=" ";

You might also like