UT 04: Converting types
This example shows how to convert variable types

Required Materials

1 x Waspmote 1 x Battery 1 x MiniUSB wire

Notes

This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------ [Ut_04] Convert types --------
3
*
4
* Explanation: This example shows how to convert variable types
5
*
6
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
7
* http://www.libelium.com
8
*
9
* This program is free software: you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation, either version 3 of the License, or
12
* (at your option) any later version.
13
*
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
18
*
19
* You should have received a copy of the GNU General Public License
20
* along with this program. If not, see .
21
*
22
* Version: 3.0
23
* Design: David Gascón
24
* Implementation: Yuri Carmona
25
*/
26
27
28
void setup()
29
{
30
// Init USB
31
USB.ON();
32
33
}
34
35
void loop()
36
{
37
/////////////////////////////////////////////////
38
// 1. Convert from long int to string
39
/////////////////////////////////////////////////
40
char number2[20];
41
Utils.long2array(1356, number2); // Gets the number ‘1356’ into the string
42
USB.println(number2);
43
44
45
/////////////////////////////////////////////////
46
// 2. Convert from float to string (3 decimals)
47
/////////////////////////////////////////////////
48
char number3[20];
49
Utils.float2String (134.54342, number3, 3);
50
USB.println(number3);
51
52
53
/////////////////////////////////////////////////
54
// 3. Convert from string to int
55
/////////////////////////////////////////////////
56
int number4 = atoi("2341");
57
USB.println(number4);
58
59
60
/////////////////////////////////////////////////
61
// 4. Convert from string to long int
62
/////////////////////////////////////////////////
63
long int number5 = atol("143413");
64
USB.println(number5);
65
66
USB.println(F("------------------------------------"));
67
68
delay(3000);
69
}
70
71
Copied!

Output

1
H#
2
1356
3
134.543
4
2341
5
143413
6
------------------------------------
7
...
Copied!
Last modified 1yr ago