Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BinaryCounter {
- private:
- static const uint8_t showSingleValuePin = 0;
- static const uint8_t digitCount = 8;
- const uint8_t pins[digitCount] = {12, 11, 10, 9, 8, 7, 6, 5};
- uint8_t count = 0;
- uint8_t valuetoShow = 255;
- public:
- BinaryCounter();
- void increment();
- uint8_t* getBinaryDigits(uint8_t num);
- };
- BinaryCounter::BinaryCounter() {
- //set LED all pins to output mode
- for(int i = 0; i < digitCount; i++) {
- pinMode(pins[i], OUTPUT);
- }
- }
- uint8_t* BinaryCounter::getBinaryDigits(uint8_t num) {
- // Make a byte array to hold all of the binary digits.
- uint8_t *digits = new uint8_t[digitCount];
- memset(digits, 0, digitCount * sizeof(uint8_t));
- uint8_t remaining = num;
- uint8_t currentPosition = digitCount-1;
- while(remaining > 0) {
- digits[currentPosition] = remaining % 2;
- remaining /= 2;
- currentPosition--;
- }
- return digits;
- }
- void BinaryCounter::increment() {
- uint8_t* binaryDigits;
- if(analogRead(showSingleValuePin) > 500) {
- binaryDigits = getBinaryDigits(valuetoShow );
- }
- else {
- binaryDigits = getBinaryDigits(count);
- if(count == 255) {
- count = 0;
- } else {
- count++;
- }
- }
- for(int i = 0; i < digitCount; i++) {
- digitalWrite(pins[i], binaryDigits[i]);
- }
- delete binaryDigits;
- delay(500);
- }
- BinaryCounter *counter;
- void setup()
- {
- counter = new BinaryCounter();
- }
- void loop()
- {
- counter->increment();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement