bonjour,
j'essaye d'envoyer une temperature venant du sonde que j'ai branché sur ma carte arduino uno. cela fonctionne mais seulement une fois ensuite la valeur est érroné. Auriez vous une idée ? voici mes codes:
Arduino:
int tempPin = 5; // analog input
int ledState = LOW;
int ledPin = 8;
void setup(void)
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop(void)
{
int tempReading;
float temp_mV, tempC, tempF;
tempReading = analogRead(tempPin); // returns 0-1023
temp_mV = map(tempReading, 0, 1023, 0, 5000); // convert to milliVolts
tempC = (temp_mV - 500)/10 ;
tempF = 9*tempC/5 + 32;
Serial.println(tempC);
delay(2000);
int received; // Variable servant à récupérer
// les données reçues
if (Serial.available()>0) { // Si des données sont disponibles
received = Serial.read(); // On les récupère
if(received == 'a') { // Si "a" est reçu
if (ledState==HIGH) // Et que la LED est allumée
ledState = LOW; // On l'éteinds
else // Sinon
ledState = HIGH; // On l'allume
digitalWrite(ledPin, ledState); // Enfin on change l'état de la LED
}
}
}
C#
using System;
using System.IO.Ports;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Timer bg = new Timer();
public Form1()
{
InitializeComponent();
serialPort1.PortName = "COM7";
serialPort1.BaudRate = 9600;
bg.Tick += (s, e) => { label3.Text = DateTime.Now.ToString(); };
bg.Interval = 200;
bg.Start();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
serialPort1.Open();
if (serialPort1.IsOpen)
{
progressBar1.Value = 100;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Close();
progressBar1.Value = 0;
}
private void button3_Click(object sender, EventArgs e)
{
if(progressBar1.Value == 100)
serialPort1.Write ("a");
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (progressBar1.Value == 100)
{
string recive = serialPort1.ReadExisting();
SetRecive(recive);
}
}
private delegate void SetReciveCallback(string recive);
private void SetRecive(string recive)
{
if (textBox1.InvokeRequired)
{
SetReciveCallback d = new SetReciveCallback(SetRecive);
textBox1.Invoke(d, new object[] { recive });
}
else
textBox1.Text = recive;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
}
}
}