Arduino Projects

how to split a string message and access the sensors values

Split a String Message, Description:

 

split a string message- In this tutorial, You learn how you can send multiple values from Arduino to Visual Basic application in one message and then how to split a string message and access each value separately. In this project, we will be using a comma”, ” as the delimiter.

To explain this we will be making An application in vb.net ” visual basic .net ” to receive a message and then split the message using the split function.

This tutorial will really help you in designing advanced level projects, in which multiple sensors are monitored. Read this article or watch Video given at the end of this article from start to the very end to learn how you can split a string and access the sensors values sent from the Arduino to visual basic application using Serial communication. The programming languages used are

  1. visual basic 2010 express edition and
  2. C language Arduino Uno IDE.

The Arduino programming is done in c language. This tutorial will cover the application designing, complete circuit diagram explanation, LDR and variable resistor interfacing, controller programming, and testing.

without any further delay, let’s get started!!!


Amazon Links:

12v Adaptor:

Arduino Uno

Arduino Nano

Mega 2560:

LDR “light dependent resistor”:

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


About the LDR:

split a string message

LDR stands for “LIGHT DEPENDENT RESISTOR”. LDR is basically a resistor whose resistance changes with the amount of light falling on the LDR. As the light intensity increases the resistance decreases. While with the decrease in light intensity the resistance increases. So we can say that an LDR is a variable resistor, whose resistance is controlled by the light.

The Basic working principle of the LDR is just like the ordinary resistor. Unlike the other resistors the LDR also has no polarity, so it doesn’t matter how you connect the two legs of the LDR. In this project, the Variable resistor will be used in series with another 10k resistor. So the LDR and the 10k resistor will make a voltage divider. The voltage on the middle of the LDR and 10k resistor will change as the light changes.


The output of the voltage divider circuit will be connected with the analog pin of the Arduino or Mega. Then using the Arduino programming we read the value of the LDR and combine this value with the value of the variable resistor and then send the complete message to the computer application, where these values will be split and displayed in desired boxes.

Programming:

Arduino Programming:

The textForSMS variable which is of the type String will be used for making a complete message consisting of the LDR and variable resistor values separated by comma. Then I defined another variable of the type integer “ldr” and assigned it to the analog pin A1 of the Arduino. now we will refer to the analog pin A1 as the ldr. From now on ldr will be the name of the A1. Similarly defined pin A2 and assigned a name vresistor. Then I defined two variables of the type integer ldata and vdata.

String textForSMS;
int ldr = A1; // ldr is connected
int vresistor = A2; // variable resistor
int ldata = 0; // ldr value
int vdata = 0; // variable resistor value

As you know my friends every Arduino and Mega Program has at least two functions which are the void step and void loop functions. Void means that these functions are not returning any values, while the empty parenthesis means that these functions are not taking any arguments as the input. To establish the serial communication, I activated the serial communication using the Serial.begin() function and set the baud rate to 9600. This is the communication speed. Then I set the ldr and variable resistor as the input. One more thing about the void setup function is that, it executes only one time when the arduino or mega board is powered up.


void setup() {

   Serial.begin(9600);
  
  pinMode(ldr, INPUT); 
  pinMode(vresistor, INPUT); 
  
  pinMode(2, OUTPUT); 
  digitalWrite(2, HIGH); // for the variable resistor to apply 5volts
  
  
}

In the void loop we read both the sensors “ LDR and vresistor “ and store the values in variables ldata and vdata. Then we add both the values with comma’s as the separators and store this in variable textForSMS. Then using the serial.println() function we send this message to the computer application. Finally we empty the string and use a delay of 1000 milli seconds which is equal to one second.  The comma is used as the delimiter which will be used to split a string message sent to the computer application.


void loop() {
  
 ldata = analogRead(ldr); 
vdata = analogRead(vresistor); 


 textForSMS = textForSMS + ldata + "," + vdata + ",";
Serial.println(textForSMS); 
     textForSMS = ""; 
delay(1000); 

}


Computer application for split a string message:

Imports System.IO
Imports System.IO.Ports

Public Class Form1

    Dim value1 As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com15"
        SerialPort1.BaudRate = "9600"
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
        SerialPort1.Open()
    End Sub



    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim s As String

        s = TextBox1.Text + "," + "," + "," + ","

        Dim somestring() As String
        ' Split string based on comma
        somestring = s.Split(New Char() {","c})

        TextBox2.Text = somestring(0)
        ' value1 = Convert.ToDecimal(TextBox2.Text)
        TextBox3.Text = somestring(1)
        TextBox4.Text = somestring(2)
        TextBox1.Text = ""


    End Sub

    Private Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            Dim mydata As String = ""
            mydata = SerialPort1.ReadExisting()

            If TextBox1.InvokeRequired Then
                TextBox1.Invoke(DirectCast(Sub() TextBox1.Text &= mydata, MethodInvoker))
            Else
                TextBox1.Text &= mydata
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

Watch Video Tutorial:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button