Well it's not perfectly smooth, it will go in steps from 0 to 255.
If it's a cheap little pot like you show in your diagram, it may be jittery when you move it.
You can try filtering the analogRead to help smooth it out.
do you know how to add a light sweeping effect to the code?
Let's start, we will take things one step at a time.
Wire the components as in post #19, include the LED on pin 13.
Look at the attached sketch.
You must ask questions about the things you do not understand.
Your heartbeat LED on pin 13 should toggle every 500ms.
When you are ready we can continue.
//
//================================================^================================================
// 5 L E D s S w i t c h a n d P o t e n t i o m e t e r
//================================================^================================================
//
// https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599
//
//
//
// Version YY/MM/DD Comments
// ======= ======== ========================================================================
// 1.00 25/08/16 Running code.
//
//
//
//
//
// Notes:
//
//
//
//
//================================================
#define LEDon HIGH
#define LEDoff LOW
#define CLOSED LOW
#define OPENED HIGH
//================================================
//constant variables (won't change)
const byte ledPin1 = 3; //LED 1 attached to number 3
const byte ledPin2 = 5; //LED 2 attached to number 5
const byte ledPin3 = 6; //LED 3 attached to number 6
const byte ledPin4 = 9; //LED 4 attached to number 9
const byte ledPin5 = 10; //LED 5 attached to number 10
const byte heartbeatLED = 13; //LED should toggle every 500ms.
const byte btnPin = 2; //push button attached to number 2
const byte potPin = A0; //potentiometer attached to A0
byte lastBtnPin = OPENED;
byte ledPins[] = {3, 5, 6, 9, 10, 13};
byte pinTotal = sizeof(ledPins);
//================================================
//timing stuff
unsigned long heartbeatTime;
unsigned long heartbeatInterval = 500ul; //milliseconds
unsigned long checkSwitchTime;
unsigned long switchInterval = 50ul; //milliseconds
// s e t u p ( )
//================================================^================================================
//
void setup()
{
Serial.begin(9600); //default 9600 baud rate
//put your setup code here, to run once:
pinMode(btnPin, INPUT_PULLUP);
//LED Array Output
for (int thisPin = 0; thisPin < pinTotal; thisPin++)
{
digitalWrite(ledPins[thisPin], LEDoff);
pinMode(ledPins[thisPin], OUTPUT);
}
//Gives us an idea if there is code blocking happening.
pinMode(heartbeatLED, OUTPUT);
} //END of setup()
// l o o p ( )
//================================================^================================================
//
void loop()
{
//======================================================================== T I M E R heartbeatLED
//Is it time to toggle the heartbeat LED ?
if (millis() - heartbeatTime >= heartbeatInterval)
{
//Restart this TIMER.
heartbeatTime = millis();
//Toggle the heartbeat LED.
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//======================================================================== T I M E R check switches
//Is it time to scan our switches ?
if (millis() - checkSwitchTime >= switchInterval)
{
//Restart this TIMER.
checkSwitchTime = millis();
//Check for switch change in state.
checkSwitches();
}
//================================================
// Other non-blocking code goes here
//================================================
} // END of loop()
// c h e c k S w i t c h e s ( )
//================================================^================================================
//
void checkSwitches()
{
byte state;
//======================================================================== btnPin
state = digitalRead(btnPin);
//================================================
//Has this switch changed state ?
if (lastBtnPin != state)
{
//Update to the new state.
lastBtnPin = state;
//========================
//Was the switch pushed ?
if (state == CLOSED)
{
}
} //END of this switch.
//======================================================================== nextSwitch
} //END of checkSwitches()
//================================================^================================================
Do I smooth it by searching the range? Can you explain more on how I can do this?
I already looked at the Arduino Larson scanner, but I don’t quite understand how to add it to my code, especially with the button and potentiometer there.
We try to avoid HIGH, LOW, true, false, when we write our sketches.
These terms do not describe (document) what is actually happening in our code.
We can write: if (mySwitchLevel == LOW) //what does this mean 6 months from now ?
The line below describes what is actually happening: if (mySwitchLevel == CLOSED) //This is easier to understand what is happening.
#define is a C++ directive that says we are creating a name that is equivalent to something else. #define CLOSED LOW
CLOSED is now the same as LOW
Byte is 8 bits, int is 16 bits (2 bytes) , i.e. byte uses less memory than int
The heartbeat code inside loop() toggles the LED on the heartbeat pin every 500ms
We can use this LED to check if the code is running.
heartbeatInterval is a variable that is equal to the time the LED will toggle.
Try changing heartbeatInterval = 2000
millis() is a function in the Arduino code collection that returns the time since the Arduino was turned on. We can use millis() to make non blocking TIMERs.
Many new people use delay(. . .) to and a timing events, however, all normal code stops while we are waiting. this is Blocking code which we try to avoid at all costs.
Thank you for explaining it to me! I think we can continue first, and I’ll try to understand more about these functions by learning through the code.
Also, will this be different from the codes that I tried before? especially the code that Jim mentioned.
I also might not respond to you in the meantime, since I'm going to take a rest. But feel free to continue explaining the code, I’ll try to get back to you as soon as possible!
When you wake up, for a drink of water, try the next version below.
Describe what it does ?
Does your heartbeat LED toggle every 500ms ?
Do you have any question on what is happening in the sketch ?
//
//================================================^================================================
// 5 L E D s S w i t c h a n d P o t e n t i o m e t e r
//================================================^================================================
//
// https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599
//
//
//
// Version YY/MM/DD Comments
// ======= ======== ========================================================================
// 1.00 25/08/16 Running code.
//
//
//
//
//
// Notes:
//
//
//
//
//================================================
#define LEDon HIGH
#define LEDoff LOW
#define CLOSED LOW
#define OPENED HIGH
#define ENABLED true
#define DISABLED false
//================================================
//constant variables (won't change)
const byte ledPin1 = 3; //LED 1 attached to number 3
const byte ledPin2 = 5; //LED 2 attached to number 5
const byte ledPin3 = 6; //LED 3 attached to number 6
const byte ledPin4 = 9; //LED 4 attached to number 9
const byte ledPin5 = 10; //LED 5 attached to number 10
const byte heartbeatLED = 13; //LED should toggle every 500ms.
const byte btnPin = 2; //push button attached to number 2
const byte potPin = A0; //potentiometer attached to A0
byte lastBtnPin = OPENED;
byte ledPins[] = {3, 5, 6, 9, 10};
byte pinTotal = sizeof(ledPins);
//================================================
//timing stuff
unsigned long heartbeatTime;
unsigned long heartbeatInterval = 500ul; //milliseconds
unsigned long checkSwitchTime;
unsigned long switchInterval = 50ul; //milliseconds
unsigned long commonTime;
unsigned long commonInterval = 2000ul; //2 seconds
bool commonFlag = DISABLED;
// s e t u p ( )
//================================================^================================================
//
void setup()
{
Serial.begin(9600); //default 9600 baud rate
//put your setup code here, to run once:
pinMode(btnPin, INPUT_PULLUP);
//LED Array Output
for (int thisPin = 0; thisPin < pinTotal; thisPin++)
{
digitalWrite(ledPins[thisPin], LEDoff);
pinMode(ledPins[thisPin], OUTPUT);
}
//Gives us an idea if there is code blocking happening.
pinMode(heartbeatLED, OUTPUT);
} //END of setup()
// l o o p ( )
//================================================^================================================
//
void loop()
{
//======================================================================== T I M E R heartbeatLED
//Is it time to toggle the heartbeat LED ?
if (millis() - heartbeatTime >= heartbeatInterval)
{
//Restart this TIMER.
heartbeatTime = millis();
//Toggle the heartbeat LED.
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//======================================================================== T I M E R check switches
//Is it time to scan our switches ?
if (millis() - checkSwitchTime >= switchInterval)
{
//Restart this TIMER.
checkSwitchTime = millis();
//Check for switch change in state.
checkSwitches();
}
//======================================================================== T I M E R common
//If this TIMER is ENABLED, did the common TIMER it expired ?
if (commonFlag == ENABLED && millis() - commonTime >= commonInterval)
{
//We are finished with this TIMER now
commonFlag = DISABLED;
//Turn off all the LEDs.
for (byte x = 0; x < pinTotal; x++)
{
digitalWrite(ledPins[x], LEDoff);
}
}
//================================================
// Other non-blocking code goes here
//================================================
} // END of loop()
// c h e c k S w i t c h e s ( )
//================================================^================================================
//
void checkSwitches()
{
byte state;
//======================================================================== btnPin
state = digitalRead(btnPin);
//================================================
//Has this switch changed state ?
if (lastBtnPin != state)
{
//Update to the new state.
lastBtnPin = state;
//========================
//Was the switch pushed ?
if (commonFlag == DISABLED && state == CLOSED)
{
//Turn on all the LEDs.
for (byte x = 0; x < pinTotal; x++)
{
digitalWrite(ledPins[x], LEDon);
}
//Allow the common TIMER to run.
commonFlag = ENABLED;
//Restart the TIMER
commonTime = millis();
}
} //END of this switch.
//======================================================================== nextSwitch
} //END of checkSwitches()
//================================================^================================================
A true statement, in sense. I have been to many conferences where a speaker advised all to keep a pencil and paper pad next to their bed so they could write down the solutions that appeared while the slept. I always still had the answer when I woke up the morning.
Do not look at this post until you examine Post #32.
If you do, all your teeth will fall out and you will receive a failing grade in this class.
Upload the sketch below.
Tell us what you see ?
If you want to learn programming, you must ask questions on those things you do not understand.
You can change the animation to whatever you desire.
Test Time
Some questions for you:
What is the range of numbers an integer type (int) variable can represent on an Arduino UNO ?
How often do we check to see if the btnPin switch has changed state ?
Why do we divide the potentiometer reading by 4 and how is this value used ?
Why should we always document our lines of code ?
Do you see the benefit of using a heartbeat LED when we write our code ?
Is there any reason why the heartbeat code cannot be removed once we finalize our sketch ?
What is purpose of the variable commonFlag ?
What is the purpose of the variable ledIndex ?
Explain what this line of code does: ledIndex = ledIndex + incDec;
What does this line of code do (use Google)? byte pinTotal = sizeof(ledPins) / sizeof(ledPins[0]);
To make the LED animation run faster, say every 100ms, what do you need to do ?
What reason can you think of why we scan the switch every 50ms and not say every 1ms ? unsigned long switchInterval = 50ul; //milliseconds
//
//================================================^================================================
// 5 L E D s S w i t c h a n d P o t e n t i o m e t e r
//================================================^================================================
//
// https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599
//
//
//
// Version YY/MM/DD Comments
// ======= ======== ========================================================================
// 1.00 25/08/16 Running code.
//
//
//
// Notes:
//
//
//================================================
//Macros
#define LEDon HIGH
#define LEDoff LOW
#define CLOSED LOW
#define OPENED HIGH
#define ENABLED true
#define DISABLED false
//================================================
const byte ledPin1 = 3; //LED 1 attached to number 3 PWM pin
const byte ledPin2 = 5; //LED 2 attached to number 5 PWM pin
const byte ledPin3 = 6; //LED 3 attached to number 6 PWM pin
const byte ledPin4 = 9; //LED 4 attached to number 9 PWM pin
const byte ledPin5 = 10; //LED 5 attached to number 10 PWM pin
const byte heartbeatLED = 13; //LED should toggle every 500ms.
const byte btnPin = 2; //Push button attached to number 2.
const byte potPin = A0; //Potentiometer attached to A0.
byte lastBtnPin = OPENED;
byte ledPins[] = {3, 5, 6, 9, 10};
byte pinTotal = sizeof(ledPins) / sizeof(ledPins[0]);
int potValue = 0; //The value the potentiometer is set to. In this code we then divide by 4.
int ledIndex = 0; //Which LED, currently selected, we will operate on in our animation.
int incDec = 1; //1 means the LEDs are going OFF, -1 means LEDs are going ON.
//================================================
//Timing stuff . . .
//===================
//Define the heartbeat TIMER.
unsigned long heartbeatTime;
unsigned long heartbeatInterval = 500ul; //milliseconds
//===================
//Define the check switches TIMER.
unsigned long checkSwitchTime;
unsigned long switchInterval = 50ul; //milliseconds
//===================
//Define the common TIMER.
unsigned long commonTime;
unsigned long commonSlowInterval = 4000ul; //4 seconds
unsigned long commonFastInterval = 200ul; //200ms
unsigned long commonInterval = commonSlowInterval;
bool commonFlag = DISABLED; //DISABLED means the TIMER is not running.
// s e t u p ( )
//================================================^================================================
//
void setup()
{
Serial.begin(9600);
//Turn on the internal pull-up resistor.
pinMode(btnPin, INPUT_PULLUP);
//=======================
//Initialization of all elements in the LED array.
for (byte x = 0; x < pinTotal; x++)
{
//LEDs to be OFF at power up time.
digitalWrite(ledPins[x], LEDoff);
pinMode(ledPins[x], OUTPUT);
}
//The heartbeat LED gives us a rough indication if there is any code blocking,
//this LED should toggle every 500ms.
pinMode(heartbeatLED, OUTPUT);
} //END of setup()
// l o o p ( )
//================================================^================================================
//
void loop()
{
//======================================================================== T I M E R heartbeatLED
//Is it time to toggle the heartbeat LED ?
if (millis() - heartbeatTime >= heartbeatInterval)
{
//Restart this TIMER.
heartbeatTime = millis();
//Toggle the heartbeat LED.
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//======================================================================== T I M E R check switches
//Is it time to scan our switches (and Potentiometer) ?
if (millis() - checkSwitchTime >= switchInterval)
{
//Restart this TIMER.
checkSwitchTime = millis();
//Check for switch change in state (and read our potentiometer).
checkSwitches();
}
//======================================================================== T I M E R common
//If this TIMER is ENABLED, did this TIMER expired ?
if (commonFlag == ENABLED && millis() - commonTime >= commonInterval)
{
//Restart this TIMER.
commonTime = millis();
//We will now run this TIMER faster for the animation.
commonInterval = commonFastInterval;
//Proceed with the LED animation.
ledAnimation();
}
//================================================
// Other non-blocking code goes here
//================================================
} // END of loop()
// l e d A n i m a t i o n ( )
//================================================^================================================
//
void ledAnimation()
{
//=======================
//Are the LEDs going OFF. (i.e. incDec == 1 means LEDs are going OFF)
if (incDec == 1)
{
digitalWrite(ledPins[ledIndex], LEDoff);
}
//=======================
//Are the LEDs going to the ON brightness level. (i.e. incDec == -1 means LEDs are going ON)
else if (incDec == -1)
{
analogWrite(ledPins[ledIndex], potValue);
}
//=======================
//Next LED in the sequence.
ledIndex = ledIndex + incDec;
//============
//Have all the LEDs been turned OFF ?
if (ledIndex >= pinTotal)
{
//Get ready to turn ON the LEDs, change direction.
incDec = -1;
//Re-adjust to the last LED index.
ledIndex = pinTotal - 1;
}
//============
//Have all the LEDs been set to the ON brightness level ?
else if (ledIndex < 0)
{
//Get ready to turn LEDs OFF, change direction.
incDec = 1;
//Re-adjust to the 1st LED index.
ledIndex = 0;
}
} //END of ledAnimation()
// c h e c k S w i t c h e s ( )
//================================================^================================================
//
//We will check for switch change in state, as an added bonus,
//we will update the reading of the potentiometer.
//
void checkSwitches()
{
byte state;
//======================================================================== potPin
//Divide by 4 to get the brightness level the LEDs will be set to.
potValue = (analogRead(potPin)) / 4;
//End of reading the potentiometer(s).
//======================================================================== btnPin
state = digitalRead(btnPin);
//================================================
//Has this switch changed state ?
if (lastBtnPin != state)
{
//Update to the new state.
lastBtnPin = state;
//========================
//Is the common TIMER not running AND is this switch closed ?
if (commonFlag == DISABLED && state == CLOSED)
{
//Turn ON all the LEDs.
for (byte x = 0; x < pinTotal; x++)
{
digitalWrite(ledPins[x], LEDon);
}
//For the up coming animation, start with the 1st LED in our array.
ledIndex = 0;
//Set the common TIMER to the slow interval.
commonInterval = commonSlowInterval;
//Allow the common TIMER to run.
commonFlag = ENABLED;
//Restart the TIMER
commonTime = millis();
}
//========================
//If this the 2nd time we pushed the switch, turn the LEDs OFF, stop the animation
else if (commonFlag == ENABLED && state == CLOSED)
{
//Stop/disabled the common TIMER
commonFlag = DISABLED;
//Turn OFF all the LEDs.
for (byte x = 0; x < pinTotal; x++)
{
digitalWrite(ledPins[x], LEDoff);
}
}
} //END of this switch.
//======================================================================== nextSwitch
} //END of checkSwitches()
//================================================^================================================
Do not proceed with this Post until you are finished with Post #34
Upload the sketch below.
What do you see happening with your circuit ?
Ask questions.
What do these lines of code do ?
//Set the common TIMER to the Slow interval.
commonInterval = commonSlowInterval;
//Allow the common TIMER to run.
commonFlag = ENABLED;
//Restart the TIMER
commonTime = millis();
//
//================================================^================================================
// 5 L E D s S w i t c h a n d P o t e n t i o m e t e r
//================================================^================================================
//
// https://forum.arduino.cc/t/light-sweep-using-5-leds-toggle-button-for-on-off-potentiometer-for-brightness/1402599
//
//
//
// Version YY/MM/DD Comments
// ======= ======== ========================================================================
// 1.00 25/08/16 Running code.
//
//
//
// Notes:
//
//
//================================================
//Macros
#define LEDon HIGH
#define LEDoff LOW
#define CLOSED LOW
#define OPENED HIGH
#define ENABLED true
#define DISABLED false
//================================================
const byte ledPin1 = 3; //LED 1 attached to number 3 PWM pin
const byte ledPin2 = 5; //LED 2 attached to number 5 PWM pin
const byte ledPin3 = 6; //LED 3 attached to number 6 PWM pin
const byte ledPin4 = 9; //LED 4 attached to number 9 PWM pin
const byte ledPin5 = 10; //LED 5 attached to number 10 PWM pin
const byte heartbeatLED = 13; //LED should toggle every 500ms.
const byte btnPin = 2; //Push button attached to number 2.
const byte potPin = A0; //Potentiometer attached to A0.
byte lastBtnPin = OPENED;
byte someRandomNumber;
byte ledPins[] = {3, 5, 6, 9, 10};
byte pinTotal = sizeof(ledPins) / sizeof(ledPins[0]);
int potValue = 0; //The value the potentiometer is set to. In this code we then divide by 4.
int ledIndex = 0; //Which LED, currently selected, we will operate on in our animation.
int incDec = 1; //1 means the LEDs are going OFF, -1 means LEDs are going ON.
//================================================
//Timing stuff . . .
//
//===================
//Define the heartbeat TIMER.
unsigned long heartbeatTime;
unsigned long heartbeatInterval = 500ul; //milliseconds
//===================
//Define the check switches TIMER.
unsigned long checkSwitchTime;
unsigned long switchInterval = 50ul; //milliseconds
//===================
//Define the common TIMER.
unsigned long commonTime;
unsigned long commonSlowInterval = 4000ul; //4 seconds
unsigned long commonFastInterval = 200ul; //200ms
unsigned long commonInterval = commonSlowInterval;
bool commonFlag = DISABLED; //DISABLED means the TIMER is not running.
// s e t u p ( )
//================================================^================================================
//
void setup()
{
Serial.begin(9600);
//Turn on the internal pull-up resistor.
pinMode(btnPin, INPUT_PULLUP);
//=======================
//Initialization of all elements in the LED array.
for (byte x = 0; x < pinTotal; x++)
{
//LEDs to be OFF at power up time.
digitalWrite(ledPins[x], LEDoff);
pinMode(ledPins[x], OUTPUT);
}
//The heartbeat LED gives us a rough indication if there is any code blocking,
//this LED should toggle every 500ms.
pinMode(heartbeatLED, OUTPUT);
//Google randomSeed.
//Select an unused floating analog input.
randomSeed(analogRead(A5));
} //END of setup()
// l o o p ( )
//================================================^================================================
//
void loop()
{
//======================================================================== T I M E R heartbeatLED
//Is it time to toggle the heartbeat LED ?
if (millis() - heartbeatTime >= heartbeatInterval)
{
//Restart this TIMER.
heartbeatTime = millis();
//Toggle the heartbeat LED.
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//======================================================================== T I M E R check switches
//Is it time to scan our switches (and Potentiometer) ?
if (millis() - checkSwitchTime >= switchInterval)
{
//Restart this TIMER.
checkSwitchTime = millis();
//Check for switch change in state (and read our potentiometer).
checkSwitches();
}
//======================================================================== T I M E R common
//If this TIMER is ENABLED, did this TIMER expired ?
if (commonFlag == ENABLED && millis() - commonTime >= commonInterval)
{
//Restart this TIMER.
commonTime = millis();
//We will now run this TIMER faster for the animation.
commonInterval = commonFastInterval;
//Proceed with the LED animation.
ledAnimation();
}
//================================================
// Other non-blocking code goes here
//================================================
} // END of loop()
// l e d A n i m a t i o n ( )
//================================================^================================================
//
void ledAnimation()
{
//Get a random number from 0 to a maximum. (maximum is excluded see Google)
//0 to pinTotal minus 1
someRandomNumber = random(0, pinTotal);
analogWrite(ledPins[someRandomNumber], potValue);
//Get a random number from 0 to a maximum. (maximum is excluded see Google)
//0 to pinTotal minus 1
someRandomNumber = random(0, pinTotal);
digitalWrite(ledPins[someRandomNumber], LEDoff);
} //END of ledAnimation()
// c h e c k S w i t c h e s ( )
//================================================^================================================
//
//We will check for switch change in state, as an added bonus,
//we will update the reading of the potentiometer also.
//
void checkSwitches()
{
byte state;
//======================================================================== potPin
//Divide by 4 to get the brightness level the LEDs will be set to.
potValue = (analogRead(potPin)) / 4;
//End of reading the potentiometer.
//======================================================================== btnPin
state = digitalRead(btnPin);
//================================================
//Has this switch changed state ?
if (lastBtnPin != state)
{
//Update to the new state.
lastBtnPin = state;
//========================
//Is the common TIMER not running AND is this switch closed ?
if (commonFlag == DISABLED && state == CLOSED)
{
//Turn ON all the LEDs.
for (byte x = 0; x < pinTotal; x++)
{
digitalWrite(ledPins[x], LEDon);
}
//For the up coming animation, start with the 1st LED in our array.
ledIndex = 0;
//Set the common TIMER to the Slow interval.
commonInterval = commonSlowInterval;
//Allow the common TIMER to run.
commonFlag = ENABLED;
//Restart the TIMER
commonTime = millis();
}
//========================
//This is the 2nd time we pushed the switch, LEDs OFF, stop the animation
else if (commonFlag == ENABLED && state == CLOSED)
{
//Stop/disabled the common TIMER
commonFlag = DISABLED;
//Turn OFF all the LEDs.
for (byte x = 0; x < pinTotal; x++)
{
digitalWrite(ledPins[x], LEDoff);
}
}
} //END of this switch.
//======================================================================== nextSwitch
} //END of checkSwitches()
//================================================^================================================
There is a LED on the UNO PCB already connected to pin 13.
It is sufficient for you to use it as the heartbeat LED.
(no need for you to add an off board LED)