Instructables

feedback from potentiometer to aurduino to slow?

so this is the code i use but i would like it to be reading the value of the potentiometer after each number appears on the 7 digit display 
i put in the serial read to tell if my poteniometer was working correctly and just because it is fun to watch the numbers fly by.



const int l1=2;
const int l2=3;
const int l3=4;
const int l4=5;
const int l5=6;
const int l6=7;
const int l7=8;


void setup () {
  Serial.begin(9600);
  pinMode(l1, OUTPUT);
  pinMode(l2, OUTPUT);
  pinMode(l3, OUTPUT);
  pinMode(l4, OUTPUT);
  pinMode(l5, OUTPUT);
  pinMode(l6, OUTPUT);
  pinMode(l7, OUTPUT);
  }
  void loop() {
    int dt = analogRead(A0);
    delay(1);
    Serial.println(dt);
     digitalWrite(l5, HIGH);
     digitalWrite(l3, HIGH);
     delay(dt);
     digitalWrite(l5, LOW);
     digitalWrite(l3, LOW);
     delay(dt);

     digitalWrite(l6,HIGH);
      digitalWrite(l5,HIGH);
      digitalWrite(l4,HIGH);
      digitalWrite(l1,HIGH);
      digitalWrite(l2,HIGH);
     delay(dt);
       digitalWrite(l6,LOW);
      digitalWrite(l5,LOW);
      digitalWrite(l4,LOW);
      digitalWrite(l1,LOW);
      digitalWrite(l2,LOW);
       delay(dt);

       digitalWrite(l6, HIGH);
       digitalWrite(l5, HIGH);
       digitalWrite(l4, HIGH);
       digitalWrite(l3, HIGH);
       digitalWrite(l2, HIGH);
       delay(dt);
       digitalWrite(l6, LOW);
       digitalWrite(l5, LOW);
       digitalWrite(l4, LOW);
       digitalWrite(l3, LOW);
       digitalWrite(l2, LOW);
       delay(dt);

       digitalWrite(l7, HIGH);
       digitalWrite(l4, HIGH);
       digitalWrite(l5, HIGH); 
       digitalWrite(l3, HIGH); 
       delay(dt);
       digitalWrite(l7, LOW);
       digitalWrite(l4, LOW);
       digitalWrite(l5, LOW); 
       digitalWrite(l3, LOW); 
       delay(dt);

       digitalWrite(l6, HIGH);
       digitalWrite(l7, HIGH);
       digitalWrite(l4, HIGH);
       digitalWrite(l3, HIGH);
       digitalWrite(l2, HIGH);
       delay(dt);
       digitalWrite(l6, LOW);
       digitalWrite(l7, LOW);
       digitalWrite(l4, LOW);
       digitalWrite(l3, LOW);
       digitalWrite(l2, LOW);
       delay(dt);

       digitalWrite(l6, HIGH);
       digitalWrite(l7, HIGH);
       digitalWrite(l4, HIGH);
       digitalWrite(l1, HIGH);
       digitalWrite(l2, HIGH);
       digitalWrite(l3, HIGH);
       delay(dt);
       digitalWrite(l6, LOW);
       digitalWrite(l7, LOW);
       digitalWrite(l4, LOW);
       digitalWrite(l1, LOW);
       digitalWrite(l2, LOW);
       digitalWrite(l3, LOW);
       delay(dt);

       digitalWrite(l6, HIGH);
       digitalWrite(l5, HIGH);
       digitalWrite(l3, HIGH);
       delay(dt);
       digitalWrite(l6, LOW);
       digitalWrite(l5, LOW);
       digitalWrite(l3, LOW);
       delay(dt);

       digitalWrite(l1, HIGH);
       digitalWrite(l2, HIGH);
       digitalWrite(l3, HIGH);
       digitalWrite(l4, HIGH);
       digitalWrite(l5, HIGH);
       digitalWrite(l6, HIGH);
       digitalWrite(l7, HIGH);
       delay(dt);
       digitalWrite(l1, LOW);
       digitalWrite(l2, LOW);
       digitalWrite(l3, LOW);
       digitalWrite(l4, LOW);
       digitalWrite(l5, LOW);
       digitalWrite(l6, LOW);
       digitalWrite(l7, LOW);
       delay(dt);
  }

rangenmi2 years ago
I think I see what you're trying to do.
Cycle through the digits of a seven-seg display at
a rate that varies with the value of your pot right?

Add
dt =analogRead(A0);
Before each
delay(dt);

Right now your program reads dt in once at the beginning and uses that value for all the delays until the program loops and starts again.
By adding this you'll read the value of dt in after each digit has been displayed, allowing you to vary that delay. If i remember right, the max value you can read in is 1024, and when.used in conjuction with the delay() function your delay between
Digits should be anywhere between 0 and 1024 milliseconds.
The delay, dt is proportional to your analog reading ! Of course it can't go any faster !

You COULD of course use say, dt/5 or dt/10 as a delay still proportional to analog read, but a lot faster.h
The Arduino can read the analog as fast as you see it run across the screen in the serial window. But when its writing to a pin its not able to simultaneously read from the analog pin. Doing all the turning off and on of the LEDs in the display is slowing the entire process down. Especially if your adding any sort of delay between all those digital write groups.
harry88 (author)  mpilchfamily2 years ago
So is that the fastest it is going to get?
If you remove everything from the sketch except the analog read and the serial print then you will see it as fast as its able to read it. Everything else in the sketch is slowing things down. Especially the delays you have set.