// Source :: Lab3
// Description :: Lab03 - Counting in Binary
// Programmed by :: Douglas Vieira Ferreira / 03-10-2024
/*
Display binary values using a set of 4 LEDs and integrate additional features such as a tri-color LED display,
a pause button, and a temperature sensor to adjust the operation dynamically.
*/
const int PAUSE_BUTTON_PIN = 8; // pause button pin
const int TEMP_SENSOR_PIN = A0; // temperature sensor pin
// Define pins for the tri-color LED
const int RED_PIN = 5; // LED connected to digital pin 5
const int GREEN_PIN = 6; // LED connected to digital pin 6
const int BLUE_PIN = 7; // LED connected to digital pin 7
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 iValue) {
for (int i = 0; i < 4; i++) {
digitalWrite(pins[i], iValue & (1 << i) ? HIGH : LOW);
}
}
};
BinaryDisplay binary;
int Counter01 = 0;
int Counter16 = 0;
void setup() {
Serial.begin(9600);
binary.setup(10, 11, 12, 13); // Initialize BinaryDisplay with LED pins
pinMode(PAUSE_BUTTON_PIN, INPUT_PULLUP); // Setup for the PAUSE button
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(TEMP_SENSOR_PIN, INPUT); // Setup for the temperature sensor
}
void loop() {
if (digitalRead(PAUSE_BUTTON_PIN) == HIGH) {
int tempValue = analogRead(TEMP_SENSOR_PIN);
int delta = (tempValue / 1023.0) * 100;
Counter01++;
if (Counter01 > 16) {
Counter01 = 0;
Counter16++;
if (Counter16 > 3) Counter16 = 0;
}
binary.display(Counter01);
// Reset tri-color LED
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
// Light up tri-color LED based on Counter16
switch (Counter16) {
case 1:
Serial.print("CASE 1 - DEBUG:");
Serial.println(Counter16);
digitalWrite(RED_PIN, HIGH);
break;
case 2:
Serial.print("CASE 2 - DEBUG:");
Serial.println(Counter16);
digitalWrite(GREEN_PIN, HIGH);
break;
case 3:
Serial.print("CASE 3 - DEBUG:");
Serial.println(Counter16);
digitalWrite(BLUE_PIN, HIGH);
break;
}
delay(350 - delta);
}
}
Loading
ds18b20
ds18b20