The idea for this project was born actually from the need of fun and knowledge. This project is easy to make and has many applications. We can use it, for example, to control, store and display some hardware variables.
Let's get startedFirst of all, we need to connect our LCD (I was using I2C converter) :
When we did this, we can move to the next part...
Let's write some code...Set 16x2 LCD with address 0x3F - address of I2C bus device can be found from scanning via simple sketch available on Arduino Playground.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,16,2);
Now we have to declare a String
variable to store our Serial input and setup rest of stuff.
String inData;
void setup()
{
lcd.init();
Serial.begin(9600);
lcd.backlight();
}
If we would like to send data directly by a string variable it could be problematic, because of about one-second latency based on string size. To avoid this use char
variable and add it to our inData
.
char recieved = Serial.read();
inData += recieved;
Ok, everything's fine but when receiving data stop coming? There is solution:
if (recieved == '*')
{ ... }
When last of char will be * Arduino will know when to proceed next part, Now we have to handle our message. We need to get rid of this *
char so:
inData.remove(inData.length() - 1, 1);
And finally:
lcd.setCursor(0,0);
lcd.print(inData);
inData = ""; // Clear buffer
But... We have second line unused... Here is the solution
if (recieved == '#')
{
inData.remove(inData.length() - 1, 1);
lcd.setCursor(0,1);
lcd.print(inData);
inData = "";
}
}
When last of char will be # Arduino will set the second line of our LCD and print other data.
ConfigurationWhy C#, not C or C++ ? C# is easy and fast (fast... I mean in writing)
First of all, we need to download additional library for easy access to our hardware variables.
After unpacking, we have our library: OpenHardwareMonitorLib.dll
Create new project -> Windows Forms Application -> Right click on References in Solution Explorer -> Browse -> OpenHardwareMonitorLib.dll and don't forget to check it.
AppearancePrepare our form:
Some directives...
using System.IO.Ports;
using OpenHardwareMonitor.Hardware;
And declarations...
SerialPort port = new SerialPort();
Computer c = new Computer()
{
GPUEnabled = true,
CPUEnabled = true
};
float value1, value2;
c
is Open Hardware Monitor object. We have to enable CPU and GPU. In Form Load event put this:
c.Open();
In form constructor:
public Form1()
add:
Init();
and then, anywhere:
private void Init()
{
try
{
notifyIcon1.Visible = false;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.DataBits = 8;
port.Handshake = Handshake.None;
port.RtsEnable = true;
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
comboBox1.Items.Add(port);
}
port.BaudRate = 9600;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Simple... Initialize declared variables, scan for open ports and add it to the comboBox1
Now major function to store and send variables:
private void Status()
{
foreach (var hardwadre in c.Hardware)
{
if (hardwadre.HardwareType == HardwareType.GpuNvidia)
{
hardwadre.Update();
foreach (var sensor in hardwadre.Sensors)
if (sensor.SensorType == SensorType.Temperature)
{
value1 = sensor.Value.GetValueOrDefault();
}
}
if (hardwadre.HardwareType == HardwareType.CPU)
{
hardwadre.Update();
foreach (var sensor in hardwadre.Sensors)
if (sensor.SensorType == SensorType.Temperature)
{
value2 = sensor.Value.GetValueOrDefault();
}
}
}
try
{
port.Write(value1 + "*" + value2 + "#");
}catch(Exception ex)
{
timer1.Stop();
MessageBox.Show(ex.Message);
toolStripStatusLabel1.Text = "Arduino's not responding...";
}
Don't forget to add Status()
function to timer tick event.
Now connect button:
try
{
if (!port.IsOpen)
{
port.PortName = comboBox1.Text;
port.Open();
timer1.Interval = Convert.ToInt32(comboBox2.Text);
timer1.Enabled = true;
toolStripStatusLabel1.Text = "Sending data...";
label2.Text = "Connected";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
We're checking if a port is already open, if not setting timer interval from comboBox2 and run everything up!
Disconnect button:
try
{
port.Write("DIS*");
port.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
label2.Text = "Disconnected";
timer1.Enabled = false;
toolStripStatusLabel1.Text = "Connect to Arduino...";
data = "";
Write last data to Arduino to said that we're done.
You can add some functions like for e.g. hide to tray etc. (Full code below)
Program has to be run with administrator privileges!
Comments