Skip to main content
Copy edited. Removed meta information (this belongs in comments).
Source Link

I don't know whether I should ask here or on C# but anyway. My problem is quite simple but I can't figure out a solution.

I would like to establish a communication between my C# program and my Arduino Duemilanove. I tried lot of different tutorials, but none of them were the same, and I'm a little bit confused.

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

namespace InterfaceArduinoWindowsFOrm {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      serialPort1.PortName = "COM3";
      serialPort1.BaudRate = 9600;
      serialPort1.Open();
    }

    private void pictureBox1_Click(object sender, EventArgs e) {
    }

    private void tabPage2_Click(object sender, EventArgs e) {
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
    }

    private void toolTip1_Popup(object sender, PopupEventArgs e) {
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
      Console.WriteLine("test");
    }

    private void button1_Click_1(object sender, EventArgs e) {
      serialPort1.Write("5");
      Console.WriteLine(serialPort1.IsOpen);
    }
  }
}
/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.
  modified 8 Nov 2013
  by Scott Fitzgerald
  http://arduino.cc/en/Tutorial/Sweep
*/ 

#include <Servo.h> 

Servo myservo;  // createCreate servo object to control a servo.
// twelveTwelve servo objects can be created on most boards.
int pos = 0;
int message = 0; // This will hold one byte of the serial message
                 // variable to store the servo position.

void setup() {
  Serial.begin(9600);
  myservo.attach(9);  // attachesAttaches the servo on pin 9 to the servo object
}

void loop() {
  if (Serial.available()) { 

    // Check to see if there is a new message
    message = Serial.read(); 

    // Put the serial input into the message
    Serial.print(message); 

    if (message == '5') {
      Serial.print("test");
      for(pos = 0; pos <= 180; pos += 1) {
        // goesGoes from 0 degrees to 180 degrees
        // in steps of 1 degree 

        myservo.write(pos);
        // tellTell servo to go to position in variable 'pos' 

        delay(15);
        // waitsWaits 15ms15 ms for the servo to reach the position
      } 

      for(pos = 180; pos>=0; pos-=1) {
        // goesGoes from 180 degrees to 0 degrees 

        myservo.write(pos);
        // tellTell servo to go to position in variable 'pos' 

        delay(15);
        // waitsWaits 15ms15 ms for the servo to reach the position
      }
    }
  }
}

The problem is, when I click my button, nothing happens, but obviously if iI remove the ifif statement, the servo motor code works.

I really hope you can help me because it's very important (it's for a school project).

I don't know whether I should ask here or on C# but anyway. My problem is quite simple but I can't figure out a solution.

I would like to establish a communication between my C# program and my Arduino Duemilanove. I tried lot of different tutorials, but none of them were the same and I'm a little bit confused.

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

namespace InterfaceArduinoWindowsFOrm {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      serialPort1.PortName = "COM3";
      serialPort1.BaudRate = 9600;
      serialPort1.Open();
    }

    private void pictureBox1_Click(object sender, EventArgs e) {
    }

    private void tabPage2_Click(object sender, EventArgs e) {
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
    }

    private void toolTip1_Popup(object sender, PopupEventArgs e) {
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
      Console.WriteLine("test");
    }

    private void button1_Click_1(object sender, EventArgs e) {
      serialPort1.Write("5");
      Console.WriteLine(serialPort1.IsOpen);
    }
}
}
/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.
  modified 8 Nov 2013
  by Scott Fitzgerald
  http://arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0;
int message = 0; // This will hold one byte of the serial message// variable to store the servo position

void setup() {
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  if (Serial.available()) {
    // Check to see if there is a new message
    message = Serial.read();
    // Put the serial input into the message
    Serial.print(message);
    if (message == '5') {
      Serial.print("test");
      for(pos = 0; pos <= 180; pos += 1) {
        // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
        myservo.write(pos);
        // tell servo to go to position in variable 'pos'
        delay(15);
        // waits 15ms for the servo to reach the position
      }
      for(pos = 180; pos>=0; pos-=1) {
        // goes from 180 degrees to 0 degrees
        myservo.write(pos);
        // tell servo to go to position in variable 'pos'
        delay(15);
        // waits 15ms for the servo to reach the position
      }
    }
  }
}

The problem is, when I click my button, nothing happens, but obviously if i remove the if statement, the servo motor code works.

I really hope you can help me because it's very important (it's for a school project).

I would like to establish a communication between my C# program and my Arduino Duemilanove. I tried lot of different tutorials, but none of them were the same, and I'm a little bit confused.

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

namespace InterfaceArduinoWindowsFOrm {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      serialPort1.PortName = "COM3";
      serialPort1.BaudRate = 9600;
      serialPort1.Open();
    }

    private void pictureBox1_Click(object sender, EventArgs e) {
    }

    private void tabPage2_Click(object sender, EventArgs e) {
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
    }

    private void toolTip1_Popup(object sender, PopupEventArgs e) {
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
      Console.WriteLine("test");
    }

    private void button1_Click_1(object sender, EventArgs e) {
      serialPort1.Write("5");
      Console.WriteLine(serialPort1.IsOpen);
    }
  }
}
/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.
  modified 8 Nov 2013
  by Scott Fitzgerald
  http://arduino.cc/en/Tutorial/Sweep
*/ 

#include <Servo.h> 

Servo myservo;  // Create servo object to control a servo.
// Twelve servo objects can be created on most boards.
int pos = 0;
int message = 0; // This will hold one byte of the serial message
                 // variable to store the servo position.

void setup() {
  Serial.begin(9600);
  myservo.attach(9);  // Attaches the servo on pin 9 to the servo object
}

void loop() {
  if (Serial.available()) { 

    // Check to see if there is a new message
    message = Serial.read(); 

    // Put the serial input into the message
    Serial.print(message); 

    if (message == '5') {
      Serial.print("test");
      for(pos = 0; pos <= 180; pos += 1) {
        // Goes from 0 degrees to 180 degrees
        // in steps of 1 degree 

        myservo.write(pos);
        // Tell servo to go to position in variable 'pos' 

        delay(15);
        // Waits 15 ms for the servo to reach the position
      } 

      for(pos = 180; pos>=0; pos-=1) {
        // Goes from 180 degrees to 0 degrees 

        myservo.write(pos);
        // Tell servo to go to position in variable 'pos' 

        delay(15);
        // Waits 15 ms for the servo to reach the position
      }
    }
  }
}

The problem is, when I click my button, nothing happens, but obviously if I remove the if statement, the servo motor code works.

added 4 characters in body
Source Link
jfpoilpret
  • 9.2k
  • 7
  • 38
  • 54
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InterfaceArduinoWindowsFOrm {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      serialPort1.PortName = "COM3";
      serialPort1.BaudRate = 9600;
      serialPort1.Open();
    }

    private void pictureBox1_Click(object sender, EventArgs e) {
    }

    private void tabPage2_Click(object sender, EventArgs e) {
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
    }

    private void toolTip1_Popup(object sender, PopupEventArgs e) {
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
      Console.WriteLine("test");
    }

    private void button1_Click_1(object sender, EventArgs e) {
      serialPort1.Write("5");
      Console.WriteLine(serialPort1.IsOpen);
    }
}
}

}

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

namespace InterfaceArduinoWindowsFOrm {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      serialPort1.PortName = "COM3";
      serialPort1.BaudRate = 9600;
      serialPort1.Open();
    }

    private void pictureBox1_Click(object sender, EventArgs e) {
    }

    private void tabPage2_Click(object sender, EventArgs e) {
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
    }

    private void toolTip1_Popup(object sender, PopupEventArgs e) {
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
      Console.WriteLine("test");
    }

    private void button1_Click_1(object sender, EventArgs e) {
      serialPort1.Write("5");
      Console.WriteLine(serialPort1.IsOpen);
    }
}

}

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

namespace InterfaceArduinoWindowsFOrm {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      serialPort1.PortName = "COM3";
      serialPort1.BaudRate = 9600;
      serialPort1.Open();
    }

    private void pictureBox1_Click(object sender, EventArgs e) {
    }

    private void tabPage2_Click(object sender, EventArgs e) {
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
    }

    private void toolTip1_Popup(object sender, PopupEventArgs e) {
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
      Console.WriteLine("test");
    }

    private void button1_Click_1(object sender, EventArgs e) {
      serialPort1.Write("5");
      Console.WriteLine(serialPort1.IsOpen);
    }
}
}

C# and arduinoArduino problem with serial port

I dontdon't know ifwhether I should ask here or on C# but anyway,

. My problem is quite simple but cantI can't figure out a solution out...

I would trylike to really simply establish a communication between my C# programmprogram and my Arduino Duemilanove:

i try. I tried lot of different tutorialtutorials, but eachnone of them weren'twere the same and i'mI'm a little bit confused...

acctually My CodeActually my code is that :

using System;

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

namespace InterfaceArduinoWindowsFOrm { public partial class Form1 : Form { public Form1() { InitializeComponent(); serialPort1.PortName = "COM3"; serialPort1.BaudRate = 9600; serialPort1.Open();

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

namespace InterfaceArduinoWindowsFOrm {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      serialPort1.PortName = "COM3";
      serialPort1.BaudRate = 9600;
      serialPort1.Open();
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
 
    }
 

    private void tabPage2_Click(object sender, EventArgs e)
    {
 
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
 
    }

    private void toolTip1_Popup(object sender, PopupEventArgs e)
    {
 
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        Console.WriteLine("test");
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
 
        serialPort1.Write("5");
        Console.WriteLine(serialPort1.IsOpen);
    }
}
/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.
  modified 8 Nov 2013
  by Scott Fitzgerald
  http://arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0;
int message = 0; // This will hold one byte of the serial message// variable to store the servo position

void setup() {
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  if (Serial.available()) {
    // Check to see if there is a new message
    message = Serial.read();
    // Put the serial input into the message
    Serial.print(message);
    if (message == '5') {
      Serial.print("test");
      for(pos = 0; pos <= 180; pos += 1) {
        // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
        myservo.write(pos);
        // tell servo to go to position in variable 'pos'
        delay(15);
        // waits 15ms for the servo to reach the position
      }
      for(pos = 180; pos>=0; pos-=1) {
        // goes from 180 degrees to 0 degrees
        myservo.write(pos);
        // tell servo to go to position in variable 'pos'
        delay(15);
        // waits 15ms for the servo to reach the position
      }
    }
  }
}

by BARRAGAN http://barraganstudio.com This example code is in the public domain.

modified 8 Nov 2013 by Scott Fitzgerald http://arduino.cc/en/Tutorial/Sweep */

#include <Servo.h>

Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards

int pos = 0; int message = 0; // This will hold one byte of the serial message// variable to store the servo position

void setup() { Serial.begin(9600); myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop() { if (Serial.available()) { // Check to see if there is a new message message = Serial.read();// Put the serial input into the message Serial.print(message); if (message == '5'){

Serial.print("test");

for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = 180; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees {
myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } } }

theThe problem is i try to, when I click my button but, nothing happenhappens, but obviously if i remove the if statmentstatement, the servo motor code work..works.

I really hope youryou can help me because it's very important ( Itsit's for a Schoolschool project  ).

C# and arduino problem with serial port

I dont know if I should ask here or on C# but anyway,

My problem is quite simple but cant figure a solution out...

I would try to really simply establish a communication between my C# programm and my Arduino Duemilanove:

i try lot of different tutorial but each of them weren't the same and i'm a little bit confused...

acctually My Code is that :

using System;

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

namespace InterfaceArduinoWindowsFOrm { public partial class Form1 : Form { public Form1() { InitializeComponent(); serialPort1.PortName = "COM3"; serialPort1.BaudRate = 9600; serialPort1.Open();

    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
 
    }
 

    private void tabPage2_Click(object sender, EventArgs e)
    {
 
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
 
    }

    private void toolTip1_Popup(object sender, PopupEventArgs e)
    {
 
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        Console.WriteLine("test");
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
 
        serialPort1.Write("5");
        Console.WriteLine(serialPort1.IsOpen);
    }
}
/* Sweep

by BARRAGAN http://barraganstudio.com This example code is in the public domain.

modified 8 Nov 2013 by Scott Fitzgerald http://arduino.cc/en/Tutorial/Sweep */

#include <Servo.h>

Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards

int pos = 0; int message = 0; // This will hold one byte of the serial message// variable to store the servo position

void setup() { Serial.begin(9600); myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop() { if (Serial.available()) { // Check to see if there is a new message message = Serial.read();// Put the serial input into the message Serial.print(message); if (message == '5'){

Serial.print("test");

for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = 180; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees {
myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } } }

the problem is i try to click my button but nothing happen, but obviously if i remove the if statment, the servo motor code work...

I really hope your can help me because it's very important ( Its for a School project  )

C# and Arduino problem with serial port

I don't know whether I should ask here or on C# but anyway. My problem is quite simple but I can't figure out a solution.

I would like to establish a communication between my C# program and my Arduino Duemilanove. I tried lot of different tutorials, but none of them were the same and I'm a little bit confused.

Actually my code is:

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

namespace InterfaceArduinoWindowsFOrm {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      serialPort1.PortName = "COM3";
      serialPort1.BaudRate = 9600;
      serialPort1.Open();
    }

    private void pictureBox1_Click(object sender, EventArgs e) {
    }

    private void tabPage2_Click(object sender, EventArgs e) {
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
    }

    private void toolTip1_Popup(object sender, PopupEventArgs e) {
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
      Console.WriteLine("test");
    }

    private void button1_Click_1(object sender, EventArgs e) {
      serialPort1.Write("5");
      Console.WriteLine(serialPort1.IsOpen);
    }
}
/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.
  modified 8 Nov 2013
  by Scott Fitzgerald
  http://arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0;
int message = 0; // This will hold one byte of the serial message// variable to store the servo position

void setup() {
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  if (Serial.available()) {
    // Check to see if there is a new message
    message = Serial.read();
    // Put the serial input into the message
    Serial.print(message);
    if (message == '5') {
      Serial.print("test");
      for(pos = 0; pos <= 180; pos += 1) {
        // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
        myservo.write(pos);
        // tell servo to go to position in variable 'pos'
        delay(15);
        // waits 15ms for the servo to reach the position
      }
      for(pos = 180; pos>=0; pos-=1) {
        // goes from 180 degrees to 0 degrees
        myservo.write(pos);
        // tell servo to go to position in variable 'pos'
        delay(15);
        // waits 15ms for the servo to reach the position
      }
    }
  }
}

The problem is, when I click my button, nothing happens, but obviously if i remove the if statement, the servo motor code works.

I really hope you can help me because it's very important (it's for a school project).

Source Link
Loading