This is an old revision of the document!


Lab 02 - GPIO operations

Purpose

  • familiarity with operations related to arduino pins
  • familiarity with the basic concepts of programming

Keywords

  • pinMode, digitalWrite, HIGH, LOW, LED
  • for, if, while, vector

Programming concepts

Variables

Variables are ways in which we store values ​​of certain types.

variabile.c
#include <stdio.h>
 
int main(void) 
{
      int variable_number = 10;
      char variable_character = 'c';
      float variable_fractional_number = 10.25;
      return 0;
}

Arrays

Arrays are more intelligent variables of a certain size that can store a string of values ​​of each type above. Example:

variabile.c
#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] = "salut";
      return 0;
}

If - Else

The if statement is a conditional sentence used to execute instructions according to certain conditions if-else construction translates to “if - otherwise”:

if_else.c
#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 { // Altfel
          // do operations when a is different from 10
      }
      return 0;
}

For

The for structure is a repetitive structure depending on a condition. Way of ussage:

for.c
#include <stdio.h>
 
int main(void) 
{
      //for(initialization, condition, operation) {}
      int i;
      for (i = 0; i <= 10; i++) {
          printf("%d ", i); // show i;
      } 
      return 0;
 
      // The code above will display: 0 1 2 3 4 5 6 7 8 9 10
}

Iterate an array

Accessing a certain value in an array is done by the index (position) of the value in the array.

vector.c
#include <stdio.h>
 
int main(void) 
{
      int i;
      int arr[] = {1,2,3,4,5,6,7,8,9};
      int sum = 0;
 
      printf("%d\n", arr[2]); // show 3
 
      for (i = 0; i < 9; i++) { // the positions in arraystart from 0!!
 
          sum = sum + arr[i]; // the sum of all the elements in the array
      } 
 
      return 0;
}

Functions

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.

vector.c
#include <stdio.h>
 
void show(int number) {
    printf("%d\n", number);
}
 
int main(void) 
{
    int x = 10;
 
    show(10); // it will show 10
 
    return 0;
}

Arduino pins operations

To use the pins of an 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
}

Exercises

All exercises will be performed on Tinkercad

  1. Using an ARDUINO, a LED and a 220 ohm RESISTOR, turn on and off a led at a fixed interval of time.
  2. Simulate a car traffic light and a pedestrian traffic light to be synchronized. The traffic lights will be implemented using LEDs and will iterate an array.
  3. Connect to an ARDUINO a 7 SEGMENT DISPLAY and using helper functions, display each number on the display.

100

info2/laboratoare/lab2en.1583231956.txt.gz · Last modified: 2020/03/03 12:39 by liviu.moraru
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0