Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. struct opCode
  4. {
  5. unsigned int digits : 3;
  6. };
  7. struct ALU_Driver
  8. {
  9. int A;
  10. int B;
  11. opCode operation;
  12. };
  13. ALU_Driver arr[8];
  14. int Result_Array[8];
  15.  
  16. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  17. int pushed = 0;
  18.  
  19. int readBtn(){
  20. pushed = analogRead(0);
  21. if(pushed == 1023) return 1;
  22. if(pushed == 1021) return 2;
  23. return pushed;
  24. }
  25.  
  26. int index = 0;
  27. int btn = 0;
  28. void Print(int index){
  29. lcd.setCursor(0, 0);
  30. String temp = "";
  31. switch (arr[index].operation.digits) {
  32. case 000:
  33. temp="+";
  34. break;
  35. case 001:
  36. temp="-";
  37. break;
  38. case 010:
  39. temp="<<";
  40. break;
  41. case 011:
  42. temp=">>";
  43. break;
  44. case 100:
  45. temp=">>";
  46. break;
  47. case 101:
  48. temp="&";
  49. break;
  50. case 110:
  51. temp="|";
  52. break;
  53. case 111:
  54. temp="^";
  55. break;
  56. default:
  57. break;
  58. }
  59.  
  60. lcd.print(arr[index].A + temp + arr[index].B + "=" + Result_Array[index]);
  61. }
  62. void setup() {
  63. lcd.begin(16, 2);
  64.  
  65. arr[0].A=3;
  66. arr[0].B=5;
  67. arr[0].operation.digits=000;
  68.  
  69. arr[1].A=13;
  70. arr[1].B=7;
  71. arr[1].operation.digits=001;
  72.  
  73. arr[2].A=22;
  74. arr[2].B=3;
  75. arr[2].operation.digits=010;
  76.  
  77. arr[3].A=33;
  78. arr[3].B=1;
  79. arr[3].operation.digits=011;
  80.  
  81. arr[4].A=33;
  82. arr[4].B=-1;
  83. arr[4].operation.digits=100;
  84.  
  85. arr[5].A=23;
  86. arr[5].B=32;
  87. arr[5].operation.digits=101;
  88.  
  89. arr[6].A=37;
  90. arr[6].B=20;
  91. arr[6].operation.digits=110;
  92.  
  93. arr[7].A=17;
  94. arr[7].B=28;
  95. arr[7].operation.digits=111;
  96.  
  97. int i=0;
  98. for (i=0;i<8;i++) {
  99. switch (arr[i].operation.digits) {
  100. case 000:
  101. Result_Array[i]=arr[i].A + arr[i].B;
  102. break;
  103. case 001:
  104. Result_Array[i]=arr[i].A - arr[i].B;
  105. break;
  106. case 010:
  107. Result_Array[i]=arr[i].A << arr[i].B;
  108. break;
  109. case 011:
  110. Result_Array[i]=arr[i].A >> arr[i].B;
  111. break;
  112. case 100:
  113. if(arr[i].B<0){
  114. arr[i].B*=-1;;
  115. Result_Array[i]=arr[i].A << arr[i].B;
  116. }else{
  117. Result_Array[i]=arr[i].A >> arr[i].B;
  118. }
  119. break;
  120. case 101:
  121. Result_Array[i]=arr[i].A & arr[i].B;
  122. break;
  123. case 110:
  124. Result_Array[i]=arr[i].A | arr[i].B;
  125. break;
  126. case 111:
  127. Result_Array[i]=arr[i].A ^ arr[i].B;
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. Print(index);
  134. }
  135. void loop() {
  136. btn = readBtn();
  137. if(btn == 1){
  138. lcd.clear();
  139. index--;
  140. if(index < 0)
  141. index = 7;
  142. delay(150);
  143. Print(index);
  144. }else if(btn == 2){
  145. lcd.clear();
  146. index = (index + 1) % 8;
  147. delay(150);
  148. Print(index);
  149. }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement