I've been struggling with modifying some code to do something different and have yet to get anywhere with my original post seen here:
http://forum.arduino.cc/index.php?topic=234474.msg1911106
I would like to pose my question differently regarding the code to follow.
The code I have posted here is a bar graph sketch to be used with a servo (modded for continuous rotation), pot and LEDs. It works much like a volume control with the LEDs lighting up as the 'volume' is turned up and going off as the 'volume' is turned down.
What I would like to do is modify this code to work more like a speaker fade/balance control with the LEDs lighting up to the left of center and back or to the right of center and back, in conjunction with the servo, instead of from the left to the right and back.
Perhaps there is a sketch that exists that does just this, but if not, can the following code be modified to work as such or do I need to use a completely different set of functions?
Thanks for your help.
#include <Servo.h>
// these constants won't change:
const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 9; // the number of LEDs in the bar graph
const int servoPin = 3;
Servo servo;
int ledPins[] = {
5,6,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);
}
}
}