Advertisement
mikroavr

led_dec_to_bin

Oct 24th, 2020
1,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const byte led1 = 2;
  2. const byte led2 = 3;
  3. const byte led3 = 4;
  4. const byte led4 = 5;
  5. const byte led5 = 6;
  6. const byte led6 = 7;
  7. const byte led7 = 8;
  8. const byte led8 = 9;
  9.  
  10. int angka;//[10];
  11.  
  12. int ar_angka[10];
  13. int i=0;
  14.  
  15. void setup() {
  16.   // put your setup code here, to run once:
  17.   pinMode(led1, OUTPUT);
  18.   pinMode(led2, OUTPUT);
  19.   pinMode(led3, OUTPUT);
  20.   pinMode(led4, OUTPUT);
  21.   pinMode(led5, OUTPUT);
  22.   pinMode(led6, OUTPUT);
  23.   pinMode(led7, OUTPUT);
  24.   pinMode(led8, OUTPUT);
  25.  
  26.   Serial.begin(115200);
  27.   angka = 255;
  28. }
  29.  
  30. void loop() {
  31.   // put your main code here, to run repeatedly
  32.  
  33.   for(i = 0; angka>0; i++){
  34.     ar_angka[i] = angka%2;
  35.     angka=angka/2;
  36.   }
  37.  
  38.   Serial.print("jumlah i pertama: ");
  39.   Serial.println(i);
  40.   for(i = i-1; i>=0; i--){
  41.     Serial.print(ar_angka[i]);
  42.   }
  43.   Serial.println();
  44.   Serial.print("jumlah i kedua: ");
  45.   Serial.println(i);
  46.  
  47.   digitalWrite(led1, ar_angka[0]);
  48.   digitalWrite(led2, ar_angka[1]);
  49.   digitalWrite(led3, ar_angka[2]);
  50.   digitalWrite(led4, ar_angka[3]);
  51.  
  52.   digitalWrite(led5, ar_angka[4]);
  53.   digitalWrite(led6, ar_angka[5]);
  54.   digitalWrite(led7, ar_angka[6]);
  55.   digitalWrite(led8, ar_angka[7]);
  56.   delay(1000);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement