String functions

dev board Arduino UNO
firmware Arduino
chip ATmega328P

Buy the components

Description

Examples of various String functions and how to pass the function a reference to a String object that you want to write to.

Code

Download code
String sample;
String animal = "lion";

void setup() {
  Serial.begin(9600);
  Serial.println("String functions...");

  Serial.print("string.length(): ");
  Serial.println(animal.length());
}

void loop() {
  temperature(sample);

  Serial.print("Temperature returned: ");
  Serial.println(sample);

  delay(1000);
}

void temperature(String &value) {
  value = String(random(0, 30));
}

Makefile

BOARD?=arduino:avr:uno
PORT?=/dev/cu.usbmodem14*
BUILD=build

.PHONY: default lint all flash clean

default: lint all flash clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright,-runtime/references *.ino

all:
	arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./

flash:
	arduino-cli upload --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)

clean:
	rm -r build

Serial console

Serial output from the firmware.

String functions serial console

Prototype

A photo of the actual setup.

String functions prototype

References