Hi everynoe!
I'm fairly new to the Arduino, but I like it very much.
But I have a problem.
I would like to make a program with c# and with an Arduino Uno R3 that sets the light intensity of an external led (not the onboard) through a scrollbar. The scroll bar has a 0 to 255 range.
I searched throughout the Net, and I got the analogWrite() solution. The only problem is, that it doesn't seem to work at all.
Here is my code for the Arduino:
// Led connected to the pin 12
int extled=12;
void setup()
{
pinMode(extled,OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available())
{
byte c =Serial.read();
analogWrite(extled,c);
}
}
And here is my c# code as well:
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 ledfade
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.PortName = "COM4"; //using COM4
serialPort1.BaudRate = 9600; // Setting Baudrate
fadescroller.Maximum = 255; //Maximum value of the scrollbar
fadescroller.Minimum = 1; // Minimum value of the scrollbar
label1.Text = fadescroller.Value.ToString(); // Shows the value of the scrollbar currently on
timer1.Start(); //Starts a timer that I use to send the values continously to the Arduino
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
}
private void btnon_Click(object sender, EventArgs e) //on button
{
serialPort1.Open(); //opens the serial port
btnoff.Enabled = true; //enables the off button
btnon.Enabled = false; // disables the on button
textBox1.Text = "L.E.D is on!"; //led status
}
private void btnoff_Click(object sender, EventArgs e) //off button
{
btnoff.Enabled = false;
btnon.Enabled = true;
serialPort1.Close(); //closes the serial port
textBox1.Text = "L.E.D is off!";
}
private void timer1_Tick(object sender, EventArgs e) //every time the timer ticks it executes the tasks below. Timer interval 0,175 sec.
{
label1.Text = fadescroller.Value.ToString(); //shows the value of the scrollbar in a label. Labels can only show string type.
if (serialPort1.IsOpen)
{
serialPort1.Write(fadescroller.Value.ToString()); //it sends the scrollbars value through the serial port as string(Is this the source of the proplem?)
textBox2.Text = fadescroller.Value.ToString();
}
}
}
}
Oh, I'm using a 280 ohm resistor, but it didn't work without the resistor either. The led is operational, I tested it many times.
I hope you guys can provide me with some useful advices.
Thank you in advance!