luni, 31 decembrie 2012
My present project, C# interfacing with Arduino
Bidirectional serial communication between C # and Arduino.
Analog and digital communication, both at the same time.
I built the PCB around ATMEGA8.
I used a 16x2 LCD to check sent / received data.
I/O:
2 digital outputs (2 LEDs),
1 PWM output (1 DC motor),
2 digital inputs (2 switches),
2 analog inputs (1 potentiometer and 1 phototransistor).
Watch video:
C# communicate with Arduino Part.1
We have 3 buttons that help turn on / off 3 LED.
The first push of the green button turns ON green LED, the second push of the green buttonturns OFF green LED. Is the same for the rest of the buttons.
C# code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace SERIAL_DIGITAL_WRITE
{
public partial class Form1 : Form
{
int green=0;
int blue=0;
int red=0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.Open();
if (green==0)
{
serialPort1.Write("1");
green = 1;
}
else if (green == 1)
{
serialPort1.Write("2");
green = 0;
}
serialPort1.Close();
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Open();
if (blue == 0)
{
serialPort1.Write("3");
blue = 1;
}
else if (blue == 1)
{
serialPort1.Write("4");
blue = 0;
}
serialPort1.Close();
}
private void button3_Click(object sender, EventArgs e)
{
serialPort1.Open();
if (red == 0)
{
serialPort1.Write("5");
red = 1;
}
else if (red == 1)
{
serialPort1.Write("6");
red = 0;
}
serialPort1.Close();
}
}
}
Arduino code:
///----------------------------------------///
/// by gabimincu 31.12.2012 ///
/// testing serial digital write ///
/// gabimincu@gmail.com ///
/// www.robot-fun.blogspot.com ///
///----------------------------------------///
int green = 4; // using pin 4 for the GREEN LED
int blue = 3; // using pin 4 for the BLUE LED
int red = 2; // using pin 4 for the RED LED
int i;
int data = 222;
void setup()
{
Serial.begin(9600); // Opens serial port, sets data rate to 9600 bps
pinMode(green, OUTPUT); // sets pin as output for green led
pinMode(blue, OUTPUT); // sets pin as output for blue led
pinMode(red, OUTPUT); // sets pin as output for red led
for (i=0;i<5;i++) // it shows that programs works
{
digitalWrite(green,HIGH);
delay(44);
digitalWrite(blue,HIGH);
delay(44);
digitalWrite(red,HIGH);
delay(44);
digitalWrite(green,LOW);
delay(44);
digitalWrite(blue,LOW);
delay(44);
digitalWrite(red,LOW);
delay(44);
}
}
void loop()
{
if (Serial.available() > 0)
{
data=Serial.read();
switch(data)
{
case '1':
digitalWrite(green, HIGH); // set the GREEN LED on
break;
case '2':
digitalWrite(green,LOW); // set the GREEN LED off
break;
case '3':
digitalWrite(blue, HIGH); // set the BLUE LED on
break;
case '4':
digitalWrite(blue,LOW); // set the BLUE LED off
break;
case '5':
digitalWrite(red, HIGH); // set the RED LED on
break;
case '6':
digitalWrite(red,LOW); // set the RED LED off
break;
}
}
}
Next time I will try to illustrate analog read from C# application.
sâmbătă, 29 decembrie 2012
Abonați-vă la:
Postări (Atom)