0% found this document useful (0 votes)
8 views7 pages

Revs

The document outlines various exercises related to the operation of seven-segment displays (SSD) using Arduino programming. It includes code examples for normal operations with common anode and common cathode configurations, as well as implementations for incrementing and decrementing values using buttons. Additionally, it describes the setup and loop functions necessary for displaying digits on the SSD.

Uploaded by

2022303190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Revs

The document outlines various exercises related to the operation of seven-segment displays (SSD) using Arduino programming. It includes code examples for normal operations with common anode and common cathode configurations, as well as implementations for incrementing and decrementing values using buttons. Additionally, it describes the setup and loop functions necessary for displaying digits on the SSD.

Uploaded by

2022303190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DIGI ELECS REVIEW {

LAB 4 Ex. 1 SSD NORMAL OPERATION digitalWrite(a,1);


COMMON ANODE digitalWrite(b,1);
int a=2; digitalWrite(c,1);
int b=3; digitalWrite(d,1);
int c=4; digitalWrite(e,1);
int d=5; digitalWrite(f,1);
int e=6; digitalWrite(g,1);
int f=7; }
int g=8;
void loop() {
void setup() { for(int i=0;i<10;i++)
pinMode(a, OUTPUT); {
pinMode(b, OUTPUT); displayDigit(i);
pinMode(c, OUTPUT); delay(1000);
pinMode(d, OUTPUT); turnOff();
pinMode(e, OUTPUT); }
pinMode(f, OUTPUT); }
pinMode(g, OUTPUT);
} LAB 4 Ex. 2 SSD NORMAL OPERATION
void displayDigit(int digit) COMMON CATHODE SWITCH CASE
{
// for segment a #define segA 2//connecting segment A to PIN2
if(digit != 1 && digit != 4) #define segB 3// connecting segment B to PIN3
digitalWrite(a, 0); #define segC 4// connecting segment C to PIN4
#define segD 5// connecting segment D to PIN5
// for segment b #define segE 6// connecting segment E to PIN6
if(digit != 5 && digit != 6) #define segF 7// connecting segment F to PIN7
digitalWrite(b, 0); #define segG 8// connecting segment G to PIN8

// for segment c int COUNT=0;//count integer for 0-9 increment


if(digit != 2) void setup()
digitalWrite(c, 0); {
for (int i=2;i<9;i++){
// for segment d pinMode(i, OUTPUT);// taking all pins from 2-8 as
if(digit != 1 && digit != 4 && digit !=7) output
digitalWrite(d, 0); }
}
// for segment e
if(digit == 2 || digit == 6 void loop()
|| digit == 8 || digit == 0) {
digitalWrite(e, 0); switch (COUNT)
{
// for segment f
if(digit != 1 && digit != 2 && digit != 3 && digit != case 0://when count value is zero show”0” on
7) disp
digitalWrite(f, 0); digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
// for segment g digitalWrite(segC, HIGH);
if(digit != 1 && digit !=7 && digit != 0) digitalWrite(segD, HIGH);
digitalWrite(g, 0); digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
} digitalWrite(segG, LOW);
break;
void turnOff()
case 1:// when count value is 1 show”1” on disp digitalWrite(segD, HIGH);
digitalWrite(segA, LOW); digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segB, HIGH); digitalWrite(segG, HIGH);
digitalWrite(segC, HIGH); break;
digitalWrite(segD, LOW);
digitalWrite(segE, LOW); case 7:// when count value is 7 show”7” on disp
digitalWrite(segF, LOW); digitalWrite(segA, HIGH);
digitalWrite(segG, LOW); digitalWrite(segB, HIGH);
break; digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
case 2:// when count value is 2 show”2” on disp digitalWrite(segE, LOW);
digitalWrite(segA, HIGH); digitalWrite(segF, LOW);
digitalWrite(segB, HIGH); digitalWrite(segG, LOW);
digitalWrite(segC, LOW); break;
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH); case 8:// when count value is 8 show”8” on disp
digitalWrite(segF, LOW); digitalWrite(segA, HIGH);
digitalWrite(segG, HIGH); digitalWrite(segB, HIGH);
break; digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
case 3:// when count value is 3 show”3” on disp digitalWrite(segE, HIGH);
digitalWrite(segA, HIGH); digitalWrite(segF, HIGH);
digitalWrite(segB, HIGH); digitalWrite(segG, HIGH);
digitalWrite(segC, HIGH); break;
digitalWrite(segD, HIGH);
digitalWrite(segE, LOW); case 9:// when count value is 9 show”9” on disp
digitalWrite(segF, LOW); digitalWrite(segA, HIGH);
digitalWrite(segG, HIGH); digitalWrite(segB, HIGH);
break; digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
case 4:// when count value is 4 show”4” on disp digitalWrite(segE, LOW);
digitalWrite(segA, LOW); digitalWrite(segF, HIGH);
digitalWrite(segB, HIGH); digitalWrite(segG, HIGH);
digitalWrite(segC, HIGH); break;
digitalWrite(segD, LOW);
digitalWrite(segE, LOW); break;
digitalWrite(segF, HIGH); }
digitalWrite(segG, HIGH); if (COUNT<10)
break; {
COUNT++;
case 5:// when count value is 5 show”5” on disp delay(1000);///increment count integer for every
digitalWrite(segA, HIGH); second
digitalWrite(segB, LOW); }
digitalWrite(segC, HIGH); if (COUNT==10)
digitalWrite(segD, HIGH); {
digitalWrite(segE, LOW); COUNT=0;// if count integer value is equal to 10,
digitalWrite(segF, HIGH); reset it to zero.
digitalWrite(segG, HIGH); delay(1000);
break; }
}
case 6:// when count value is 6 show”6” on disp
digitalWrite(segA, HIGH); LAB 4 Ex. 3 2 SSD ONE ANODE ONE CATHODE
digitalWrite(segB, LOW); ONES ANODE (9-13) , TENS CATHODE(2-8)
digitalWrite(segC, HIGH);
void setup() { const int b = 3; //For displaying segment "b"
for (int i = 2; i <= 15; i++) { const int c = 4; //For displaying segment "c"
pinMode(i, OUTPUT); // Set pins 2 to A1 (15) as const int d = 5; //For displaying segment "d"
OUTPUT const int e = 6; //For displaying segment "e"
} const int f = 7; //For displaying segment "f"
} const int g = 8; //For displaying segment "g"
bool bPress = false;
// 7-segment encoding for digits 0-9 const int IncbuttonPin = 9;
// Common Cathode: HIGH = ON, LOW = OFF const int DecbuttonPin = 10;
const int number[10][7] = { // Variables will change:
{1,1,1,1,1,1,0}, // 0 int buttonPushCounter = 0; // counter for the
{0,1,1,0,0,0,0}, // 1 number of button presses
{1,1,0,1,1,0,1}, // 2 int IncbuttonState = 0; // current state of the
{1,1,1,1,0,0,1}, // 3 button
{0,1,1,0,0,1,1}, // 4 int lastIncbuttonState = 0; // previous state of
{1,0,1,1,0,1,1}, // 5 the button
{1,0,1,1,1,1,1}, // 6 int DecbuttonState = 0; // current state of the
{1,1,1,0,0,0,0}, // 7 button
{1,1,1,1,1,1,1}, // 8 int lastDecbuttonState = 0; // previous state of
{1,1,1,0,0,1,1} // 9 the button
}; void setup() {
// put your setup code here, to run once:
void loop() { pinMode(a, OUTPUT); //A
for (int tens = 0; tens < 10; tens++) { pinMode(b, OUTPUT); //B
display_tens(tens); // Show tens digit pinMode(c, OUTPUT); //C
(Common Anode) pinMode(d, OUTPUT); //D
for (int ones = 0; ones < 10; ones++) { pinMode(e, OUTPUT); //E
display_ones(ones); // Show ones digit pinMode(f, OUTPUT); //F
(Common Cathode) pinMode(g, OUTPUT); //G
delay(300); // Delay to view the number pinMode( IncbuttonPin , INPUT_PULLUP );
} pinMode( DecbuttonPin , INPUT_PULLUP );
} displayDigit(buttonPushCounter);
} }

// Function to display the tens digit (Common void loop() {


Anode SSD) IncbuttonState = digitalRead(IncbuttonPin);
void display_tens(int tens) { DecbuttonState = digitalRead(DecbuttonPin);
for (int i = 0; i < 7; i++) { checkIncButtonPress();
digitalWrite(2 + i, !number[tens][i]); // Invert checkDecButtonPress();
logic for common anode if( bPress ){
} bPress = false;
} turnOff();
displayDigit(buttonPushCounter);
// Function to display the ones digit (Common }
Cathode SSD) }
void display_ones(int ones) {
for (int i = 0; i < 7; i++) {
digitalWrite(9 + i, number[ones][i]); // Normal void checkIncButtonPress() {
logic for common cathode // compare the IncbuttonState to its previous
} state
} if (IncbuttonState != lastIncbuttonState) {
// if the state has changed, increment the
LAB 4 Ex. 4 SSD WITH 2 BUTTONS INC AND counter
DEC USING COMMON CATHODE if (IncbuttonState == LOW) {
const int a = 2; //For displaying segment "a"
// if the current state is HIGH then the button digitalWrite(e,HIGH);
went from off to on: }
bPress = true; //Conditions for displaying segment f
buttonPushCounter++; if(digit != 1 && digit !=2 && digit!=3 && digit !=7) {
if( buttonPushCounter > 9){ digitalWrite(f,HIGH);
buttonPushCounter =0 ; }
} // Delay a little bit to avoid bouncing delay(50); if (digit!=0 && digit!=1 && digit !=7) {
delay(50); digitalWrite(g,HIGH);
}
} }
// save the current state as the last state, for
next time through the loop void turnOff() {
lastIncbuttonState = IncbuttonState; digitalWrite(a,LOW);
} digitalWrite(b,LOW);
} digitalWrite(c,LOW);
digitalWrite(d,LOW);
void checkDecButtonPress(){ digitalWrite(e,LOW);
// compare the IncbuttonState to its previous digitalWrite(f,LOW);
state digitalWrite(g,LOW);
if (DecbuttonState != lastDecbuttonState) { }
// if the state has changed, increment the
counter LAB 4 P1 – 4 BUTTONS
if (DecbuttonState == LOW) { • Button 1 (Display 10,20,30,40,50,60,70,80,90)
// if the current state is HIGH then the button • Button 2(Display 00 to 99)
went from off to on: • Button 3 (Increment)
bPress = true; • Button 4 (Decrement)
buttonPushCounter--; 2 COMMON CATHODE
if( buttonPushCounter < 0){ 2 SSD SAME CONFIG WITH EX 3.
buttonPushCounter = 9;
} // Delay a little bit to avoid bouncing delay(50); const int a1 = 2;
delay(50); const int b1 = 3;
} const int c1 = 4;
// save the current state as the last state, for const int d1 = 5;
next time through the loop const int e1 = 6;
lastDecbuttonState = DecbuttonState; const int f1 = 7;
} const int g1 = 8;
} const int a2 = 9;
void displayDigit(int digit) { const int b2 = 10;
//Conditions for displaying segment a const int c2 = 11;
if(digit!=1 && digit != 4) { const int d2 = 12;
digitalWrite(a,HIGH); const int e2 = A0;
} const int f2 = A1;
//Conditions for displaying segment b const int g2 = A2;
if(digit != 5 && digit != 6) { const int ModeButton1 = A3;
digitalWrite(b,HIGH); const int ModeButton2 = A4;
} const int IncbuttonPin = A5;
//Conditions for displaying segment c const int DecbuttonPin = 13;
if(digit !=2) { int buttonPushCounter = 0;
digitalWrite(c,HIGH); int mode = 0;
} void setup() {
if(digit != 1 && digit !=4 && digit !=7) { int segmentPins1[] = {a1, b1, c1, d1, e1, f1, g1};
digitalWrite(d,HIGH); int segmentPins2[] = {a2, b2, c2, d2, e2, f2, g2};
} for (int i = 0; i < 7; i++) {
//Conditions for displaying segment e pinMode(segmentPins1[i], OUTPUT);
if(digit == 2 || digit ==6 || digit == 8 || digit==0){ pinMode(segmentPins2[i], OUTPUT);
} {1, 0, 1, 1, 0, 1, 1}, // 5
pinMode(ModeButton1, INPUT); {1, 0, 1, 1, 1, 1, 1}, // 6
pinMode(ModeButton2, INPUT); {1, 1, 1, 0, 0, 0, 0}, // 7
pinMode(IncbuttonPin, INPUT); {1, 1, 1, 1, 1, 1, 1}, // 8
pinMode(DecbuttonPin, INPUT); {1, 1, 1, 1, 0, 1, 1} // 9
displayNumber(buttonPushCounter); };
}
void loop() { digitalWrite(a, segments[digit][0]);
checkModeButtons(); digitalWrite(b, segments[digit][1]);
checkButtonPress(IncbuttonPin, 1); digitalWrite(c, segments[digit][2]);
checkButtonPress(DecbuttonPin, -1); digitalWrite(d, segments[digit][3]);
} digitalWrite(e, segments[digit][4]);
void checkButtonPress(int buttonPin, int digitalWrite(f, segments[digit][5]);
increment) { digitalWrite(g, segments[digit][6]);
}
if (digitalRead(buttonPin) == HIGH) { void turnOff() {
buttonPushCounter += (mode == 1) ? increment int segmentPins1[] = {a1, b1, c1, d1, e1, f1, g1};
* 10 : increment; int segmentPins2[] = {a2, b2, c2, d2, e2, f2, g2};
if (buttonPushCounter > 99) buttonPushCounter for (int i = 0; i < 7; i++) {
= 0; digitalWrite(segmentPins1[i], LOW);
if (buttonPushCounter < 0) buttonPushCounter = digitalWrite(segmentPins2[i], LOW);
99; }
turnOff(); }
displayNumber(buttonPushCounter);
delay(200); LAB 4 P2 – Utilize digital pins for the SSD. Use
} analog pins for the push buttons. When button
} 1 is pressed, display ones 0 to 9. When button
void checkModeButtons() { 2 is pressed, display 00 to 99. When button 3 is
if (digitalRead(ModeButton1) == HIGH) { pressed, ones will start count up from 0 to 9,
mode = 1; // Mode for 10, 20, 30... and tens will count down from 9 to 0,
buttonPushCounter = 10; simultaneuously. When button 4 is pressed, 00
delay(200); display will continually blink for 10 times.
} Delay 500 ms.
if (digitalRead(ModeButton2) == HIGH) {
mode = 2; // Mode for 00-99 const int segmentPins1[] = {8, 9, 4, 5, 6, 2, 3};
buttonPushCounter = 0; const int segmentPins2[] = {10, 11, 12, A0, A1,
delay(200); A2, A3};
} const int btn1 = A4; // Button to start counting
turnOff(); const int btn2 = A5;
displayNumber(buttonPushCounter); const int btn3 = 7;
} const int btn4 = 13;
void displayNumber(int number) { int ones = 0, tens = 0;
int tens = number / 10; const int numbers[10][7] = {
int ones = number % 10; {1, 1, 1, 1, 1, 1, 0}, // 0
displayDigit(a1, b1, c1, d1, e1, f1, g1, tens); {0, 1, 1, 0, 0, 0, 0}, // 1
displayDigit(a2, b2, c2, d2, e2, f2, g2, ones); {1, 1, 0, 1, 1, 0, 1}, // 2
} {1, 1, 1, 1, 0, 0, 1}, // 3
void displayDigit(int a, int b, int c, int d, int e, int f, {0, 1, 1, 0, 0, 1, 1}, // 4
int g, int digit) { {1, 0, 1, 1, 0, 1, 1}, // 5
int segments[10][7] = { {1, 0, 1, 1, 1, 1, 1}, // 6
{1, 1, 1, 1, 1, 1, 0}, // 0 {1, 1, 1, 0, 0, 0, 0}, // 7
{0, 1, 1, 0, 0, 0, 0}, // 1 {1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 0, 1, 1, 0, 1}, // 2 {1, 1, 1, 1, 0, 1, 1} // 9
{1, 1, 1, 1, 0, 0, 1}, // 3 };
{0, 1, 1, 0, 0, 1, 1}, // 4 void setup() {
for (int i = 0; i < 7; i++) { }
pinMode(segmentPins1[i], OUTPUT);
pinMode(segmentPins2[i], OUTPUT); LAB 4 P3 - Connect SSD and LED to digital pins
} of the Arduino. Use button 1 to start the
program. SSD will display from 0 to 9. Then it
pinMode(btn1, INPUT); will also displayed simulateneously by the LED
} binary equivalent. Use a delay of 1s
void loop() {
if (digitalRead(btn1) == HIGH) { const int buttonPin = 2;
for (tens = 0; tens < 10; tens++) { const int ssdPins[7] = {3, 4, 5, 6, 7, 8, 9};
displayNumber(0, tens); const int ledPins[4] = {10, 11, 12, 13};
delay(500); int numbers[10][7] = {
digitalWrite(tens, LOW); {1, 1, 1, 1, 1, 1, 0}, // 0
} {0, 1, 1, 0, 0, 0, 0}, // 1
} {1, 1, 0, 1, 1, 0, 1}, // 2
if (digitalRead(btn2) == HIGH) { {1, 1, 1, 1, 0, 0, 1}, // 3
for (ones = 0; ones < 10; ones++) { {0, 1, 1, 0, 0, 1, 1}, // 4
for (tens = 0; tens < 10; tens++) { {1, 0, 1, 1, 0, 1, 1}, // 5
displayNumber(ones, tens); {1, 0, 1, 1, 1, 1, 1}, // 6
delay(500); {1, 1, 1, 0, 0, 0, 0}, // 7
} {1, 1, 1, 1, 1, 1, 1}, // 8
} {1, 1, 1, 1, 0, 1, 1} // 9
} };
if (digitalRead(btn3) == HIGH) { void setup() {
for (int i = 0; i < 10; i++) { pinMode(buttonPin, INPUT); for (int i = 0; i < 7; i++)
displayNumber(i, 9 - i); {
delay(500); pinMode(ssdPins[i], OUTPUT);
} }
} for (int i = 0; i < 4; i++) {
if (digitalRead(btn4) == HIGH) { pinMode(ledPins[i], OUTPUT);
for (int i = 0; i < 10; i++) { }
displayNumber(0, 0); }
delay(500); void loop() {
clearDisplay(); if (digitalRead(buttonPin) == HIGH) {
delay(500); for (int num = 0; num < 10; num++) {
} displayNumber(num);
} displayBinary(num);
} delay(1000);
void displayNumber(int ones, int tens) { }
setSegments(segmentPins1, tens); // Display }
ones on SSD1 }
setSegments(segmentPins2, ones); // Display void displayNumber(int num) {
tens on SSD2 for (int i = 0; i < 7; i++) {
}
void setSegments(const int pins[], int num) { digitalWrite(ssdPins[i], numbers[num][i]);
for (int i = 0; i < 7; i++) { }
digitalWrite(pins[i], numbers[num][i]); }
} void displayBinary(int num) {
} for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[i], (num >> i) & 1);
void clearDisplay() { }
for (int i = 0; i < 7; i++) { }
digitalWrite(segmentPins1[i], LOW);
digitalWrite(segmentPins2[i], LOW);
}
ADDITIONAL KERBY paused = true;
4PBs + 1SSD + 4LEDs Conditions PB1 – incrementing = false;
continuous increment PB2 – continuous decrementing = false;
decrement PB3 – pause PB4 – reset delay(200);
int btnIncP = A0; // Increment button }
int btnDecP = A1; // Decrement button if (digitalRead(btnResetP) == HIGH) {
int btnPauseP = A2; // Pause button currentNumber = 0;
int btnResetP = A3; // Reset button paused = false;
const int ssdPins[] = {2, 3, 4, 5, 6, 7, 8}; // 7- incrementing = false;
segment display pins decrementing = false;
const int ledPins[] = {9, 10, 11, 12}; // LED pins for displayNumber(currentNumber);
binary display displayBinary(currentNumber);
const int numbers[10][7] = { // 7-segment digit delay(200);
patterns }
{1,1,1,1,1,1,0}, // 0 if (!paused) {
{0,1,1,0,0,0,0}, // 1 if (incrementing && currentNumber < 9) {
{1,1,0,1,1,0,1}, // 2 currentNumber++;
{1,1,1,1,0,0,1}, // 3 displayNumber(currentNumber);
{0,1,1,0,0,1,1}, // 4 displayBinary(currentNumber);
{1,0,1,1,0,1,1}, // 5 delay(200);
{1,0,1,1,1,1,1}, // 6 }
{1,1,1,0,0,0,0}, // 7 if (decrementing && currentNumber > 0) {
{1,1,1,1,1,1,1}, // 8 currentNumber--;
{1,1,1,1,0,1,1} // 9 displayNumber(currentNumber);
}; displayBinary(currentNumber);
int currentNumber = 0; delay(200);
bool incrementing = false; }
bool decrementing = false; }
bool paused = false; }
void setup() { void displayNumber(int num) {
pinMode(btnIncP, INPUT); for (int i = 0; i < 7; i++)
pinMode(btnDecP, INPUT); digitalWrite(ssdPins[i],numbers[num][i]);
pinMode(btnPauseP, INPUT); }
pinMode(btnResetP, INPUT); void displayBinary(int num) {
for (int i = 0; i < 7; i++) pinMode(ssdPins[i], for (int i = 0; i < 4; i++) digitalWrite(ledPins[i],
OUTPUT); (num
for (int i = 0; i < 4; i++) pinMode(ledPins[i], >> i) & 1);
OUTPUT); }
displayNumber(currentNumber);
displayBinary(currentNumber);
}
void loop() {
if (digitalRead(btnIncP) == HIGH) {
incrementing = true;
decrementing = false;
paused = false;
delay(200);
}
if (digitalRead(btnDecP) == HIGH) {
decrementing = true;
incrementing = false;
paused = false;
delay(200);
}
if (digitalRead(btnPauseP) == HIGH) {

You might also like