Jedes mal, wenn sich etwas dem Infrarot-Sensor nähert, wird die im 7-Segmente-Display angezeigte Zahl um eins größer.
Materialien:
- Steckplatine
- Infrarot-Sensor-Modul
- Dual 7-Segmente-Display
- Widerstände (220Ω)
- Arduino UNO
- Kabel
- 9V-Batterie + Batteriestecker
Videos:
Hardware:
Achte auf das Datenblatt, jede Anzeige (Display) hat eine eigene Pinbelegung. Wir benutzen folgendes zweistellige 7-Segment-Display mit gemeinsamer Anode (3261B):
Display-Segmente A … G (LEDs) für jede Ziffernstelle (Dig.1 und Dig.2):
Jedes Segment (A … G) ist eine LED. Wir steuern die Kathoden dieser LEDs, um die gewünschte Zahl anzuzeigen. Aus der Graphik oben können wir sehen, dass:
Wir wählen die anzuzeigende Stelle (Zehner oder Einer) mit „Multiplexing“: dabei werden die Anoden so schnell an- und ausgeschaltet, dass es scheint, als würden beide Stellen konstant leuchten.
Jede Kathode braucht einen Widerstand:
Arduino Verbindungen:
Der Infrarot-Sensor ist mit Analog-Eingang A0 verbunden. Die Pins des 7-Segment-Displays zum Arduino sind so verbunden:
Display-Segment ON (rot) / OFF (weiß):
Wir benutzen einen Variablen-Typ „Array“:
https://www.arduino.cc/reference/de/language/variables/data-types/array/
Code:
/**** Counter using a dual 7-Segment-Display 3261B (small) with common anodes and an Infrared Sensor Module.*****/
/***** MStronik.blog *****************************************************************************/
// Define the cathode pins for the 7-Segment Display:
#define a 3 //pin 3 in SSD
#define b 9 //pin 9 in SSD
#define c 8 // pin 8 in SSD
#define d 6 //pin 6 in SSD
#define e 7 //pin 7 in SSD
#define f 4 //pin 4 in SSD
#define g 2 //pin 1 in SSD
// define the anodes:
int Dig1 = 5;
int Dig2 = 10;
int Seg_Array[7] = {a,b,c,d,e,f,g};
int SensorPin = A0;
long SensorValue, last_SensorVal;
int counter, units, tens;
bool flag_count, last_state;
unsigned long start_time;
void setup() {
counter = 0;
last_SensorVal = 0;
flag_count = LOW;
pinMode(SensorPin, INPUT);
for( int i = 0; i<7;i++){
pinMode(Seg_Array[i], OUTPUT);
}
pinMode(Dig1, OUTPUT);
pinMode(Dig2, OUTPUT);
all_OFF();
//Serial.begin(9600);
}
void loop() {
Tens = int(counter/10);
Units = int(counter%10);
start_time = millis();
for(unsigned long t_elapsed = 0; t_elapsed < 700; t_elapsed = millis()-start_time){
show_Units(Units);
delay(5);
show_Tens(Tens);
delay(5);
}
SensorValue = analogRead(SensorPin);
//Serial.println(SensorValue);
//delay(500);
if ((SensorValue < 70 )||(flag_count != last_state)){ // if the sensor is active
flag_count = true;
counter++;
if(counter > 99)
counter = 0;
}
else{
flag_count = false;
}
last_state = flag_count;
}
/***************** FUNCTIONS ******************************************/
void show_Tens(int digit)
{
digitalWrite(Dig1, LOW);
digitalWrite(Dig2, HIGH);
show_Digit(digit);
}
void show_Units(int digit)
{
digitalWrite(Dig1, HIGH);
digitalWrite(Dig2, LOW);
show_Digit(digit);
}
// if cathode pin is HIGH: Segment will be OFF !
// Seg_Array[] = {a, b, c, d, e, f, g}
// index: 0, 1, 2, 3, 4, 5, 6
void show_Digit(int digit)
{int i;
switch (digit){
case 0:
all_ON();
// turn OFF Segment G:
digitalWrite(Seg_Array[6],HIGH);
break;
case 1:
all_OFF();
// turn ON Segment B and C:
digitalWrite(Seg_Array[1],LOW);
digitalWrite(Seg_Array[2],LOW);
break;
case 2:
all_ON();
// turn OFF Segment C and F:
digitalWrite(Seg_Array[2],HIGH);
digitalWrite(Seg_Array[5],HIGH);
break;
case 3:
all_ON();
// turn OFF Segment E and F:
digitalWrite(Seg_Array[4],HIGH);
digitalWrite(Seg_Array[5],HIGH);
break;
case 4:
all_ON();
// turn OFF Segment A, D and E:
digitalWrite(Seg_Array[0],HIGH);
digitalWrite(Seg_Array[3],HIGH);
digitalWrite(Seg_Array[4],HIGH);
break;
case 5:
all_ON();
// turn OFF Segment B and E:
digitalWrite(Seg_Array[1],HIGH);
digitalWrite(Seg_Array[4],HIGH);
break;
case 6:
all_ON();
// turn OFF Segment B:
digitalWrite(Seg_Array[1],HIGH);
break;
case 7:
all_OFF();
// turn ON Segment A, B and C:
digitalWrite(Seg_Array[0],LOW);
digitalWrite(Seg_Array[1],LOW);
digitalWrite(Seg_Array[2],LOW);
break;
case 8:
all_ON();
break;
case 9:
all_ON();
// turn OFF Segment E:
digitalWrite(Seg_Array[4],HIGH);
break;
default:
all_OFF();
break;
}
}
/*Alternative function for the 7-Segment-Display:
void Display_Num(int digit){
if((digit != 1)||(digit != 4))
{Null();
digitalWrite(Seg_Array[0],LOW);}
else if ((digit != 5)||(digit != 6))
{ Null();
digitalWrite(Seg_Array[1],LOW);}
else if ((digit != 2))
{ Null();
digitalWrite(Seg_Array[2],LOW);}
else if ((digit != 1)||(digit != 4)||(digit != 7))
{Null();
digitalWrite(Seg_Array[3],LOW);}
else if ((digit == 0)||(digit == 2)||(digit == 6)||(digit == 8))
{Null();
digitalWrite(Seg_Array[4],LOW);}
else if ((digit != 1)||(digit != 2)||(digit != 3)||(digit != 7))
{Null();
digitalWrite(Seg_Array[5],LOW);}
else if ((digit != 0)||(digit != 1)||(digit != 2)||(digit != 7))
{Null();
digitalWrite(Seg_Array[6],LOW);}
}*/
Download the code here for a diferent Dual 7-segment Display (5621BS):
2 Kommentare