Sie sind auf Seite 1von 9

using

using
using
using
using
using
using
using
using

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

namespace arduinoproyec
{
public partial class Form1 : Form
{
string RxString, velocidad="0",velocidad2="0", direccion;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
try
{
serialPort1 = new SerialPort();
serialPort1.BaudRate = 9600;
serialPort1.PortName = "COM3";
serialPort1.Parity = Parity.None;
serialPort1.DataBits = 8;
serialPort1.StopBits = StopBits.One;
serialPort1.DataReceived += serialPort1_DataReceived;
serialPort1.Open();
textBox1.Text = "";
buttonStart.Enabled = false;
buttonStop.Enabled = true;
textBox1.ReadOnly = false;
btnderecha.Enabled = true;
btnizquierda.Enabled = true;
btnparo.Enabled = true;
buttonEnviar.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled =true;
label1.Enabled = true;
label2.Enabled = true;
}
catch (Exception exepcion)
{
MessageBox.Show(exepcion.Message, "Error");
}
}
}

private void buttonStop_Click(object sender, EventArgs e)


{
if (serialPort1.IsOpen)
{
try
{
serialPort1.Close();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
textBox1.ReadOnly = true;
btnderecha.Enabled = false;
btnizquierda.Enabled = false;
btnparo.Enabled = false;
buttonEnviar.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
label1.Enabled = false;
label2.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// If the port is closed, don't try to send a character.
if (!serialPort1.IsOpen) return;
// If the port is Open, declare a char[] array with one element.
char[] buff = new char[1];
// Load element 0 with the key character.
buff[0] = e.KeyChar;
// Send the one character buffer.
serialPort1.Write(buff, 0, 1);

// Set the KeyPress event as handled so the character won't


// display locally. If you want it to display, omit the next line.
e.Handled = true;

private void serialPort1_DataReceived(object sender,


System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));

}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
velocidad = 'X' + textBox2.Text +'Y';
}

private void g(object sender, EventArgs e)


{
}
private void btnderecha_Click_1(object sender, EventArgs e)
{
direccion = "D";
if ((serialPort1 != null) && (serialPort1.IsOpen == true))
serialPort1.Write(direccion);
}
private void btnparo_Click_1(object sender, EventArgs e)
{
direccion = "P";
if ((serialPort1 != null) && (serialPort1.IsOpen == true))
serialPort1.Write(direccion);
}
private void btnizquierda_Click_1(object sender, EventArgs e)
{
direccion ="I";
if ((serialPort1 != null) && (serialPort1.IsOpen == true))
serialPort1.Write(direccion);
}
private void buttonEnviar_Click_1(object sender, EventArgs e)
{
if ((serialPort1 != null) && (serialPort1.IsOpen == true))
serialPort1.Write(velocidad+velocidad2);
}
private void Form1_Load_1(object sender, EventArgs e)
{

}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
velocidad2 = textBox3.Text + 'N';
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}

Botn Start
Name buttonstart
Enabled True
En el icono de eventos Click
Botn Stop
Name buttonStop
Enabled False
En el icono de eventos Click
Botn Derecha
Name btnderecha

Enabled False
En el icono de eventos Click
Botn detener
Name btnparo
Enabled False
En el icono de eventos Click
Botn Izquierda
Name btnizquierda
Enabled False
En el icono de eventos Click
Botn Enviar
Name buttonEnviar
Enabled False
En el icono de eventos Click
serialPort1
doble clic en el icono de eventos en Data Received
Cuadro Form1
Icono ce eventos
Doble click en Form Closing
Doble click en Load

Name Textbox1
Enabled false
Read only true
Multiline true
Scrollbars Vertical
En iconod e eventos doble click en:
Key press
Textchange

Name textBox2
Enabled false
Icono de eventos doble click en:
Textchanged

Name textBox3
Enabled false
Icono de eventos doble click en:
Textchanged

Necesitas agregar la librera Timer descargala de la pagina de arduino una ves


descomprimido el archivo mete la carpeta Timmer en ARduino/libreras busca la carpeta
donde se haya guardado arduino en tu disco C y ah busca la carpeta donde estn las
leibrerias ah pega Timer. Tambin descarga la librera PID y has lo mismo.
Codigo en arduino
#include<Timer.h>
int LeftPin=8; //Pines de salida del Arduino
int RightPin=9;
char input=0;
int
pulso=0,revmandar=0,contador=0,valorx=0,valory=0,valorn=0,posc=0,poscar=0,longicad
=0;
Timer tiempo;
String velrpm="",posicion="",cadena_recibida="";
double rpm;
void setup(){
Serial.begin(9600);
pinMode(LeftPin, OUTPUT);
pinMode(RightPin, OUTPUT);
attachInterrupt(0,funcion_contadora,RISING);
tiempo.every(1000,imppulso);
}
void loop(){
tiempo.update();
if (Serial.available()>0){
input=Serial.read();
//Serial.println(input);
switch(input){
case 'D': //El motor girar a la derecha
digitalWrite(LeftPin, LOW);
analogWrite(RightPin, revmandar);
break;
case'I': //El motor girar a la izquierda
analogWrite(LeftPin, revmandar);
digitalWrite(RightPin, LOW);
break;
case'P': //El motor se detendr
digitalWrite(LeftPin, LOW);
digitalWrite(RightPin, LOW);
break;
}
if((input!='D') && (input!='I') && (input!='P') && (input!='X') && (input!='N')){
cadena_recibida+=input;
}

if(input== 'X'){
velrpm="";
cadena_recibida="";
}
if(input== 'Y')
posicion="";
if(input== 'N'){
poscar=cadena_recibida.indexOf('Y');
for(int i=0; i<longicad;i++){
if(i<poscar)
velrpm+=cadena_recibida[i];
if(i>poscar)
posicion+=cadena_recibida[i];
}
rpm= velrpm.toInt();
posc=posicion.toInt();
}
Serial.println(rpm);
Serial.println(posc);

}
revmandar=rpm*255/977;
//Serial.println(cadena_recibida);
longicad=cadena_recibida.length();

void funcion_contadora(){
pulso++;
}
void imppulso(){

pulso=0;
}

Das könnte Ihnen auch gefallen