Advertisement
ONEMUNEEB

Untitled

Apr 1st, 2022
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. // Array of Output Pin variables, set to the pins being used
  2. const int b_size = 4;
  3. const int b[b_size] = {2, 3, 4, 5};
  4. // Output Buffer
  5. int b_buf = 0x00;
  6. // Input Variables
  7.  
  8. // Serial Monitor Buffer
  9. int s_buf = 0x00;
  10.  
  11. /* 's' is an array of integers of size 8. Note that arrays at 1 start
  12. We will use this to be able to see the individual bit values of the s_buf
  13.  
  14. */
  15. const int s_size = 8;
  16. int s[s_size];
  17.  
  18. // We can also define our own helper functions. It is a good idea to use helper functions whenever they make sense.
  19. // Normally we also define the return type (void for none)
  20.  
  21. // Read from the serial monitor into s_buf and the s[] array for individual bits
  22. void readData()
  23. {
  24. if(Serial.available())
  25. s_buf = Serial.parseInt();
  26.  
  27. for(int i = (s_size - 1); i>=0; i--) {
  28. s[i] = (s_buf >> i) & 0x01; // What's going on here?
  29. // ">>" bit shifting
  30. // "&" bit masking
  31. }
  32.  
  33. }
  34. // Reset the Output Buffer.
  35. void resetBuffer() {
  36. for(int i = 0; i < b_size; i++) {
  37. // Note this is an arduino function call to the pins
  38. digitalWrite(b[i], LOW);
  39. }
  40. }
  41. // Writes to the buffer. Note this function ORs the current value with the new value
  42. // Note that size is an optional argument with default size the same as the buffer
  43. void writeBuffer(unsigned char b_temp, int size = b_size)
  44. {
  45. for (int i = (size - 1); i >= 0; i--) {
  46. if ((b_temp >> i) & 0x01) {
  47. digitalWrite(b[i], HIGH);
  48. }
  49. }
  50. }
  51.  
  52.  
  53. void setup() {
  54. // OUTPUT is a defined macro in Arduino!
  55. for(int i = 0; i < b_size; i++)
  56. {
  57. pinMode(b[i], OUTPUT);
  58. }
  59. // We will also read from the serial monitor
  60. Serial.begin(9600);
  61.  
  62. pinMode(8, INPUT);
  63. pinMode(9, INPUT_PULLUP);
  64. }
  65.  
  66. void loop() {
  67. readData();
  68. //resetBuffer();
  69.  
  70. s_buf = map(s_buf, 0, 255, 0, 15);
  71. //b_buf = s_buf;
  72.  
  73. writeBuffer(b_buf);
  74. b_buf = map(b_buf, 0, 255, 0, 15);
  75.  
  76. int counter = 0;
  77. int prevCounter = 0;
  78.  
  79. //PART 2
  80.  
  81. int button8State = digitalRead(8);
  82. b_buf = button8State;
  83.  
  84.  
  85. if(button8State == HIGH){
  86. writeBuffer(b_buf);
  87. }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement