Hi y'all.
I am using basic basic coding which I took from the examples section in the arduino app, but am not sure how to achieve desired result.
I put together the knob and shift register sketches in the examples to control the speed and direction of a servo (modified for continuous rotation) and eight LEDs. I would like to sync the LEDs to be more of a reflection of the servo status. I know the code can be easily changed or modified to do this, but I am confused by the different options. Also, wondering if it's even possible, if it would be better to have the shift register/LEDs read from the servo position rather than the pot.
As it is right now, with the knob turned all the way left, the servo runs at full speed and slows down as the knob turns right toward its center position where the servo changes direction and picks up speed as the knob continues to the farthest right position. This is fine, though it would be nice to have a finer resolution incrementally.
However, the LEDs, which begin off with the knob turned all the way left, increase in number (0-8) until the knob is turned all the way right, with half the LEDs lit when knob and servo are midway.
Goal: I would like the LEDs to light all the way (1-8) with the speed of the servo and reverse as the servo slows down and reverses. I know that may involve mods to the INT or FOR and putting in something that has -- in contrast to the ++, but I have been unsuccessful in my experimentation. I am new at this arduino stuff. Any advice will be great. Thanks much.
Here is my current sketch:
#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 / 114; //1023 / 9
leds = 0;
for (int i = 0; i < numLEDSLit; i++)
{
bitSet(leds, i);
}
updateShiftRegister();
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}