leonardo & serial/USB

Hi very good morning, I have a problem to see if I could help, I have a leonardo connected to a potentiometer at the input A0 and want to read that data using Visual Studio with C language The probem is when I type in the serial port you want to read and stops, is because when the leonardo detects that another wants to use COM stops.

Arduino code:

#include

byte v;

void setup()
{
Serial.begin(9600);
while (!Serial);
pinMode(13, OUTPUT);
}

void loop()
{

int rx;

if (Serial.available() > 0)
{
rx = Serial.read();
if (rx == ‘H’) digitalWrite (13, HIGH);
else if (rx == ‘L’) digitalWrite (13, LOW);
}

if (v != analogRead(A0) / 4)
{
v = analogRead(A0) / 4;
Serial.write(v);
}

}

C code (VS):

using System;
using System.IO.Ports;
using System.Threading;

namespace CSharpArduino
{
class Program
{

private static void Encabezado()
{
Console.WriteLine(“————————————–”);
Console.WriteLine(“C# + Arduino usando el System.IO.Ports”);
Console.WriteLine(“————————————–”);
Console.WriteLine(“Guillem Mestre Segui”);
Console.WriteLine(“————————————–”);
}

private static void Pie()
{
Console.WriteLine(“————————————–”);
Console.WriteLine(“COM: ” + puertoArduino);
Console.WriteLine(“LED: ” + LED);
Console.WriteLine(“————————————–”);
}

private static void EsperarParaContinuar()
{

Console.WriteLine(“————————————–”);
Console.WriteLine(“Espere …”);
Console.WriteLine(“————————————–”);

Thread.Sleep(2000);

}

private static void Menu()
{
Encabezado();
Console.WriteLine(“|L| Listar puertos COM disponibles”);
Console.WriteLine(“|S| Setear puerto COM del Arduino”);
Console.WriteLine(“|E| Encender/Apagar LED”);
Console.WriteLine(“|P| Leer POT”);
Console.WriteLine(“|Q| Salir”);
Pie();
}

private static void ListarPuertos()
{
Console.Clear();

Encabezado();

string[] v = SerialPort.GetPortNames();
Array.Sort(v);

foreach (string s in v)
{
Console.WriteLine(s);
}

EsperarParaContinuar();

}

private static byte puertoArduino = 0;

private static void SetearPuertoArduino()
{

Console.Clear();

Encabezado();

Console.WriteLine(“Indicar solo el número de puerto COM”);
Console.WriteLine(“al que se encuentra conectado la placa”);
Console.WriteLine(“————————————–”);
Console.Write(“Número de puerto: “);

try
{

Console.CursorVisible = true;
puertoArduino = byte.Parse(Console.ReadLine());
Console.CursorVisible = false;

if (
Array.Find(
SerialPort.GetPortNames(),
s => s.Equals(“COM” + puertoArduino)
) == null
) throw new Exception();

Console.WriteLine(“Puerto ” + puertoArduino + ” seteado”);

}
catch (Exception)
{
Console.WriteLine(“(!) Puerto incorrecto”);
puertoArduino = 0;
}

EsperarParaContinuar();

}

private static bool LED = false;

private static void EncenderLED()
{

using (SerialPort sp = new SerialPort(“COM” + puertoArduino, 9600))
{

sp.Open();

sp.Write(LED ? “L” : “H”);

sp.Close();

}

LED = !LED;

}

private static void LeerPOT()
{

Console.Clear();
Console.WriteLine(“————————————–”);
Console.WriteLine(“POT: 0?);
Console.WriteLine(“————————————–”);
Console.WriteLine(“Presione una tecla para regresar …”);
Console.WriteLine(“————————————–”);

using (SerialPort sp = new SerialPort(“COM” + puertoArduino, 9600))
{

sp.Open();

while (!Console.KeyAvailable)
{
Console.SetCursorPosition(7, 1);
sp.DiscardInBuffer();
//Thread.Sleep(10000); //pausa de 10 segundos.
Console.WriteLine(sp.ReadByte().ToString().PadLeft(3, ‘ ‘)); //peta en esta linea cuando leemos poten
}

sp.Close();

}

}

static void Main()
{

while (true)
{

Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.CursorVisible = false;

Menu();

char c = Console.ReadKey(true).KeyChar;

switch (char.ToUpper(c))
{

case ‘L’:
ListarPuertos();
break;

case ‘S’:
SetearPuertoArduino();
break;

case ‘E’:
if (puertoArduino != 0) EncenderLED();
break;

case ‘P’:
if (puertoArduino != 0) LeerPOT();
break;

case ‘Q’:
return;

}

}

}
}
}

PS: Also select the com and turn on / off the LED. Works perfectly on arduino UNO to have two mics one for the program and for communication, but in case it does not work ... you'd know leonard help? Thank you very much in advance.

C code (VS):

That is NOT C code. That is NOT C++ code, either. That is C# code.

using (SerialPort sp = new SerialPort(“COM” + puertoArduino, 9600))
{
sp.Open();
sp.Write(LED ? “L” : “H”);
sp.Close();
}

Open the serial port, resetting the Arduino.. Before it has time to react, jam some data down the serial port. Then, close the serial port, resetting the Arduino again. Well, it's no wonder the Arduino doesn't seem to do anything.

Please get your tab key fixed.

The LED goes well, read the potentiometer no.