TP1 P5K2 Up Uc




1. Foto Hardware dan Diagram Blok [Kembali]

  • Dip Switch
  • Arduino Uno
  • 7-segment
  • Resistor
  • Power supply
  • Ground


2. Prosedur Percobaan  [Kembali]

  • Rangkai semua komponen 
  • Buat program di aplikasi arduino IDE
  • Setelah selesai masukkan program ke arduino 
  • Jalankan program pada simulasi dan cobakan dengan modul



Rangkaian terdiri dari di input berupa dip switch 4 pin yang dihubungkan ke port analog A0-A5 pada arduino. Sebelum dihubungkan dengan arduino, keluaran dari dip switch masing - masing diparalelkan dengan resistor lalu digroundkan agar keluaran dip switch tidak selalu high. Sedangkan keluaran yaitu 7-segment pin/segment A-B dihubungkan ke pin 7-13 arduino, serta pin 1 dan 2 ke pin 6 dan 5 arduino.

Ketika jumlah switch on kurang dari dua maka hanya digit 2 yang akan melakukan counting dari 0-9.
Ketika jumlah switch on sama dengan dua maka kedua digit akan melakukan counting yang dilakuan secara bergantian.
Ketika jumlah switch on lebih dari dua maka hanya digit 1 yang akan melakukan counting dari 0-9.



4. Flowchart dan Listing Program [Kembali]





// Define pin numbers
const int segPins[] = { 7, 8, 9, 10, 11, 12, 13}; // Pins for segments A, B, C, D, E, F, G
const int digitPins[] = {5, 6}; // Pins for digit selection
const int switchPins[] = {A1, A2, A3, A4}; // Pins for dip switches

// Define 7-segment display patterns for digits 0-9 (only segments A-G)
const int digits[10][7] = {
  {1, 1, 1, 1, 1, 1, 0}, // 0
  {0, 1, 1, 0, 0, 0, 0}, // 1
  {1, 1, 0, 1, 1, 0, 1}, // 2
  {1, 1, 1, 1, 0, 0, 1}, // 3
  {0, 1, 1, 0, 0, 1, 1}, // 4
  {1, 0, 1, 1, 0, 1, 1}, // 5
  {1, 0, 1, 1, 1, 1, 1}, // 6
  {1, 1, 1, 0, 0, 0, 0}, // 7
  {1, 1, 1, 1, 1, 1, 1}, // 8
  {1, 1, 1, 1, 0, 1, 1}  // 9
};

void setup() {
  // Initialize the dip switch pins as inputs
  for (int i = 0; i < 4; ++i) {
    pinMode(switchPins[i], INPUT_PULLUP);
  }

  // Initialize the 7-segment display pins as outputs
  for (int i = 0; i < 7; ++i) {
    pinMode(segPins[i], OUTPUT);
  }
  for (int i = 0; i < 2; ++i) {
    pinMode(digitPins[i], OUTPUT);
  }
}

void loop() {
  // Read the states of the dip switches
  int switchStates[4];
  for (int i = 0; i < 4; ++i) {
    switchStates[i] = digitalRead(switchPins[i]);
  }

  // Check if the first two switches are active
  if (switchStates[0] == LOW && switchStates[1] == LOW) {
    // Counting for digit 1
    countAndDisplay(0);
  } else if (switchStates[0] == HIGH && switchStates[1] == HIGH) {
    // Counting for digit 2
    countAndDisplay(1);
  }

  // Check if the next two switches are active
  if (switchStates[2] == LOW && switchStates[3] == LOW) {
    // Counting for digit 1
    countAndDisplay(0);
  } else if (switchStates[2] == HIGH && switchStates[3] == HIGH) {
    // Counting for digit 2
    countAndDisplay(1);
  }
}

// Function to count and display on the selected digit
void countAndDisplay(int digitIndex) {
  static int count = 0; // Static variable to hold the count value
  displayDigit(count, digitIndex); // Display the current count on the selected digit
  count = (count + 1) % 10; // Increment count and wrap around at 10
  delay(500); // Delay for visual effect, adjust as needed
}

// Function to display a digit on a 7-segment display
void displayDigit(int digit, int digitIndex) {
  // Turn off all digits
  for (int i = 0; i < 2; ++i) {
    digitalWrite(digitPins[i], HIGH);
  }
  // Select the desired digit
  digitalWrite(digitPins[digitIndex], LOW);

  // Display only segments A-G for the digit
  for (int i = 0; i < 7; ++i) {
    digitalWrite(segPins[i], digits[digit][i]);
  }
}


5. Kondisi [Kembali]

Setiap 2 switch yang aktif akan melakukan counting 0-9 pada digit 1 dan 2 secara berseling











Tidak ada komentar:

Posting Komentar

M4

[KEMBALI KE MENU SEBELUMNYA] DAFTAR ISI 1. Pendahuluan 2. Tujuan 3. Alat dan Bahan 4. Dasar Teori Percobaan a. ...