This is my Arduino code:
int ledPinA = A1;//Setting Pin A1 for the LED
int ledPin = 2;//Setting Pin 2 for the LED
void setup()
{
pinMode(ledPin, OUTPUT);//ledPin as an output
Serial.begin(9600);
}
void loop()
{
if(Serial.available())//checking for serial
{
int LedStatus = Serial.read();//reading from serial ports
if (LedStatus == '1')
{
digitalWrite(ledPin,HIGH);//turn on LED
}
else if (LedStatus == '0')
{
digitalWrite(ledPin,LOW);
int LedIntensity_Scroll = Serial.read();//reading from serial port
analogWrite(ledPinA, LedIntensity_Scroll);//What to use this to change the intensity of the led light.
}
Serial.flush();
}
}
This is the 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;
using System.IO;
namespace ArduinoLedControl
{
public partial class ArduinoLEDControl : Form
{
bool ledState = false;//setting initial led status
public ArduinoLEDControl()
{
InitializeComponent();
}
private void LedOn_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == true)
{
serialPort1.Write("1");//tells arduino that the port is ON
LedStatusBox.Text = "THE LED IS ON";//tells the user that the led is ON
LedOnButton.Enabled = false;//disable the ON button
LedOffButton.Enabled = true;//enable the OFF button
ledState = true;//turn on LED
LedIntensity.Enabled = false;//disabling he led intensity track bar
}
else
{
MessageBox.Show("There is no Serial Ports open.");//message box telling user that there is not ports open
}
}
private void LedOff_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == true)
{
serialPort1.Write("0");//tells arduino that the port is OFF
LedStatusBox.Text = "THE LED IS OFF";//tells the user that the led is OFF
LedOnButton.Enabled = true;//enable the ON button
LedOffButton.Enabled = false;//disable the OFF button
ledState = false;//turn off LED
LedIntensity.Enabled = true;//enabling he led intensity track bar
}
else
{
MessageBox.Show("There is no Serial Ports open.");//message box telling user that there is not ports open
}
}
private void ArduinoLEDControl_Load(object sender, EventArgs e)
{
}
private void LedStatusBox_TextChanged(object sender, EventArgs e)
{
}
private void StartPortButton_Click(object sender, EventArgs e)
{
if (SelectPortBox.SelectedItem == null)
{
MessageBox.Show("No Serial Ports was selected, Try again.");//message box telling user that there is not ports open
}
else
{
serialPort1.PortName = SelectPortBox.SelectedItem.ToString();//setting serialPort1 to a selected port by user
serialPort1.BaudRate = 9600;//this value most be the same as in serial.begin(9600) of arduino's code
if (serialPort1.IsOpen == true)
{
serialPort1.Write("0");
serialPort1.Close();//closing port if previously open
LedStatusBox.Clear();
}
else
{
serialPort1.Open();//opening port when the program starts
}
}
}
private void SelectPortBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void ClosePortButton_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == true)//checking if any port is open in order to close it
{
serialPort1.Write("0");
serialPort1.Close(); //close serialPort1
MessageBox.Show("Serial Port: " + serialPort1.PortName + " have been closed.");//message box telling user that the port is closed
LedStatusBox.Clear();
}
else
{
MessageBox.Show("There is no Serial Ports open.");//message box telling user that there is not ports open
LedStatusBox.Clear();
}
}
private void LedIntensity_Scroll(object sender, EventArgs e)
{
serialPort1.Write(LedIntensity.Value.ToString());//This is where the problem happens
//Console.WriteLine(LedIntensity.Value.ToString());
}
}
}
I can not operate arduino with the line : serialPort1.Write(LedIntensity.Value.ToString()); and receive the command/data at arduino with int LedIntensity_Scroll = Serial.read();
I will really appreciate your help, also, how many ports can i use with the Duemilanove ATmega328P? :-/