Skip to main content

Proyek Arduino dengan 2 Buah Seven Segment

Proyek Arduino dilakukan dengan menggunakan dua buah 7-segment. Tujuan dari ptoyek yang dilakukan adalah membuat 7-segment menampilkan angka random setiap kali tombol ditekan. Alat dan bahan yang dibutuhkan untuk proyek kali ini adalah.
  1. Arduino UNO
  2. Jumpers
  3. Breadboard
  4. Resistor
  5. 2 buah 7-segment
  6. Komputer
  7. Button
Berikut ini adalah video hasil bagaimana proyek Arduino bekerja.


Terdapat kesalahan dalam pembuatan proyek ini. Tujuan awal adalah supaya angka yang ditampilkan di 7-segment benar-benar random. Namun hasil proyek ini justru menampilkan angka kanan dan kiri yang sama. Hal ini kemungkinan dikarenakan rangkaian Arduino menggunakan flip-flop.

Dan berikut ini adalah source code yang digunakan

/*  * SevenSegmentMpx sketch
    * Shows numbers ranging from 0 through 99 on a four-digit display
    * This example displays the value of a sensor connected to an analog input */
// bits representing segments A through G (and decimal point) for numerals 0-9
const int trigPin = 12;
const int echoPin = 13;
const int numeral[10] = {
  //ABCDEFG /dp
  B11111100,  // 0
  B01100000,  // 1
  B11011010,  // 2
  B11110010,  // 3
  B01100110,  // 4
  B10110110,  // 5
  B00111110,  // 6
  B11100000,  // 7
  B11111110,  // 8
  B11100110,  // 9
};

// pins for decimal point and each segment
// dp,G,F,E,D,C,B,A

const int segmentPins[] = { 4,7,8,6,5,3,2,9};
const int nbrDigits= 2;  // the number of digits in the LED display
                         //dig  1  2
const int digitPins[nbrDigits] = {11,10};
const int buttonPin = 4; 
int buttonState = 0;
int hasil = 0;
int digit = 1;
int x = 1;

void setup() {
  Serial.begin(9600);
  for(int i=0; i < 8; i++)
  pinMode(segmentPins[i], OUTPUT);

// set segment and DP pins to output
  for(int i=0; i < nbrDigits; i++)
  pinMode(digitPins[i], OUTPUT);
  
  pinMode(buttonPin, INPUT);
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
  buttonState = digitalRead(buttonPin);

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  /*
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  */
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  /*
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  */
  // convert the time into a distance
  /*
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  cm = cm + 1;
  */
//  Serial.print(inches);
//  Serial.print(“in, “);
  /*
   * Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  */
//  delay(50);

   if (buttonState == LOW) {
    int x = random(1,52);
    
    showNumber(x);
    //
   
    
   // x++;
  }
  delay(50);  
}

void showNumber( int number) {
  if(number == 0)
    showDigit( 0, nbrDigits-1) ; // display 0 in the rightmost digit
  else  {    // display the value corresponding to each digit    // leftmost digit is 0, rightmost is one less than the number of places
  for( int digit = nbrDigits-1; digit >= 0; digit--)    {
    if(number > 0)      {
      showDigit( number % 10,digit)  ;
      number = number / 10;
    } else if (number == 0) {
      showDigit( number % 10,digit) ;
    }
    }
  }
}

// Displays given number on a 7-segment display at the given digit position

void showDigit( int number, int digit) {
  digitalWrite( digitPins[digit], HIGH );
  for(int segment = 1; segment < 8; segment++)  {
    boolean isBitSet = bitRead(numeral[number], segment);    // isBitSet will be true if given bit is 1
    // isBitSet = ! isBitSet; // remove this line if common cathode display

    digitalWrite( segmentPins[segment], isBitSet);
  }

  delay(10);

  digitalWrite( digitPins[digit], LOW );
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax’s datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Comments