Hi everyone, having some trouble understanding/figuring out what to do here. If it helps to know, I'm using an Arduino UNO.
I have a C# program that turns an LED connected to Analog pin 9 - that works perfectly fine. I am now trying to add a text box to my program that will take a value entered (between 0-255) and adjust the brightness of the LED accordingly when I hit the "change" button. But I am having trouble understanding how to send a value like that, and how to have the Arduino "interpret" that - then adjust the brightness. I know how to "hard code" a brightness value straight into the Arduino sketch, but I want the brightness to be sort of like a variable that can be adjusted from my C# application.
Here is my C# Code at the moment:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace ArduinoBlink
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.PortName = "COM4"; //Using COM4 as serial port
serialPort1.BaudRate = 9600; //Setting our Baud Rate to 9600
serialPort1.Open(); //Opening our serial port
ButtonOff.Enabled = false; //Disabling the off button when we start our program, as the LED is off by default
BrightnessBox.Enabled = false; //Cannot adjust brightness until the LED is "on"
ChangeButton.Enabled = false; //Same as the line above
//Check if port is open
if (!serialPort1.IsOpen)
{
MessageBox.Show("Could not open port");
}
}
private void ButtonOn_Click(object sender, EventArgs e)
{
//When "On" button is clicked, enable the Textbox "Brightness" and the "Change" button. Enables "Off" button.
BrightnessBox.Enabled = true;
ChangeButton.Enabled = true;
ButtonOn.Enabled = false;
ButtonOff.Enabled = true;
//Writes "1" to the serial port, the arduino sees this and turns the LED on.
serialPort1.Write("1");
label1.Text = "LED is on!";
label1.BackColor = Color.Green;
}
private void ButtonOff_Click(object sender, EventArgs e)
{
//When "Off" button is clicked, disable the Textbox "Brightness" and the "Change" button. Enables "On" button.
BrightnessBox.Enabled = false;
ChangeButton.Enabled = false;
ButtonOn.Enabled = true;
ButtonOff.Enabled = false;
//Writes "0" to the serial port, the arduino sees this and turns the LED off.
serialPort1.Write("0");
label1.Text = "LED is off!";
label1.BackColor = Color.Red;
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//If we close the form and the port is open, close it.
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
}
private void ChangeButton_Click(object sender, EventArgs e)
{
//When I click the "change" button, I want to take the value in BrightnessBox.text (from 0-255)
//and then send that to the Arduino. I then want the Arduino to do whatever is needed
//To adjust the brightness via information sent from my C# program
}
}
}
And here's the Arduino code:
int ledPin = 9;
void setup()
{
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available())
{
char incomingChar = Serial.read();
//your protocol goes here
switch(incomingChar)
{
case '1':
// led is ON
analogWrite(ledPin, 255);
break;
case '0':
// led is OFF
analogWrite(ledPin, 0);
break;
}
}
}
If it helps to understand what I'm trying to do better, here's a little screenshot of my c# program (Once the LED is "on" I want to be able to adjust the brightness of the LED):
Thanks in advanced, let me know if I can make something more clear. However, I am still very "noob" at this.