Saturday, December 9, 2017

14 November 2017- Day 22- Introduction to C++ and logging with SD cards

Day 22:
The affordable nature of working with arduino and the ease of implementation for just about any electronic sensor allows this platform to be an ideal solution for collecting large amounts of data.  The only drawback to the arduino is the limited space available on the chip for storing data.  One way around these restrictions is to include an SD card to store .csv files that can be easily retrieved and also stored in large data sets.  A good example is a weather station.  They require to be put in remote locations and left to collect for long periods of time.  Storing all the information such as pressure, wind speed, temperature, and humidity can be easily accessed in an excel file.
Before starting to use an SD card you must format the card to the proper file system type.  The arduino also comes with an SD library with all necessary functions to properly implement into your program.  When writing to an SD card it is important to note that the file names must be in 8.3 format.  This means the extension can only be 3 characters and the name must be 8 characters or fewer.  Reading from an SD card can be helpful for setting program parameters. For homework we were required to implement the SD card and a temp sensor to collect the temperature once a min and store into an excel file.

(example of the program for saving to SD card.)
(SD card shield hooked up to Arduino; buttons were just left on breadboard)

For the lecture we were introduced to the C++ language.  We noticed of the bat that a lot of the same features in the C language translate almost verbatim to the C++.  C++ also had some new features but not much different than its predecessor.  The following are a side by side example of the same program in both C and C++ to see the differences between the syntax but have the same output:
 Velocity/Acceleration Program in C:
#include <stdio.h>
#include <math.h>

int main(void)
{
/* Declare variables. */
double time, velocity, acceleration;
/* Get time value from the keyboard. */
printf("Enter new time value in seconds: \n");
scanf("%lf",&time);
/* Compute velocity and acceleration. */
velocity = 0.00001*pow(time,3) - 0.00488*pow(time,2) + 0.75795*time + 181.3566;
acceleration = 3 - 0.000062*velocity*velocity;
/* Print velocity and acceleration. */
printf("Velocity = %8.3f m/s \n",velocity);
printf("Acceleration = %8.3f m/sˆ2 \n",acceleration);
/* Exit program. */
return 0;
}

Same program in C++:
#include <iostream>
#include <cmath>
using namespace std;
int main(void)

{
// Declare variables.
double time, velocity, acceleration;
// Get time value from the keyboard.
cout << "Enter new time value in seconds:" << endl;
cin >> time;
// Compute velocity and acceleration.
velocity = 0.00001*pow(time,3) - 0.00488*pow(time,2)
+ 0.75795*time + 181.3566;
acceleration = 3 - 0.000062*velocity*velocity;
// Print velocity and acceleration.
cout.setf(ios::fixed);
cout.precision(3);
cout << "Velocity = " << velocity << " m/s" << endl;
cout << "Acceleration = " << acceleration << " m/s^2" << endl; // Exit program.
return 0;
}


First off, you notice the input and output statements at the beginning of the program. Also, the print statements have a different syntax with cout and << for setting what needs to print out from the program.     We start to find out that the same way functions, arrays, and data files are used in C are also used similarly in C++.  

No comments:

Post a Comment