I did up top on my first post in the wokwi link.
Anyway I've got it working really well now. I would like to upload a short video but I tried and the zip file was too big. Anyway I will upload my final iteration of the code as I've made some minor tweaks to get it working so well.
// DEFINE ALL PINS TO COMPONENTS
// LED PINS + BRIGHTNESSES
const int LED_YELLOW_PIN = 5; int YELLOW_BRIGHTNESS = 0;
const int LED_BLUE_PIN = 6; int BLUE_BRIGHTNESS = 0;
const int LED_GREEN_PIN = 9; int GREEN_BRIGHTNESS = 0;
const int LED_RED_PIN = 10; int RED_BRIGHTNESS = 0;
// POT_PIN
#define potValue analogRead(POT_PIN) // ABSOLUTE CURRENT POT VALUE READ
const int POT_PIN = A0;
// LED brightnes
#define mappedPot map(potValue, 0, 1023, 0, 255) // DEFINE LED BRIGHTNESS BASED ON ABSOLUTE CURRENT POT VALUE
int POT_INPUT_VALUE = potValue;
int LED_BRIGHTNESS = 0;
int time = 0;
void setup() {
// ESTABLISH PIN MODES
pinMode(LED_YELLOW_PIN, OUTPUT);
pinMode(LED_BLUE_PIN, OUTPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
pinMode(LED_RED_PIN, OUTPUT);
// CREATE TIME-STAMP
time = millis();
// READ POTENTIOMETER. MAP IT TO PMW VALUE AND SAVE TO VARIABLE
POT_INPUT_VALUE = potValue;
// ENABLE SERIAL MONITOR FOR DEBUGGING
Serial.begin(9600);
}
// ------ PRINT VALUES TO SCREEN --------
void displayValues() {
Serial.print("Sensor: ");
Serial.println(POT_INPUT_VALUE);
Serial.print("PWM Output: ");
Serial.println(LED_BRIGHTNESS);
}
// -------- OUTPUT TO LED'S --------
void outputLED() {
analogWrite(LED_YELLOW_PIN, LED_BRIGHTNESS);
analogWrite(LED_RED_PIN, LED_BRIGHTNESS);
analogWrite(LED_GREEN_PIN, LED_BRIGHTNESS);
analogWrite(LED_BLUE_PIN, LED_BRIGHTNESS);
}
// ------------------- POT VALUE HAS CHANGED. UPDATE LED'S GRADUALLY ONE BY ONE ----------------
void gradualChange() {
// LED'S GRADUATE UP
if (potValue > POT_INPUT_VALUE){
// FOR LOOP 1 - 4 (LEFT TO RIGHT) LED BRIGHTNESS GRADUALLY INCREASES
for (int L = 0; L <= 3; L++){
for(int B = LED_BRIGHTNESS; B <= mappedPot; B+=2){
if (L == 0){ // : OUTPUTS RED LED ONLY TO CURRENT BRIGHTNESS VALUE
analogWrite(LED_RED_PIN, B);
}
else if (L == 1){ // 1 : OUTPUTS GREEN LED ONLY TO CURRENT BRIGHTNESS VALUE
analogWrite(LED_GREEN_PIN, B);
}
else if (L == 2){ // 2 : OUTPUTS BLUE LED ONLY TO CURRENT BRIGHTNESS VALUE
analogWrite(LED_BLUE_PIN, B);
}
else if (L == 3){ // 3 : OUTPUTS YELLOW LED ONLY TO CURRENT BRIGHTNESS VALUE
analogWrite(LED_YELLOW_PIN, B);
}
delay(2);
}
}
}
else{ // if (potValue < POT_INPUT_PREV_VALUE){
// FOR LOOP 3 - 0 (RIGHT TO LEFT) LED BRIGHTNESS GRADUALLY DECREASES
for (int L = 3; L >= 0; L--){
for(int B = LED_BRIGHTNESS; B >= mappedPot; B-=2){
if (L == 0){ // : OUTPUTS RED LED ONLY TO CURRENT BRIGHTNESS VALUE
analogWrite(LED_RED_PIN, B);
}
else if (L == 1){ // 1 : OUTPUTS GREEN LED ONLY TO CURRENT BRIGHTNESS VALUE
analogWrite(LED_GREEN_PIN, B);
}
else if (L == 2){ // 2 : OUTPUTS BLUE LED ONLY TO CURRENT BRIGHTNESS VALUE
analogWrite(LED_BLUE_PIN, B);
}
else if (L == 3){ // 3 : OUTPUTS YELLOW LED ONLY TO CURRENT BRIGHTNESS VALUE
analogWrite(LED_YELLOW_PIN, B);
}
delay(2);
}
}
}
}
// ===================================================================================================
// ======================================= MAIN LOOP ===============================================
void loop() {
time = millis(); // TIME STAMP FOR ALLOWING USER ADJUSTMENT TIME
// CHECK FOR POT CHANGES WITHIN TIME LIMIT TO ALLOW USER TIME TO ADJUST FURTHER
while(abs(potValue - POT_INPUT_VALUE) > 5 && millis() - time < 150){
}
// IF POT VALUE HAS CHANGED AT 10 CALL gradualChange()
if(abs(potValue - POT_INPUT_VALUE) > 15){
gradualChange();
}
// UPDATE POT VALUE
POT_INPUT_VALUE = potValue;
//displayValues(); // DEBUGGING
LED_BRIGHTNESS = mappedPot; // UPDATE LED BRIGHTNESS VALUES TO CURRENT POT VALUE
outputLED();
delay(20);
}
Consider
byte PinLeds [] = { Red, Blue, Green };
Just curious. Why use byte instead of integer?
Memory saving.
A byte stores an 8-bit unsigned number, from 0 to 255. An int stores a 16-bit (2-byte) value on the UNO and other ATmega based boards.
Sweet thanks for the info. I was figuring it was to save memory space but i just didn't understand how it would. Now i do
Another benefit of using a byte on the UNO and ATmega based boards is that they process data in byte-sized chunks, so reading or writing a 2-byte int takes twice as long. Also there are certain circumstances where the other bytes in a multi-byte value can change while you are working with one of the bytes. Keeping the storage as small as possible is best practice.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.