Monday, July 7, 2014

LED Chase Advanced with Arduino and C#

Purpose : This concept can develop more application for Christmas light , 7 segment board and another advanced project by using LED .

Step :
1. Design The Circuit
I use Resistor 100 ohm for more bright .


2. Build Program
1. Open C# > Make new project > Design
I use : 1 : Picturebox , 2 : label , 3 : Button , 4 : GroupBox , 5 : RadioButton , 6. SerialPort

2. Connect Arduino to PC
Open Device Manager
See at what port .

My port is COM21

3. I make serialport COM21 , and Baudrate 9600
by put in this :

serialPort1.PortName = "COM21";
serialPort1.BaudRate = 9600;

4. Otherwise we open Arduino application and put this :

int message = 0;
int LEDPin1 = 4;int LEDPin2 = 5;int LEDPin3 = 6;int LEDPin4 = 7;int LEDPin5 = 8;int LEDPin6 = 9;int LEDPin7 = 10;int LEDPin8 = 11;int LEDPin9 = 12;int LEDPin10 = 13;
int timer = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LEDPin1, OUTPUT);pinMode(LEDPin2, OUTPUT);pinMode(LEDPin3, OUTPUT);pinMode(LEDPin4, OUTPUT);pinMode(LEDPin5, OUTPUT);pinMode(LEDPin6, OUTPUT);pinMode(LEDPin7, OUTPUT);pinMode(LEDPin8, OUTPUT);pinMode(LEDPin9, OUTPUT);pinMode(LEDPin10, OUTPUT);
}


Logic :
message : it will received data from C#
LEDPin1 = 4, LEDPin2 = 5 : thats Pot Pin on arduino , i use pin 4,5,6,7,8,9. . . 13
Serial.begin(9600) : it means baudrate which is must same with C# if wanna to connect each other.
pinMode () , its means setup output of arduino from the pot pin .


5. Back C# , setting event button1 
by put this in  :
if (button1.Text == "ON") 
{
button1.Text = "OFF";
serialPort1.Open();
if (serialPort1.IsOpen) 
{
serialPort1.WriteLine("A");
}
serialPort1.Close();
}
else if(button1.Text == "OFF")
{
button1.Text = "ON";
serialPort1.Open();
if (serialPort1.IsOpen) 
{
serialPort1.WriteLine("a");
}
serialPort1.Close();
}
6. Setting event button2 : Overall its same coding at c# but what makes different is variable sending message for Arduino
by put this in  :
if (button2.Text == "ON") 
{
button2.Text = "OFF";
serialPort1.Open();
if (serialPort1.IsOpen) 
{
serialPort1.WriteLine("Z");
}
serialPort1.Close();
}
else if(button2.Text == "OFF")
{
button2.Text = "ON";
serialPort1.Open();
if (serialPort1.IsOpen) 
{
serialPort1.WriteLine("z");
}
serialPort1.Close();
}
7. Setting Arduino for Logical Process .
We put this :
if (Serial.available() > 0){
   message = Serial.read();
   if (message == 'A'){
    digitalWrite(LEDPin1,HIGH);
  } 
  if (message == 'a'){
    digitalWrite(LEDPin1,LOW);
  }
  if (message == 'Z'){
   digitalWrite(LEDPin1,HIGH);digitalWrite(LEDPin2,HIGH);digitalWrite(LEDPin3,HIGH);digitalWrite(LEDPin4,HIGH);digitalWrite(LEDPin5,HIGH); 
  }
  if (message == 'z'){
   digitalWrite(LEDPin1,LOW);digitalWrite(LEDPin2,LOW);digitalWrite(LEDPin3,LOW);digitalWrite(LEDPin4,LOW);digitalWrite(LEDPin5,LOW);
  }
  }

Logical Explain :
if serial available then Arduino start doing the process .
In the process there's If , means read message A from C# will turn on LED on pot pin 4, message a will turn off LED on pot pin 4. 
And If message Z , means LED on pot pin 4 until 9 will turn ON . if message z ,means LED on pot pin 4 until 9 will turn OFF. 



For more prove , I make some tutorial on Youtube : http://youtu.be/X85LB6LvgFU

My complete Project :
You can download HERE
My Arduino script :
My video from Complete Project
http://youtu.be/YKTGG7tgucU