Hi y'all. It's been a while since i worked on this project, but it's in front of me again. i did try the suggested code adjustments and struggled with my lack of experience and knowledge in these areas however, I was able to get a basic set up running for the time being.
It has been so long since i looked at this code and stuff, I am having to relearn everything so far all over again. I have been experimenting quite a bit more though which may have only made things worse as I've tangentted off into too many directions.
So, here I am, just trying to make an led level meter which shows the position/direction of my continuous rotation servo controlled by potentiometer.
I have eliminated the shift register and switched to the map function, but as before, I can only get the leds to light in a straight sequence. I have problems understanding which parts of code to change and to what sometimes, so i come up with so many ways to resolve it, but then don't know the best way to execute. i am currently wondering if i should treat the two different directions of the servo should be mapped to two different led arrays - thoughts anyone?
here is my code:
#include <Servo.h>
// these constants won't change:
const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 7; // the number of LEDs in the bar graph
const int servoPin = 3;
Servo servo;
int ledPins[] = {
7,8,9,10,11,12,13 }; // an array of pin numbers to which LEDs are attached
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
servo.attach(servoPin);
}
}
void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
int angle = sensorReading / 6;
servo.write(angle);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}