// /*
// * ref_vs_pointers
// * A sketch written to help people understand the use of references and pointers
// * Written by Peter Dalmaris, April 8 2016.
// * Arduino Tips and Tricks
// * txplore.com
// *
// * Requirements: MemoryFree from https://github.com/McNeight/MemoryFree
// */
// #include "MemoryFree.h"
// String a_str;
// void setup() {
// // put your setup code here, to run once:
// Serial.begin(9600);
// print_free_memory();
// int a = 5;
// int b = 10;
// int *pointer; // Create a new pointer. Variable "pointer" will contain a memory address where
// // an int value will be stored
// print_free_memory();
// // // Play with pointer to variable a
// // Serial.println(F("1 ----- a -----"));
// // Serial.print(F("a="));
// // Serial.println(a);
// // pointer = &a; //Get the address for variable "a" and assign it to pointer "pointer"
// // *pointer = 6; // Store value "6" to memory location stored in "pointer".
// // Serial.print(F("a="));
// // Serial.println(a);
// // print_free_memory();
// // Serial.println();
// // // Play with pointer to variable b
// // Serial.println(F("2 ----- b -----"));
// // Serial.print(F("b="));
// // Serial.println(b);
// pointer = &b; //Get the address for variable "b" and assign it to pointer "pointer"
// // This will reassign the memory location stored in "pointer" from the original
// // memory location for int "a", to that of int "b".
// *pointer = 11; // Store value "11" to memory location stored in "pointer".
// // Serial.print(F("b="));
// // Serial.println(b);
// // print_free_memory();
// // Serial.println();
// // // Play with passing data to a function via reference
// // pass_reference(a); //Even though we are passign int "a", the function expects a reference as a parameter.
// // //Notice that the function declaration uses the "&" operator. This will retrieve
// // //the memory location for int "a" instead of making a copy for it.
// // Serial.print(F("a="));
// // Serial.println(a);
// // print_free_memory();
// // Serial.println();
// // pass_reference(b); //Exactly as above.
// // print_free_memory();
// // Serial.println();
// // Play with passing data to a function via pointer
// // pass_pointer(pointer);
// // pointer = &a;
// // pass_pointer(pointer);
// // Serial.print(F("a="));
// // Serial.println(a);
// // print_free_memory();
// // Serial.println();
// // //Play with passing data to a function via value
// // pass_value(a);
// // pass_value(b);
// // Serial.print(F("b="));
// // Serial.println(b);
// // print_free_memory();
// // Serial.println();
// //Do all of the above using a String object (instead of primitives)
// String a_string = "Hello World!";
// String b_string = "Hello again!";
// String *string_pointer;
// print_free_memory();
// Serial.println();
// // print_free_memory();
// // Serial.println();
// // a_string.reserve(50);
// // b_string.reserve(50);
// // print_free_memory();
// // Serial.println();
// //Print the content of the pointer;
// // string_pointer = &a_string;
// // Serial.println(F("6 ------- String a -----"));
// // Serial.print(F("string a="));
// // Serial.println(*string_pointer);
// // print_free_memory();
// // Serial.println();
// // string_pointer = &b_string;
// // Serial.println(F("7 ------- String b -----"));
// // Serial.print(F("string b="));
// // Serial.println(*string_pointer);
// // print_free_memory();
// // Serial.println();
// // Play with passing string to a function via reference
// // pass_reference_str(a_string);
// // print_free_memory();
// // Serial.println();
// // a_string = "Hello World 55";
// // Serial.println(a_string);
// // print_free_memory();
// // Serial.println();
// // pass_reference_str(b_string);
// // print_free_memory();
// // Serial.println();
// // Play with passing string to a function via pointer
// // pointer = &a_string;
// print_free_memory();
// Serial.println();
// pass_pointer_str(&a_string);
// print_free_memory();
// Serial.println();
// // pointer = &a_string;
// pass_pointer_str(&b_string);
// print_free_memory();
// Serial.println();
// //Play with passing data to a function via value
// pass_value_str(a_string);
// print_free_memory();
// Serial.println();
// pass_value_str(b_string);
// print_free_memory();
// Serial.println();
// }
// void loop() {
// // put your main code here, to run repeatedly:
// a_str = a_str + "5555555555555555555555555555555555555555555555555555555555555555";
// print_free_memory();
// Serial.println();
// }
// void pass_reference(int &a_ref)
// {
// a_ref = 0;
// Serial.println(F("3 ----- Passing by reference -----"));
// Serial.println(a_ref);
// print_free_memory();
// Serial.println();
// }
// void pass_pointer(int *a_pointer)
// {
// *a_pointer = 99;
// Serial.println(F("4 ----- Passing by pointer -----"));
// Serial.println(*a_pointer);
// print_free_memory();
// Serial.println();
// }
// void pass_value(int value)
// {
// value = 88;
// Serial.println(F("5 ----- Passing by value-----"));
// Serial.println(value);
// print_free_memory();
// Serial.println();
// }
// void pass_reference_str(String &a_ref)
// {
// a_ref = "World Hello1234567890";
// Serial.println(F("8 ----- Passing String by reference -----"));
// Serial.println(a_ref);
// print_free_memory();
// Serial.println();
// }
// void pass_pointer_str(String *a_pointer)
// {
// *a_pointer = "dee jaaa";
// Serial.println(F("9 ----- Passing String by pointer -----"));
// Serial.println(*a_pointer);
// print_free_memory();
// Serial.println();
// }
// void pass_value_str(String value)
// {
// Serial.println(F("10 ----- Passing String by value-----"));
// Serial.println(value);
// print_free_memory();
// Serial.println();
// }
// void print_free_memory(){
// Serial.print(F("freeMemory()="));
// Serial.println(freeMemory());
// }
void setup() {
Serial.begin(9600);
// Prior to rolling over
unsigned long currentMillisA = 4294952295;
// An earlier time than currentMillis
unsigned long lastMillisA = 4294947295;
Serial.print("1. Difference: ");
// Find out the difference
// between the two times
Serial.println(currentMillisA-lastMillisA);
Serial.println();
// Rolled over
unsigned long currentMillisB = 3000;
// Prior to rolling over 4,294,967,295
unsigned long lastMillisB = 4294947295;
Serial.print("2. Difference: ");
// Find out the difference
// between the two times
Serial.println(currentMillisB-lastMillisB);
}
void loop() {
}