vjpcat:
Try:
#include <Servo.h>
int potPin = 0;
int servoPin = 9;
Servo servo;
int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int leds = 0;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
servo.attach(servoPin);
}
void loop(){
int reading = analogRead(potPin); // 0 to 1023
int angle = reading / 6; //0 to 100-ish
servo.write(angle);
int numLEDSLit = reading / 228; //You only want max. 4 LEDs lit.
leds = 0;
if (angle >= 55){ //positive half of turn
for (int i = 4; i < (numLEDSLit+4); i++){
bitSet(leds, i); // lights LEDs 4 to 7
}
}
else if (angle<=45){ //negative half of turn
for (int i = (numLEDSLit -1) ; i >-1; i--){
bitSet(leds, i);
} // lights LEDs 3 to 0
}
// angles between 46 and 54 (centre of turn) will have no LEDs lit
updateShiftRegister();
}
void updateShiftRegister(){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
Also look at the map function.