Indice
Descrizione
In questo video scoprirai come usare il display a 7 segmenti con Arduino.
Imparerai come visualizzare e modificare le cifre e come trasformare il display in un dado digitale.
#define DATA_PIN 4
#define LATCH_PIN 3
#define CLOCK_PIN 2
byte numbers[] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110 // 9
};
int n = 0;
void setup() {
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
}
void loop(){
n = (n+1) % 10;
writeRegister(numbers[n]);
delay(500);
}
void writeRegister(byte value){
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, value);
digitalWrite(LATCH_PIN, HIGH);
}
#define DATA_PIN 4
#define LATCH_PIN 3
#define CLOCK_PIN 2
#define BUTTON_PIN 6
byte numbers[] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110 // 9
};
int n = 0;
void setup() {
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
randomSeed(analogRead(A1));
writeRegister(0);
}
void loop(){
if (digitalRead(BUTTON_PIN) == HIGH) {
do {
displayNextNumber();
} while(digitalRead(BUTTON_PIN));
int r = random(20, 26);
for (int i = 0; i < r; i++) {
displayNextNumber();
}
writeRegister(numbers[n]);
}
delay(10);
}
void displayNextNumber() {
n = n % 6 + 1;
writeRegister(numbers[n] | B00000001);
delay(30);
}
void writeRegister(byte value){
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, value);
digitalWrite(LATCH_PIN, HIGH);
}
#define DATA_PIN 4
#define LATCH_PIN 3
#define CLOCK_PIN 2
#define PLUS_PIN 6
#define MINUS_PIN 7
#define POTENTIOMETER_PIN A0
byte numbers[] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110 // 9
};
int n = 0;
int lastPotentiometerValue = 0;
void setup() {
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(PLUS_PIN, INPUT);
pinMode(MINUS_PIN, INPUT);
}
void loop(){
updateNumber();
writeRegister(numbers[n]);
delay(10);
}
void updateNumber() {
int input = analogRead(POTENTIOMETER_PIN);
int potentiometerValue = input * 10 / 1024;
if (potentiometerValue != lastPotentiometerValue) {
n = potentiometerValue;
lastPotentiometerValue = potentiometerValue;
}
if (digitalRead(PLUS_PIN) == HIGH) {
n = (n+1) % 10;
delay(200);
} else if (digitalRead(MINUS_PIN) == HIGH) {
n--;
if (n < 0) {
n = 9;
}
delay(200);
}
}
void writeRegister(byte value){
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, value);
digitalWrite(LATCH_PIN, HIGH);
}