// Source :: sketch.io
// Description :: Blinker with Potentiometer for Lab 04
// Programmed by :: Kristian Schurman / 2024-03-18
/*
count1 increments every 1000ms (with a maximum value of 15) and is sent to the LEDs,
where they will read it as a binary number and light up according to it. From left
to right the LEDs are a digit. If an LED is on, the number of that digit is 1,
otherwise it's 0 (on off on off = 1010). When count1 goes past 16, it's reset to 0
and has the rollover added to the new number, and count16 increments by one.
Likewise, when count1 goes under 0 it's reset to 63 and subtracts the leftovers,
and count16 is unincremented by 1. Increment size of count1 is dependant on the
potentiometer.
Every time count1 goes past 15 and count16 is already 3, count16 is reset to 0.
Every time count1 goes below 0 and count16 is at 0, count16 is reset to 3.
IF:
count16 = 0: RGB LED is off
count16 = 1: RGB LED is red
count16 = 2: RGB LED is green
count16 = 3: RGB LED is blue
*/
class BinaryDisplay {
public:
int pin1, pin2, pin4, pin8;
void setup(int pin1, int pin2, int pin4, int pin8) { //Set up the regular LED pins
this->pin1 = pin1;
this->pin2 = pin2;
this->pin4 = pin4;
this->pin8 = pin8;
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin8, OUTPUT);
}
void display(int iValue) {
digitalWrite(pin1, (iValue & 1) ? HIGH : LOW); //If the binary representation of iValue has a 1 as the last digit
digitalWrite(pin2, (iValue & 2) ? HIGH : LOW); //If the binary representation of iValue has a 1 as the third digit
digitalWrite(pin4, (iValue & 4) ? HIGH : LOW); //If the binary representation of iValue has a 1 as the second digit
digitalWrite(pin8, (iValue & 8) ? HIGH : LOW); //If the binary representation of iValue has a 1 as the first digit
}
};
BinaryDisplay binary;
int count1 = 0; //Counts from 0 to 15 then resets
int count16 = 0; //Counts every time count1 resets
int total = 0; //Total number, resets after 63
int red = 6; //Red LED pin
int green = 7; //Green LED pin
int blue = 8; //Blue LED pin
int potPin = A0; //Potentiometer pin
void setup() {
binary.setup(2, 3, 4, 5); // Assuming pin numbers 2, 3, 4, 5 are used for the LED's
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
Serial.begin(9600);
Serial.println("Assignment 4: Counting in Binary (with Potentiometer (0 to 8)) by Kristian");
Serial.println(" "); //This and above are part A
//Serial.println("Assignment 4: Counting in Binary (with Potentiometer (4 to -4)) by Kristian");
//println(" "); //This and above are part B
delay(100); //Delay so we can read the pot sensor
int pValue = analogRead(potPin); //Read pot sensor
int pMapped = map(pValue, 0, 1023, 0, 8); //Part A
//int pMapped = map(pValue, 0, 1023, -4, 4); //Part B
//These serials below only serve the purpose of printing the initial values and the current increment
Serial.print("Increment: ");
Serial.print(pMapped);
Serial.print(", counter16: ");
Serial.print(count16);
Serial.print(", counter01: ");
Serial.print(count1);
Serial.print(", TOTAL: ");
Serial.println(total);
}
void loop() {
delay(1000); //There needs to be a delay so the circuit can register the potentiometer position properly
int pValue = analogRead(potPin); //Read pot sensor
int pMapped = map(pValue, 0, 1023, 0, 8); //Part A
//int pMapped = map(pValue, 0, 1023, -4, 4); //Part B
//Explaining map(). pValue is the input of the Potentiometer, and can include any
//number between 0 (left) and 1023 (right). Here we're selecting the range, and
//setting them to a new range. In part one, we're keeping the left value of 0 the
//same, but we're changing the right value from 1023 to 8. In part two, it's 0 to
//4, and 1023 to -4
count1 = count1 + pMapped; //Increment count1 by the pot sensor value
if (count1 > 15) { //If count1 is more than 15
count1 = count1 - 16; //Reset it to 0 and add rollover
count16++; //Increment count16 by one
count16 = count16 > 3 ? 0 : count16; //If count16 is more than 3, reset it to 0
}
else if (count1 < 0) { //If count1 is less than 0
count1 = count1 + 16; //Reset it to 16 and subtract it with the rollover
count16--; //Unincrement (that's a word I swear)
count16 = count16 < 0 ? 3 : count16; //If count16 is less than 0, reset it to 3
}
binary.display(count1); //Set pins 1-4 lights
total = total + pMapped;
if (total > 63) {
total = total - 64;
}
else if (total < 0) {
total = total + 64;
} //These two are basically the same as above
if (count16 == 0) { //RGB LED off
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 0);
}
else if (count16 == 1) { //RGB LED is red
analogWrite(red, 255);
analogWrite(green, 0);
analogWrite(blue, 0);
}
else if (count16 == 2) { //RGB LED is green
analogWrite(red, 0);
analogWrite(green, 255);
analogWrite(blue, 0);
}
else if (count16 == 3) { //RGB LED is blue
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 255);
}
Serial.print("Increment: ");
Serial.print(pMapped);
Serial.print(", counter16: ");
Serial.print(count16);
Serial.print(", counter01: ");
Serial.print(count1);
Serial.print(", TOTAL: ");
Serial.println(total);
}