Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Array of Output Pin variables, set to the pins being used
- const int b_size = 4;
- const int b[b_size] = {2, 3, 4, 5};
- // Output Buffer
- int b_buf = 0x00;
- // Input Variables
- // Serial Monitor Buffer
- int s_buf = 0x00;
- /* 's' is an array of integers of size 8. Note that arrays at 1 start
- We will use this to be able to see the individual bit values of the s_buf
- */
- const int s_size = 8;
- int s[s_size];
- // We can also define our own helper functions. It is a good idea to use helper functions whenever they make sense.
- // Normally we also define the return type (void for none)
- // Read from the serial monitor into s_buf and the s[] array for individual bits
- void readData()
- {
- if(Serial.available())
- s_buf = Serial.parseInt();
- for(int i = (s_size - 1); i>=0; i--) {
- s[i] = (s_buf >> i) & 0x01; // What's going on here?
- // ">>" bit shifting
- // "&" bit masking
- }
- }
- // Reset the Output Buffer.
- void resetBuffer() {
- for(int i = 0; i < b_size; i++) {
- // Note this is an arduino function call to the pins
- digitalWrite(b[i], LOW);
- }
- }
- // Writes to the buffer. Note this function ORs the current value with the new value
- // Note that size is an optional argument with default size the same as the buffer
- void writeBuffer(unsigned char b_temp, int size = b_size)
- {
- for (int i = (size - 1); i >= 0; i--) {
- if ((b_temp >> i) & 0x01) {
- digitalWrite(b[i], HIGH);
- }
- }
- }
- void setup() {
- // OUTPUT is a defined macro in Arduino!
- for(int i = 0; i < b_size; i++)
- {
- pinMode(b[i], OUTPUT);
- }
- // We will also read from the serial monitor
- Serial.begin(9600);
- pinMode(8, INPUT);
- pinMode(9, INPUT_PULLUP);
- }
- void loop() {
- readData();
- //resetBuffer();
- s_buf = map(s_buf, 0, 255, 0, 15);
- //b_buf = s_buf;
- writeBuffer(b_buf);
- b_buf = map(b_buf, 0, 255, 0, 15);
- int counter = 0;
- int prevCounter = 0;
- //PART 2
- int button8State = digitalRead(8);
- b_buf = button8State;
- if(button8State == HIGH){
- writeBuffer(b_buf);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement