void setup() {
Serial.begin(115200);
Serial.println("Hello floating point");
float x = 0.3;
float y = 2.0;
Serial.println(x / y, 7);
Serial.println(y / x, 7);
Serial.println( y * x, 7);
}
Floating point precision issues that might turn up to bite you are not confined to division. The subtraction of two numbers that are very similar can produce a result that is imprecise and may cause issues if that small value is used in later calculations.Page 22 introduces literal values. One of those can be used to define a binary number and the page suggests a format that is now deprecated (although still valid).
int bb = B1100011;To avoid warning messages you should consider using the prefix 0b or 0B instead of the single B.
int bb = 0b1100011
The preferred new literal prefix is consistent with the hexadecimal prefix I suppose but that begs a question or two about a preceeding zero being used to define a numeric literal in Octal format. [This catches programmers out frequently.]
void setup() {
Serial.begin(115200);
char pad[] = "just some text";
char test[] = "program";
Serial.println(test);
test[-2] = 'z';
Serial.println(pad);
}
void setup() {
Serial.begin(115200);
char test[12] = "program";
Serial.println(test);
test[9] = 'x';
test[10] = 'y';
test[11] = '\0';
Serial.println(test);
}
void setup() {
Serial.begin(115200);
byte x = 34; // maximum byte value is 255
int y = 32765;
x = y;
Serial.println(x);
}
void setup() {
Serial.begin(115200);
int x = 9;
int y = 2;
float f = x/ y;
Serial.print("x / y = ");
Serial.println(f);
f = x / (float)y;
Serial.print("x / (float)y = ");
Serial.println(f);
int a = 123;
int b = 456;
int c = a + b;
Serial.print("a + b = ");
Serial.println(c);
c = (char)(a + b);
Serial.print("(char)(a + b) = ");
Serial.println(c);
}
void setup() {
Serial.begin(115200);
Serial.println("Type me a number 0 to 2");
}
void loop() {
byte nChar = 0;
if (Serial.available() > 0) {
nChar = Serial.read();
switch(nChar){
case '0':
Serial.println("Lets go higher");
break;
case '1':
Serial.println("Getting started");
break;
case '2':
Serial.println("Thats my number");
break;
default:
Serial.println("Just 0 to 2, please");
break;
}
}
}
void setup() {
Serial.begin(115200);
for(int lp = 1; lp < 11; lp++) {
Serial.println(lp);
}
}
void setup() {
Serial.begin(115200);
for(int lp = 10, j = 0; lp > j; lp--) {
Serial.println(lp);
}
}
for (float f = 0; f <= 1.0; f += 0.1)
{
Serial.println(f, 7);
}
for (int i = 0; i < 11; i++)
{
Serial.println(i * 0.1, 7);
}
void setup() {
Serial.begin(115200);
byte a = 244;
int b = 12;
Serial.println(ourMax(a, b));
}
long ourMax(long x, long y) {
if(x > y) {
return x;
}
return y;
}
void setup() {
Serial.begin(115200);
int test[] = {23, 76, 81, 1, 9, 677, 9876, 11};
Serial.print("Array test element count: ");
Serial.println(sizeof(test) / sizeof(test[0]));
Serial.println(getMax(test));
}
int getMax(int mInts[]){
Serial.print("Size of argument mInts: ");
Serial.println(sizeof(mInts));
}
void setup() {
Serial.begin(115200);
int test[] = {23, 76, 81, 1, 9, 677, 9876, 11};
Serial.print("Maximum value: ");
Serial.println(getMax(test, sizeof(test) / sizeof(test[0])));
}
int getMax(int mInts[], int arrayLen){
int maxVal = -32768; // lowest 16 bit int value
for(int i = 0; i < arrayLen; i++) {
if (mInts[i] > maxVal){
maxVal = mInts[i];
}
}
return maxVal;
}
void setup() {
Serial.begin(115200);
float test = 32.8765;
long res2 = 0;
long res1 = myFunct(test, &res2);
Serial.print("Result: ");
Serial.println(res1);
Serial.print("Other result: ");
Serial.println(res2);
}
long myFunct(float num, long* b) {
long a = floor(num); // floor returns the int part of num
b = a * a; // sets the value of res2 back in the setup() function
// to the square of a
return a;
}
void setup() {
Serial.begin(115200);
Serial.print("Series total is ");
Serial.println(getSum(9, 34, 76, 54, 34, 12, 22, 90, 101, 33));
}
int getSum(int x, ...) {
int sum = 0 ;
va_list args;
va_start(args, x);
for(int i = 0 ; i < x ; i++) {
sum += va_arg(args, int);
}
va_end(args);
return sum;
}
void setup() {
Serial.begin(115200);
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int* po = &array[0];
for (int i = 0; i < 9; i++) {
Serial.print("Pointer value: ");
Serial.print((int)po);
Serial.print(" array value: ");
Serial.println(*po);
po++;
}
foo(array);
}
void foo(int array[]) {
Serial.print("Pointer to array: ");
Serial.println((int)array);
Serial.print("value at array[0]: ");
Serial.println(*array);
}
void setup() {
Serial.begin(115200);
char array[][9] = {"January", "February", "March", "April"};
inspect(array);
}
void inspect(char array[][9]){
for (int p = 0; p < 4; p++) {
Serial.print("Pointer value: ");
Serial.println((int)array);
Serial.println(*array);
array++; // increment the pointer
}
}
void setup() {
Serial.begin(115200);
char months[][2][9] = {
{"Jan", "January"},
{"Feb", "February"},
{"Mar", "March"},
{"Apr", "April"},
{"May", "May"},
{"Jun", "June"},
{"Jul", "July"},
{"Aug", "August"},
{"Sep", "September"},
{"Oct", "October"},
{"Nov", "November"},
{"Dec", "December"}
};
Serial.println(months[0][0]); // outputs “Jan”
Serial.println(months[3][1]); // outputs “April”
Serial.println(&months[0][0][0]); // outputs “Jan”
Serial.println(&months[3][1][0]); // outputs “April”
}
void setup() {
Serial.begin(115200);
int x = 7;
int* p1 = &x;
int **p2 = &p1;
Serial.print("The address of x: ");
Serial.println((int)p1);
Serial.print("The value of x: ");
Serial.println(*p1);
Serial.print("The address of pointer p1: ");
Serial.println((int)&p1);
Serial.print("The value at P2: ");
Serial.println((int)p2);
Serial.print("The value P2 indirectly points to: ");
Serial.println(**p2);
Serial.print("The address of P2: ");
Serial.println((int)&p2);
}
void setup() {
Serial.begin(115200);
int (*ptr)(float, int); // declares the pointer type
ptr = func1; // ptr is initialised
int a = ptr(3.2, 5); // calls func1() using ptr
Serial.println(a);
ptr = func2; // resets ptr to point to func2()
Serial.println(ptr(55.0, 5)); // calls func2() using ptr
}
int func1(float x, int y){
return y *= x;
}
int func2(float x, int y){
return x /y;
}
void (*actions[3])(int, int); // declares a function pointer array
const int IN_1 = A0 ;
const int IN_2 = A1 ;
void setup() {
Serial.begin(115200);
actions[0] = actionA; // initialises the array elements
actions[1] = actionB;
actions[2] = actionC;
}
void loop() {
int a = analogRead(IN_1); // will get a random value
int b = analogRead(IN_2); // if pin not connected
if(Serial.available()) {
int act = Serial.parseInt();
actions[act](a, b); // calls a function selected from the
// function pointer array
}
}
void actionA(int temp, int sg) {
Serial.println("Running action A"); // dummy actions
}
void actionB(int temp, int sg) {
Serial.println("Running action B");
}
void actionC(int temp, int sg) {
Serial.println("Running action C");
}
void setup() {
Serial.begin(115200);
Serial.println(math(21, 4, mod));
}
int math(int a, int b, int (*mathOp)(int, int)) {
return mathOp(a, b);
}
int sum(int a, int b) {
return a + b;
}
int prod(int a, int b) {
return a * b;
}
int divd(int a, int b) {
return a / b;
}
int mod(int a, int b) {
return a % b;
}
typedef int (*fptr)(int); // defines the fptr function pointer type
void setup() {
Serial.begin(115200);
fptr z = func(-4);
Serial.println(z(3));
Serial.println(func(7)(4));
}
int f1(int x) {
return x * 23;
}
int f2(int x) {
return x * 56;
}
fptr func(int a) { // defines a function that returns the fptr type
if(a > 0) {
return f1; // returns a pointer to f1
}
return f2; // returns a pointer to f2
}
struct compo {
struct {
byte red;
byte green;
byte blue;
} colours;
byte bright;
};
void setup() {
Serial.begin(115200);
compo nextComp;
nextComp.colours.red = 255;
compo* comptr = &nextComp; // defines the pointer
Serial.println((*comptr).colours.red); // uses the pointer to access the struct instance member
}
union {
int tempInt;
float tempFloat;
long tempLong;
} tempVar = {tempFloat: 67.8}; // initialises a float value in tempVar
union vars{
int intVar;
float fVar;
}; // declares a named union type vars
void setup() {
Serial.begin(115200);
Serial.println(tempVar.tempFloat); // displays 67.80
vars myVar; // creates an instance of the vars union type
myVar.fVar = 8.9876; // assigns a float value to that instance
Serial.println(myVar.intVar); // result is ??
vars myOtherVar = {intVar: 327}; // creates and initialises new instance of vars
}
enum ValueType {INTS, FLOATS, LONGS, UINTS, ULONGS, BYTE};
struct variant {
ValueType v_type;
union {
int intVar;
float floatVar;
long longVar;
unsigned int uintVar;
unsigned long ulongVar;
byte byteVar;
};
};
void setup() {
Serial.begin(115200);
variant arg = {INTS, {intVar: 77}};
Serial.println(arg.intVar);
variant* vptr = &arg;
vptr->intVar = 45;
Serial.println(vptr->intVar);
}
union {
struct{
byte b0:1;
byte b1:1;
byte b2:1;
byte b3:1;
byte b4:1;
byte b5:1;
byte b6:1;
byte b7:1;
}bits;
byte bt;
} binb;
void setup() {
Serial.begin(115200);
binb.bt = 'a'; // set the union byte value
Serial.println(binb.bt, BIN); // display the significant bits of the byte
Serial.println(binb.bits.b0); // display individual bits
Serial.println(binb.bits.b1);
Serial.println(binb.bits.b2);
Serial.println(binb.bits.b3);
Serial.println(binb.bits.b4);
Serial.println(binb.bits.b5);
Serial.println(binb.bits.b6);
Serial.println(binb.bits.b7);
binb.bits.b1 = 1; // set one of the bits
Serial.println((char)binb.bt); // review the result
}
struct Position{
int x;
int y;
int z;
Position operator+(const Position& p){
Position pos;
pos.x = this->x + p.x;
pos.y = this->y + p.y;
pos.z = this->z + p.z;
return pos;
};
};
inline Position operator++(Position& p) {
p.x++;
p.y++;
p.z++;
}
void setup() {
Serial.begin(115200);
Position pos = {23, 45, 46};
Position vect = {5, 10, 0};
pos = pos + vect;
Serial.print("x: ");
Serial.print(pos.x);
Serial.print(", y: ");
Serial.print(pos.y);
Serial.print(", z: ");
Serial.println(pos.z);
pos++;
Serial.print("x: ");
Serial.print(pos.x);
Serial.print(", y: ");
Serial.print(pos.y);
Serial.print(", z: ");
Serial.println(pos.z);
}
struct Position{
int x;
int y;
int z;
Position operator+(const Position& p){
Position pos;
pos.x = this->x + p.x;
pos.y = this->y + p.y;
pos.z = this->z + p.z;
return pos;
};
};
inline Position operator++(Position& p) {
p.x++;
p.y++;
p.z++;
}
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
#define endl "\n"
void setup() {
Serial.begin(115200);
Position pos = {23, 45, 46};
Position vect = {5, 10, 0};
pos = pos + vect;
Serial << "x: " << pos.x << ", y: " << pos.y << ", z: " << pos.z << endl;
pos++;
Serial << "x: " << pos.x << ", y: " << pos.y << ", z: " << pos.z << endl;
}
template<class T> inline T const& Min (T const& a, T const& b)
{
return (a > b) ? b : a;
}
void setup() {
Serial.begin(115200);
Serial.print("Min of 8 or 9 is: ");
Serial.println(Min(8, 9));
Serial.print("Min of 76.9 or 4.5: ");
Serial.println(Min(76.9, 4.5));
}