// Source :: Lab4
// Description :: Lab04 - Counting in Binary
// Programmed by :: Douglas Vieira Ferreira / 03-16-2024
/*
The assignment involves modifying Lab 3 Arduino project to integrate a potentiometer
for dynamic control over a counter's increment value.
*/
const int POT_PIN = A0; // Potentiometer pin connected to A0
const int RED_PIN = 5;
const int GREEN_PIN = 6;
const int BLUE_PIN = 7;
const int PAUSE_BUTTON_PIN = 8; // Pause button pin
// Define the BinaryDisplay class to manage the LED display
class BinaryDisplay {
public:
int pins[4];
void setup(int pin1, int pin2, int pin4, int pin8) {
pins[0] = pin1;
pins[1] = pin2;
pins[2] = pin4;
pins[3] = pin8;
for (int i = 0; i < 4; i++) {
pinMode(pins[i], OUTPUT);
}
}
void display(int value) {
for (int i = 0; i < 4; i++) {
digitalWrite(pins[i], value & (1 << i) ? HIGH : LOW);
}
}
};
BinaryDisplay binary;
int Counter01 = 0;
int Counter16 = 0;
int Total = 0;
void setup() {
Serial.begin(9600);
binary.setup(10, 11, 12, 13); // Initialize BinaryDisplay with LED pins
pinMode(PAUSE_BUTTON_PIN, INPUT_PULLUP);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(POT_PIN, INPUT);
}
void loop() {
int potValue = analogRead(POT_PIN);
int increment = map(potValue, 0, 1023, -4, 4);
if (digitalRead(PAUSE_BUTTON_PIN) == HIGH) {
Counter01 += increment;
if (Counter01 >= 16) {
Counter01 -= 16;
Counter16++;
} else if (Counter01 < 0) {
Counter01 += 16;
Counter16--;
}
if (Counter16 >= 4) {
Counter16 = 0;
} else if (Counter16 < 0) {
Counter16 = 3;
}
Total = Counter16 * 16 + Counter01;
binary.display(Counter01);
Serial.print("Increment: ");
Serial.print(increment);
Serial.print(", counter16: ");
Serial.print(Counter16);
Serial.print(", counter01: ");
Serial.print(Counter01);
Serial.print(", TOTAL: ");
Serial.println(Total);
}
delay(200);
}