I am currently trying to control an LED bargraph using a 4017 decade counter as in this example:
My question is how would I do this without using delay? I have looked at the BlinkwithoutDelay code and this was my attempt, however it does not work. Any help is appreciated!
/*
* LED Bar Graph controlled with an external potentiometer.
*
* A 4017 counter is used to save pins on the arduino.
* The technique of "Persistence of vision" is used to turn on
* many LEDs at the same time.
*
* Developed by Leonel Machava
* http://codentronix.com
*
* This code is release under the "MIT License" available at
* http://www.opensource.org/licenses/mit-license.php
*/
/* Digital pin connected to the counter's clock pin */
int clockPin = 7;
/* Digital pin connected to the counter's reset pin */
int resetPin = 6;
/* Analog pin connected to the potentiometer wiper */
int potPin = 0;
long clockTimer = 0;
long resetTimer = 0;
int clockState = LOW;
int resetState = LOW;
void setup() {
pinMode(clockPin,OUTPUT);
pinMode(resetPin,OUTPUT);
digitalWrite(resetPin,HIGH);
delay(1);
digitalWrite(resetPin,LOW);
}
void loop() {
/* Read the analog value from the potentiometer. */
int potValue = analogRead(potPin);
/* Map the value to the range 0-9. */
int n = potValue * 10 / 1024;
/* Turn ON/OFF quickly the first n LEDs. The n LEDs
will appear to be ON at the same time due to the
"persistence of vision" effect. */
long currentMillis = millis();
if(currentMillis - clockTimer > 100) {
clockTimer = currentMillis;
for( int i = 0; i < n; i++ ) {
if (clockState == HIGH) {
clockState = LOW;
}
else {
clockState = HIGH;
}
digitalWrite(clockPin, clockState);
}
}
if(currentMillis - resetTimer > 100) {
resetTimer = currentMillis;
if (resetState == HIGH) {
resetState = LOW;
}
else {
resetState = HIGH;
}
digitalWrite(resetPin, resetState);
}
}