Chapter 5 - Basic Programming construct - (i) Variable

Basic Programing Construct (i): Variable

Variables are core fundamental to any programming language.
They are small chunks of memory within your program, or you can think of them like boxes.


You’ll use them a lot in your programming journey—for example, in a factory robot to keep track of how many boxes it has moved or maybe something more relatable, computer game where the game needs to hold your current score or keep track of how many lives you have left.


Naming Variables

There are some basic rules when it comes to variable naming. As we continue your journey in programming, you will gradually get to know the reserved keywords (Easier to read the compilation error than to memorize the whole list). But what I would like to point out to you now is some rules which comes with giving a variable names.

Do(s) & Don't(s)

  • Always start the variable’s name with a letter. 
  • Any letter or number can be used in the name.
  • Do not use spaces. An underscore ( _ ) can be used instead.
  • Symbols such as  / # @ aren’t allowed.
  • Variables are case sensitive. Uppercase (capital) and lowercase letters are different.


Data Type

The Arduino programming language inherited most of its core features from the C language, thus along came the requirement to declare a data type for every variable created.

The following table provides all the data types that you will use during Arduino programming.

void Boolean char unsigned char
byte int unsigned int word
long unsigned long short float
double [Array] [String]

void -
The void keyword is normally only used in function declarations. It indicates that the function is not expected to return any information.

Boolean -
A Boolean holds one of two values, true or false. Each Boolean variable occupies one byte of memory.

Char -
A data type that takes up one byte of memory that stores a character value. 
Please refer to a ascii table for the conversion between the alphanumerical to its numeric representation.
 https://www.asciitable.com

unsigned char/byte -
Unsigned char is an unsigned data type that occupies one byte of memory. The unsigned char data type encodes numbers from 0 to 255.

int -
Integers are the primary data-type for number storage. int stores a 16-bit (2-byte) value.
This yields a range of -32,768 to 32,767.

 The int size can varies from device to device.

Long - 
Long variables are extended size variables for number storage, and store 32 bits (4 bytes).
Range from -2,147,483,648 to 2,147,483,647.

unsigned long / word -
Unsigned long variables are extended size variables for number storage and store 32 bits (4 bytes). 
Range from 0 to 4,294,967,295.

short -
A short is a 16-bit (2-byte) value. 
Range from -32,768 to 32,767.

float -
Data type for floating-point number is a number that has a decimal point. 
Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information.

double -
In Arduino, the Double precision floating-point number occupies four bytes just like the float data type.

Example - Fading LED (Breathing):

Instead of just blinking the LED like a Robot (no punt intended). We will flash/control the LED with a greater finest this round.
The simplest method of creating the so-called breathing LED is by linearly sweeping from minimum to maximum, producing an output similar to a triangle wave.

A triangle wave can be written in equation form as follows:



where x represents the input from 0 - N, with N acting as the total number of discreet points per cycle. 

 What is the range of values for y?


How do we vary the brightness of the LED ?

For a digital processor (without DAC) such as our ESP32-C3 processor to vary the LED's brightness, we will employ a technique known as PWM (Pulse width modulation).

PWM toggles the IO pin rapidly with varying duty cycles to give an average voltage lower than the supply voltage source (3v3).
Have a look at the illustration below showing the different duty cycles & the apparent average voltage as seen by the device.






 PWM Output on Arduino ?

To generate the PWM signal through the Arduino C programming language, we are going to use a function call analogWrite(). We are going to formally introduce the concept of function in Chapter 8 (Hold your horses ). 


 Todo:

  • Can you modify the above triangle breathing light for a circular wave instead?
    (The formulae for a circular wave is as shown below)





    Which do you prefer? and Why?

  • (Advance) The most interesting & convincing breathing light would most likely be driven by a Gaussian functions.
    Gaussian function is being used in many STEM & statistic studies. If you have used Photoshop to do image touch-up, you would have undoubtedly use Gaussian functions for blurring/smoothing.



    Have a read of the wikipedia entry for Gaussian function:
     https://en.wikipedia.org/wiki/Gaussian_function

    And try to come out with your own implementation of the breathing LED using the Gaussian formulae.




    Gamma affects the slop of the gaussian curve. AKA more On vs Off.
    We find 0.13 -> 0.15 (more On time)works the best.