const byte Led = 13; // Declaramos la variable pin del Led.
char caracter;
String comando;
void setup()
{
pinMode(Led, OUTPUT); // Inicializa el pin del LED como salida:
Serial.begin(115200); // Puerto serie 115200 baudios.
}
void loop()
{
/*
Voy leyendo carácter a carácter lo que se recibe por el canal serie
(mientras llegue algún dato allí), y los voy concatenando uno tras otro
en una cadena. En la práctica, si usamos el "Serial monitor" el bucle while
acabará cuando pulsamos Enter. El delay es conveniente para no saturar el
canal serie y que la concatenación se haga de forma ordenada.
*/
while (Serial.available() > 0)
{
caracter = Serial.read();
comando.concat(caracter);
delay(10);
}
/*
Una vez ya tengo la cadena "acabada", compruebo su valor y hago que
la placa Arduino reacciones según sea este. Aquí podríamos hacer lo
que quisiéramos: si el comando es "tal", enciende un Led, si es cual,
mueve un motor... y así.
*/
// Si le llega el mensaje Luz_ON.
if (comando.equals("Luz_ON") == true)
{
digitalWrite(Led, HIGH); // Enciende el Led 13.
Serial.write("ON - Led encendido."); // Envía este mensaje a C++.
}
// Si le llega el mensaje Luz_ON.
if (comando.equals("Luz_OFF") == true)
{
digitalWrite(Led, LOW); // Apaga el Led 13.
Serial.write("OFF - Led apagado. "); // Envía este mensaje a C++.
}
// Limpiamos la cadena para volver a recibir el siguiente comando.
comando = "";
}
(******************************************************
* ComPort Library ver. 4.11 *
* for Delphi 5, 6, 7, 2007-2010,XE and *
* C++ Builder 3, 4, 5, 6 *
* written by Dejan Crnila, 1998 - 2002 *
* maintained by Lars B. Dybdahl, 2003 *
* Homepage: http://comport.sf.net/ *
* *
* Brian Gochnauer Oct 2010 *
* Removed ansi references for backward compat *
* Made unicode ready *
*****************************************************)
Puerto : TComPort;
Desconectar:Array[1..8]of char=('1',#255,#255,#255, #255, #255, #255, #0);
Detener :Array[1..8]of char=('1', #0, #0, #0, #0, #0, #0, #0);
Lectura :Array[1..8]of char=('0', #0,#255,#255, #0, #255, #255, #0);
Reset :Array[1..8]of char=('2', #0, #0, #0, #0, #0, #0, #0);
Pinza_0 :Array[1..8]of char=('3', #0, #0, #0, #0, #0, #0, '0');
Pinza_1 :Array[1..8]of char=('3', #0, #0, #0, #0, #0, #0, '1');
Sonido_1 :Array[1..8]of char=('4', #0, #0, #0, #0, #0, #0, '1');
Sonido_2 :Array[1..8]of char=('4', #0, #0, #0, #0, #0, #0, '2');
Sonido_3 :Array[1..8]of char=('4', #0, #0, #0, #0, #0, #0, '3');
procedure TFrmBase.VrPinzaLowerClick(Sender: TObject);
begin
if Puerto.Connected then Puerto.Write(Pinza_0,8)
end;
void loop(){
if(Serial.available() > 7){
// Lectura de Buffer de entrada
Accion= Serial.read();
VelM1 = Serial.read(), DrM1A = Serial.read(), DrM1B = Serial.read();
VelM2 = Serial.read(), DrM2A = Serial.read(), DrM2B = Serial.read();
Aux = Serial.read();
Serial.flush();
switch(Accion){
case '0': // Informar estado de los sensores
Serial.print(digitalRead(SnsIz)); //Interruptores
Serial.print(digitalRead(SnsDr));
/*
Serial.print(analogRead(Anal_5)/4); Serial.print(analogRead(Anal_4)/4); Serial.print(analogRead(Anal_3)/4);
Serial.print(analogRead(Anal_2)/4); Serial.print(analogRead(Anal_1)/4); Serial.print(analogRead(Anal_0)/4);
*/
//Reflexivos
if(analogRead(Anal_5) < 512) Serial.print('0'); else Serial.print('1');
if(analogRead(Anal_4) < 512) Serial.print('0'); else Serial.print('1');
if(analogRead(Anal_3) < 512) Serial.print('0'); else Serial.print('1');
if(analogRead(Anal_2) < 512) Serial.print('0'); else Serial.print('1');
if(analogRead(Anal_1) < 512) Serial.print('0'); else Serial.print('1');
if(analogRead(Anal_0) < 512) Serial.print('0'); else Serial.print('1');
break;
case '1': // Accionar los actuadores
ActualizarMotor(pinEN1, pinA1, pinB1, VelM1, DrM1A, DrM1B);
ActualizarMotor(pinEN2, pinA2, pinB2, VelM2, DrM2A, DrM2B);
break;
case '2': // Reset
setup();
break;
case '3': // Pinza
if(Aux == '1') AccionarPinza(HIGH, LOW);
else AccionarPinza(LOW, HIGH);
delay(150);
AccionarPinza(LOW, LOW);
break;
case '4': // Sonido
switch(Aux){
case '1': // Primera mitad de la melodÃa
Tonos(0, Medio);
break;
case '2': // Segunda mitad de la melodÃa
Tonos(Medio + 1, MAX_COUNT);
break;
case '3': // MelodÃa completa
Tonos(0, MAX_COUNT);
break;
case '4': // Una nota, alarma
Tonos(DrM2B, DrM2B);
break;
}
break;
case '5': // Consulta de estado
Serial.print('.');Serial.print('L');Serial.print('I');Serial.print('S');Serial.print('T');Serial.print('O');Serial.print('!');Serial.print('.');
break;
}
}
}
procedure TFrmBase.VrPinzaLowerClick(Sender: TObject);
begin
if Puerto.Connected then Puerto.Write("Luz_ON"); // Enciende el Led.
end;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports; // No olvidar.
namespace Arduino_Led
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Abrir puerto mientras se ejecuta esta aplicación.
if(!serialPort1.IsOpen)
{
try
{
serialPort1.Open(); // Abrir puerto.
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
// Si pulsas el botón
private void button_Encender_Click(object sender, EventArgs e)
{
// Envía la codificación en ASCII
byte[] mBuffer = Encoding.ASCII.GetBytes("Led_ON");
// el comando "Led_ON".
serialPort1.Write(mBuffer, 0, mBuffer.Length);
}
private void button_Apagar_Click(object sender, EventArgs e)
{
byte[] mBuffer = Encoding.ASCII.GetBytes("Led_OFF");
serialPort1.Write(mBuffer, 0, mBuffer.Length);
}
}
}
if Puerto.Connected then Puerto.Write("Luz_ON");
if Puerto.Connected then Puerto.Write('Luz_ON');
Memo1.Lines.Add('hola');