software

Strings in arduino what are they, what are they for and how to use them in the programming of free hardware projects?

Strings in Arduino are very useful tools that you should use to program your board . Due to this it is important to know what these functions are for and what are the classes of Strings that you can find.

For this it is necessary that you read this article to the end because we will explain to you what are the Strings that exist and how is the syntax that you must use to understand their work.

But this is not all, you can also find a guide to correctly use a String (Object) and a list with the best projects that you can do yourself to practice.

What is a Strings in Arduino programming and what are they for when programming this type of hardware?

In Arduino programming, Strings are tools used to store character strings. Therefore, a String is not considered as a data in itself, but as a dependent class of these. The ending they have is by means of a null character. In addition, they are characterized by reserving a certain memory capacity of the Arduino board to create a buffer and to be able to use the functions they offer to users intensively .

Types of Strings in Arduino programming What are all that exist?

You can find different types of String that are used in Arduino programming .

We will show you each of them below:

Char Array

Char Array are character arrays used in large text strings . That is, every time you work on a string with a C ++ language that ends in the value 0 (\ 0), consecutive memory composition structures are created, thus occupying less memory and otherwise a String would be identified as char . Keep in mind that you always have to end with a null character and you can refer to the individual characters in the string.

Object

In case of not working with large amounts of texts , a String Object can be used . These data classes improve the handling of character strings through methods that are used by users to compare two strings, find certain characters, concatenate and add elements to the string, among other functions.

Strings operators

Operators are symbols that are used to set a particular statement to String. In this way, actions can be established that serve to add, extract and see if a certain character in the string is true or not.

Syntax of a String (char array) How to understand its declarations and conditions of use?

When using a long text string you will have to use a Char Array .

To do this, we show you how you should handle it:

Declarations

The statements that are valid in a string for a String are:

  • char Str1 [14]; This syntax is used to start an array in a general way.
  • char Str2 [4] = {‘a’, ‘u’, ‘i’}; In this case additional characters are set, I want and compiler will add the null character.
  • char Str3 [4] = {‘a’, ‘u’, ‘i’, ‘\ 0’}; If you prefer and want to incorporate the null character manually, you will have to use this structure.
  • char Str4 [] = “ipap”; You can initialize a string with a constant using single quotes. The IDE will set the size of the array.
  • char Str5 [8] = “ipaparduino”; In case of setting the size manually and indicating a constant, use this syntax.
  • char Str6 [15] = “ipaparduino”; This structure allows you to add a larger string, since a blank space is left.

Null termination

Null endings are used to define where a string ends, for this it is necessary to include a null character, from ASCII code, at the end of the function. It can be included manually by writing “\ 0” or automatically, so that it has been interpreted by the Arduino IDE .

String arrays

Arrays are used to configure strings that have large amounts of text . In this way it is possible to work more easily because the complexity of the handling is reduced.

Learn step by step how to use a Strings (Object) to program in Arduino

To use a String in IDE you must follow this step by step:

Syntax

The first thing you will have to know is how to handle the structure that a String has .

To do this, you will have to take into account the syntax used in a String (Object):

  • String (val)
  • String (val, base)
  • String (val, decimalPlaces)

Parameters

Next, you are going to have to implement the parameters, which are used in the previous syntax:

  • val: refers to the variable that needs to be formatted to get a string. The supported data in these parameters are byte, char, double, float, int, long, string, unsigned int, and unsigned long.
  • base: this parameter is optional and shows the base in which the variable should be formatted. It defaults to base 10, but can also be used on hexadecimal and binary bases. For example, String (14, HEX) and String (13, BIN).
  • decimalPlaces: when a floating or double data is used as values, the decimal place must be set.

Features

Finally, you will have to incorporate the functions to the String.

The functions used in the Arduino IDE are:

  • charAt (): this function is used to access a specific element of the string.
  • comparateTo (): with this tool you can compare two strings and in this way establish if they are the same or not.
  • concat (): when you need to add an element or parameter to the string you will have to use this function.
  • c_str (): this variable is used when you need to convert a string to a type C string.
  • endsWith (): used to check if a certain string ends with a specific character.
  • equals (): you can use this function to compare two strings and they found equality.
  • equalsIgnoreCase (): like the previous function, this variable is used to compare two strings, but not case sensitive.
  • getBytes (): allows you to copy characters from a string.
  • index of (): this function is used on a String when you need to search for a string or a value within a string.
  • lastIndexOf (): used to find a character or a string.
  • lenght (): when I need to know the length of the string, you are going to have to use this function.
  • remove (): remove or modify characters.
  • replace (): You can replace one character with another within a string.
  • reserve (): you can use this function to establish a buffer to the memory of the board and in this way to be able to work with processes without affecting the performance of the hardware.
  • setCharAt (): used to embed a character.
  • begins with (): this anointing is characterized by checking if a string begins with a certain element.
  • substring (): its name says it all, this function is used to obtain a substring within a string.
  • toCharArray (): allows you to copy characters into the selected buffer to have a space in memory.
  • toDouble (): If the string is valid, it can be used to duplicate it.
  • toInt (): a string can be converted to an integer, as long as it is valid.
  • toFloat (): manages to convert a String into a float character.
  • toLowerCase (): when the String is written in uppercase and needs to be converted to lowercase, this function should be used.
  • toUpperCase (): it is the inverse situation to the previous function.
  • trim (): you can use this tool to get a white space at the beginning or end of the String version.

List of the best projects programmed with Strings that you can do yourself to practice

We will show you below the best projects that you can program yourself using Strings .

Let’s get started:

Quick response device

With an Arduino UNO board , a push button, a breadboard, a led screen, a 10k resistor set necessary for the installation, you can create a screen in which you enter a question and receive a random answer. To incorporate the questions and answers, the String must be used.

The codes you should use:

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

Now, you will have to include the variables and configure them on the screen display:

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 32

#define STRING_ARRAYSIZE 10

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) - Arduino UNO: A4 (SDA - Data), A5 (SCL - Clock)

#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display (SCREEN_WIDTH, SCREEN_HEIGHT, & Wire, OLED_RESET);

Set the button variables:

const int buttonPin = 2;

int buttonState = 0;

int lastButtonState = 0;

int randomNumber = 0;

String mainText = "Ask";

void setup () {

// Serial setup

begin (9600);

// OLED setup

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally

if (! display.begin (SSD1306_SWITCHCAPVCC, 0x3C)) {// Address 0x3C for 128x32

println (F ("SSD1306 allocation failed"));

for (;;); // Don't proceed, loop forever

}

displayTextOnOLED (mainText);

// PIN dedicated to button setup

pinMode (buttonPin, INPUT);

// if analog input pin 0 is unconnected, random analog

// noise will cause the call to randomSeed () to generate

// different seed numbers each time the sketch runs.

// randomSeed () will then shuffle the random function.

randomSeed (analogRead (0));

}

// purpose is to listen to button state

// and when pressed the OLED to display a different text

// when released to show the setup () text

void loop () {

// read the state of the pushbutton value:

buttonState = digitalRead (buttonPin);

if (buttonState! = lastButtonState) {

if (buttonState == HIGH) {

displayTextOnOLED (randomText ());

} else {

displayTextOnOLED (mainText);

}

}

lastButtonState = buttonState;

}

void displayTextOnOLED (String str) {

clearDisplay ();

setTextSize (2); // Draw 2X-scale text

setTextColor (SSD1306_WHITE);

setCursor (0,0);

println (str);

display (); // Show initial text

}

Use the string:

String randomText (void) {

String texts [STRING_ARRAYSIZE] = {"Da", "Nu", "Nu stiu!", "Nu vreau!", "Du-te naibii!", "Varule", "Te pup!", "Sigur!", "Sigur ca nu!", "Nah!"};

if (buttonState! = lastButtonState) {

randomNumber = random (1, 10);

return texts [randomNumber];

} else {

return texts [randomNumber];

}

}

Radar station

In this project you will need an Arduino board, an HC-SR04 ultrasonic sensor , a servo motor, cables and a 10k resistor. You will have to connect the sensor to pins 10 and 11 of the board and then the servo to pin number 12.

Next, you will have to enter these codes using Strings:

#include <Servo.h>.

const int trigPin = 10;

const int echoPin = 11;

Long duration;

int distance;

To control the servomotor you will have to create a servo object:

MyServo Servo;

void setup () {

pinMode (trigPin, OUTPUT);

Output PinMode (echoPin, INPUT);

begin (9600);

attach (12);

}

void loop () {

Set the turning radius of the servomotor between 15 to 165 degrees:

for (int i = 15; i <= 165; i ++) {

write (i);

delay (30);

distance = calculateDistance ();

print (i);

print (",");

print (distance);

print (".");

}

for (int i = 165; i> 15; i -) {

write (me);

delay (30);

distance = calculateDistance ();

print (i);

print (",");

print (distance);

print (".");

}

}

int calculateDistance () {

digitalWrite (trigPin, LOW);

delayMicroseconds (2);

digitalWrite (trigPin, HIGH);

delayMicroseconds (10);

digitalWrite (trigPin, LOW);

duration = pulseIn (echoPin, HIGH);

distance = duration * 0.034 / 2;

return distance;

}

Use SerialEvent () to receive distance and angle values:

empty drawRadar () {

pushMatrix ();

translate (960,1000);

noFill ();

strokeWeight (2);

cerebrovascular accident (98,245,31);

arc arc (0,0,1800,1800, PI, TWO_PI);

arc (0,0,1400,1400, PI, TWO_PI);

arc (0,0,1000,1000, PI, TWO_PI);

arc (0,0,600,600, PI, TWO_PI);

line of the angular lines (-960,0,960,0);

line (0,0, -960 cos (radians (30)), - 960 sin (radians (30)));

line (0,0, -960 cos (radians (60)), - 960 sin (radians (60)));

line (0,0, -960 cos (radians (90)), - 960 sin (radians (90)));

line (0,0, -960 cos (radians (120)), - 960sin (radians (120)));

line (0,0, -960 cos (radians (150)), - 960 sin (radians (150)));

line (-960 * cos (radians (30)), 0.960.0);

popMatrix ();

}

Draw the lines that will be seen on the radar:

empty drawLine () {

pushMatrix ();

strokeWeight (9);

cerebrovascular accident (30,250,60);

translate (960,1000);

location line (0,0,950 cos (radians (iAngle)), - 950 sin (radians (iAngle)));

popMatrix ();

}

Radar-Print-Screen-04

Now you are going to have to draw the objects that the radar detects:

empty drawObject () {

pushMatrix ();

translate (960,1000);

strokeWeight (9);

cerebrovascular accident (255,10,10);

pixsDistance = iDistance 22.5;

if (iDistance <40) {

// draw the object according to the angle and

distance line (pixsDistance cos (radians (iAngle)), -pixsDistance sin (radians (iAngle)), 950 cos (radians (iAngle)), - 950 * sin (radians (iAngle)));

}

popMatrix ();

}

Add words to the screen:

empty draw () {

fill (98,245.31);

textFont (orcFont);

noStroke ();

fill (0.4);

rect (0, 0, width, 1010);

fill (98,245.31); // green color

drawRadar ();

Draw line();

drawObject ();

drawText ();

}

Perform the radar programming:

import processing.series. *;

import java.awt.event.KeyEvent;

import java.io.IOException;

Serial myPort;

String angle = "";

Chord distance = "";

String data = "";

String noObject;

float pixsDistance;

int iAngle, iDistance;

int index1 = 0;

int index2 = 0;

PFont orcFont;

empty setting () {

size (1920, 1080);

gentle();

myPort = new Serial (this, "COM4", 9600);

bufferUntil ('.');

orcFont = loadFont ("OCRAExtended-30.vlw");

}

empty draw () {

fill (98,245.31);

textFont (orcFont);

noStroke ();

fill (0.4);

rect (0, 0, width, 1010);

fill (98,245.31);

drawRadar ();

Draw line();

drawObject ();

drawText ();
Tags

Leave a Reply

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

Back to top button