Here you can find my answers to the exercises and practice proposed at page Arduino [Unit 1.1] - Blink. You should read that article and try to solve the exercises and practical proposals in the laboratory before reading the answers on this page.

 

♦ Exercise 1
Modify the code so that the light is on for 100 ms (milliseconds) and off for 900 ms. Write the code.
Answer

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(100);                  // waits for 0.1 second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(900);                  // waits for 0.9 second
}

 

♦ Exercise 2
Modify the code so that the light is on for 50 ms and off for 50 ms. What happens?

Answer
Intense strobe action


♦ Exercise 3
Modify the code so that the light is on for 10 ms and off for 10 ms. What happens?

Answer
The light is no longer blinking


♦ Exercise 4
Modify the code so that the LED is connected on pin 10.

Answer

#define ledPin 10


Arduino + 7 segments displayPractice 2
Program an Arduino sketch to represent numbers on a 7 segment display.

Answer
In the schematic we omit 7 LEDs protection resistors, but they are needed because Arduino emits a current of about 200 mA, distributed over all LEDs. Estimating to have at most 10mA per LED, it is necessary a protection resistor for each of them. We can use 200 kΩ.

Display common cathode has this pin connected to ground, instead the anodes of the LEDs are connected to 8 digital pins of Arduino:  a with pin 3, b with pin 4, c with pin 5, d with pin 6, e with pin 7, f with pin 8, g with pin 9, dp not connected.
Display common cathode has this pin connected to ground, instead the anodes of the LEDs are connected to 8 digital pins of Arduino:  a with pin 3, b with pin 4, c with pin 5, d with pin 6, e with pin 7, f with pin 8, g with pin 9, dp is not connected.
Arduino enables one anode of the display from time to time according to send the number to be displayed. This will exploit the persistence of the image on the retina of the eye to create the illusion that the numbers on the display seem fixed, while in reality the LEDs are updated quickly in sequence.
To turn on, from time to time, one LED of the display, Arduino must send the respective anode at 5 V. The current sent from the microcontroller is sufficient to illuminate the display.
The sketch is shown below. It uses a lookup table. We store the coding of each of the ten digits from 0 to 9 in a vector. The coding of each digit is made on one byte, but only seven bits are needed: bit = 1 means LED on, instead  bit = 0 indicates LED off.
In the for loop we send the coding, bit after bit, in output towards display, using digitalWrite() function that sends only the least significative bit. Single ampersand, &, used between coding and 1 operates like Bxxxxxxxx AND B00000001 on each bit position of the expression, but only the last position is important: if both least significative bits are 1, the resulting output is 1, otherwise the output is 0. digitalWrite() translates 1 = HIGH (5 V) and 0 = LOW (0 V).
In the for loop the right shift operator >> causes the bits in the left operand to be shifted right by 1 position per cicle, specified by the right operand.

/*  g  f  kc a  b        (pin  3)  ->  a
    |  |  |  |  |        (pin  4)  ->  b
    -------------        (pin  5)  ->  c
    |     a     |        (pin  6)  ->  d
    |    ---    |        (pin  7)  ->  e
    | f | g | b |        (pin  8)  ->  f
    |    ---    |        (pin  9)  ->  g
    | e |   | c |         kc       ->  GND
    |    ---    |         dp       ->  NC
    |     d     |
    -------------
    |  |  |  |  |
    e  d  kc c  dp */

int a=3;
int b=4;
int c=5;
int d=6;
int e=7;
int f=8;
int g=9;

  // Lookup table per display common cathode: B0gfedcba
byte lookup_7segmenti_catodocomune[10] = {
  B00111111, B00000110, B01011011, B01001111, B01100110,
  B01101101, B01111101, B00000111, B01111111, B01101111 };
 
void setup(){
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
}

void displayWriteSPA(int numero)
{
 for (int i=a; i<=g; i++)
 {
   digitalWrite(i,numero&1);
   numero = numero >> 1;
 }
}

void loop(){
    // test print number 4
    displayWriteSPA(lookup_7segmenti_catodocomune[4]);
  }
}

Advertising

Advertising

Advertising