Variables are ways in which we store values of certain types.
#include <stdio.h> int main(void) { int variable_number = 10; char variable_character = 'c'; float variable_fractional_number = 10.25; return 0; }
Arrays are complex variables of a certain size that can store a list of values. Example:
#include <stdio.h> int main(void) { int arr1[6]; int arr2[] = { 1,2,3,4,5,6}; char arr3[] = {'a', 'b', 'c', 'd'}; char arr4[6] = "hello"; return 0; }
The if statement is a conditional sentence used to execute instructions based on certain conditions. The if-else construction translates to “if - otherwise”:
#include <stdio.h> int main(void) { int a = 10; if (a == 10) { //if a is equal to 10 // do operations when a is 10 } else { // Else // do operations when a is different from 10 } return 0; }
The for structure is a repetitive structure dependent on a condition. Usage:
Accessing a certain value in an array is done by the index (position) of the value in the array.
The functions divide complex tasks into small pieces that are easier to understand and to program. These can be reused for other occasions, instead of being rewritten from scratch. Also, the functions are useful for hiding the operating details of certain parts of the program, helping in its working mode. Using functions, which represent the fundamental unit of execution of C programs, a logical division of large and complex programs is obtained.
To use the pins of the Arduino, we need to set the way we want to use them (INPUT/OUTPUT). Example:
void setup() { pinMode(MY_PIN_IN, INPUT); pinMode(MY_PIN_OUT, OUTPUT); } void loop() { }
If we set a pin to OUTPUT mode, we can now output voltage to this pin using digitalWrite(). Example:
void setup() { pinMode(MY_PIN_IN, INPUT); pinMode(MY_PIN_OUT, OUTPUT); } void loop() { digitalWrite(MY_PIN_OUT, HIGH); //on this pin we now have 5V delay(3000); // wait 3 seconds digitalWrite(MY_PIN_OUT, LOW); //now we have 0V on this pin }
All exercises will be performed on Tinkercad