Minggu, 25 Desember 2022

Roll a Die using Arduino

 #define btnPin 7 // connected to button

#define grupA 8  // Arduino pin driving LED at center
#define grupB 9  // Ardn pin to 2 LEDs at diagonal
#define grupC 10 // Ardn pin to 2 LEDs at the other diagonal
#define grupD 11 // Ardn pin to 2 LEDs at the sides, lit when showing 6.
int Btn;        // reading of button
int ledState=0; // variable to count 0 to 5. Zero represents 6.
int dt=400;     // delay time
int dtMin= 20;  // delay time minimum, fastest
int dtMax= 400; // delay time maximum, slowest
int dtInc= 20;  // delay time increment

void setup() {
  pinMode(btnPin,INPUT_PULLUP);
  pinMode(grupA,OUTPUT);
  pinMode(grupB,OUTPUT);
  pinMode(grupC,OUTPUT);
  pinMode(grupD,OUTPUT);
}
void speedUp() {
  if (dt > dtMax)  { dt = dtMax; } // when dt is already very big, we just start from dtMax
  dt = dt - dtInc; // reduce delay time dt, move faster
  if (dt < dtMin)  { dt = dtMin; } // when dt reaches minimum, no need to go below minimum
}
void slowDown() {
  dt = dt + dtInc; // To slow down, we increace dt.
                   // In the main loop we check when button button is released, slow down,
                   // then if dt > dtMax we stop the rolling.
}
void showDice(int number) {
  switch (number) {
    case 0: { digitalWrite(grupA,LOW); digitalWrite(grupB,HIGH); digitalWrite(grupC,HIGH);
              digitalWrite(grupD,HIGH); break; } // 6: all HIGH except groupA
    case 1: { digitalWrite(grupA,HIGH); digitalWrite(grupB,LOW); digitalWrite(grupC,LOW);
              digitalWrite(grupD,LOW); break; }  // 1: only group A at the center
    case 2: { digitalWrite(grupA,LOW); digitalWrite(grupB,HIGH); digitalWrite(grupC,LOW);
              digitalWrite(grupD,LOW); break; }  // 2: group B LEDs lit
    case 3: { digitalWrite(grupA,HIGH); digitalWrite(grupB,LOW); digitalWrite(grupC,HIGH);
              digitalWrite(grupD,LOW); break; } // 3: group A and C
    case 4: { digitalWrite(grupA,LOW); digitalWrite(grupB,HIGH); digitalWrite(grupC,HIGH);
              digitalWrite(grupD,LOW); break; } // 4: group B and C
    case 5: { digitalWrite(grupA,HIGH); digitalWrite(grupB,HIGH); digitalWrite(grupC,HIGH);
              digitalWrite(grupD,LOW); break; }// 5: A, B, and C
  }
}
void loop() {
  Btn = digitalRead(btnPin);
  while (Btn==LOW) {  // Button is pushed ON, start rolling
    ledState = (ledState+1) % 6  ; // roll 0,1,2,3,4,5,0,1,...
    showDice(ledState);
    speedUp(); delay(dt);
    Btn = digitalRead(btnPin);
  }     // Randomness is obtained from the fact that button is released at any random moment,
        // at which moment the ledState is unknown by user because the die is rolling fast.
  while ((Btn==HIGH) & (dt < dtMax)) {  // high = button released, slow down.
    ledState = (ledState+1) % 6  ; // still rolling while dt < dtMax. After reaching dtmax we stop.
    showDice(ledState);
    slowDown(); delay(dt);
    Btn = digitalRead(btnPin);
  }
}

Selasa, 13 Desember 2022

Easy to understand explanation: how an astable multivibrator (or LED Flip Flop) works

This is how we build the astable multivibrator and how it works

(Penjelasan dalam Bahasa Indonesia di bagian bawah)

At the beginning, we connect a resistor and an LED, and give them a voltage; the LED is lit. Next, put an NPN transistor in between the LED and ground, LED is OFF now. The transistor acts as a switch. Give a positive bias to the base of the transistor, it is switched on and LED is ON.

Set up another pair of resistor and LED, with a transistor and a resistor to its base. Both LEDs are lit ON.

To disrupt the stable state, we introduce capacitor C2 to the base of transistor T2 with the other leg of C2 goes to the collector of transistor T1. When T1 is ON, voltage at its collector is very low close to ground. Voltage at base of T2 will be dragged low by C2, and thus T2 will be switched OFF. So now, T1 ON, T2 OFF. But after a short time, C2 is charged up through R2 and voltage at T2 base will recover to high enough level to turn T2 ON again.

When T2 is ON we use this opportunity to bring down the base voltage of T1 thus switching T1 OFF, by connecting base of T1 to collector of T2 with capacitor C1. Now, T2 ON, T1 OFF; only for a while, because C1 is charged up through R1 and T1 will be ON again, then the cycle repeats.

--- --- --- --- ---  --- --- --- --- ---     In Indonesian language:             --- --- --- --- --- --- --- --- --- --- 

Pada awalnya, sambungkan sebuah LED dengan sebuah resistor, beri tegangan listrik; LED menyala. Kemudian kita pasang sebuah transistor di antara LED dan ground, maka LED menjadi OFF. Transistor berfungsi sebagai switch. Ketika diberi tegangan bias positif pada Base transistor, maka transistor dan LED menjadi ON.


Rangkaikan lagi sepasang LED dan resistor, dengan transistor berikut resistor pada Basenya. Kedua LED sekarang ON.


Untuk menimbulkan disrupsi pada rangkaian, kita pasang kapasitor C2 ke Base transistor T2 dan kaki C2 yang satunya lagi dikonek ke Collector T1. Ketika T1 ON, maka tegangan pada Collector itu sangat rendah mendekati ground. Tegangan Base T2 akan dibawa lebih rendah oleh C2, sehingga T2 akan di-switch OFF. Sekarang jadinya T1 ON, dan T2 OFF. Tapi selang beberapa saat, C2 di-charge melalui R2 dan tegangan pada Base T2 akan kembali naik sehingga cukup untuk membuat T2 ON kembali.


Begitu T2 ON, kita gunakan kesempatan ini untuk membikin turun tegangan pada Base T1, dengan cara mengkonek Base T1 ke Collector T2 menggunakan kapasitor C1, sehingga T1 menjadi OFF. Maka sekarang T2 ON, dan T1 OFF. Keadaan inipun tak lama, karena C1 di-charge melalui R1 dan T1 bakal menjadi ON kembali, dan siklusnya berulang, demikian seterusnya kedua LED menyala flip flop.