There are probably a lot of similar questions in this forum however, no matter what I try, I can't make it work. So I decided to create new topic specific to my project.
So, what I want o accomplish is -- using an arduino duemilanove, 5 LED's and 5 phidgets force sensors -- to create a C# program that sends a set of commands for the arduino to light up the LED's in a specific order, which I've already accomplished, and then I want the arduino to send back the following information:
- the time it took from the lighting of the LED to the force in the sensor (the LED shuts of when someone presses the respective sensor);
- the maximum force on the sensor.
My problem is both in the arduino code and the C# code...
Arduino Code:
// First I defined all the variables, set the pinModes, and wrote Serial.Begin(9600);
void loop()
{
if(Serial.available())
{
command = Serial.read();
switch (command)
{
case 'a':
{
digitalWrite(ledPin1, HIGH);
} break; // LED 1
(...)
}
}
// If there's a command from the C# program, the LED turns on. I do this for five different cases. Five commands can come from the C# program (a,b,c,d and e)
sensorValue1 = analogRead(sensorPin1);
if (sensorValue1 > 100)
{
digitalWrite(ledPin1, LOW);
}
(...)
}
// the sensor is pressed the LED turns off. I also do this for every sensor/LED
Now I want to know what's the best way to send information to the C# program. I was thinking of using millis to measure the time difference, and connect each time value to the force value before sending it to the PC.
C# Code:
// This is the code I've tried to use for receiving the data and writing it on a text box. Which, of course hasn't been working. I've tried to change the arduino code to send a simple message to the PC, but I was unsuccessful.
SerialPort serialPort1 = new SerialPort();
public Form1()
{
InitializeComponent();
serialPort1.PortName = "COM4";
serialPort1.BaudRate = 9600;
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.DtrEnable = true;
serialPort1.Open();
}
private static void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
GlobalVars.values = sp.ReadExisting();
Form1 form1 = new Form1();
form1.textBox1.Text += GlobalVars.values;
}
Please Help! This is for an important University project