Fading between two LED's with an analog pot

DISCLAIMER: Total noob :smiley:

If this is in the wrong forum, I’m really sorry.

My girlfriend got me an arduino duemilanove kit over christmas, and I’ve been playing around with sample codes and whatnot. It is really getting interesting ;D

I make a lot of analog circuits for music stuff, so I’ve been trying to come up with a project to use my new digital fun chip with. I’ve decided a digital wah would be fun (digital pot control, that is), and I’m having two problems with completeing it.

The first is fading between 2 LED’s. The pot in the wah enclosure can be used into the arduino as a voltage divider with the 5v, so I am trying to set it up so that as you move the rocker forward, the red LED fades in (blue fading out) and as you move it back, the blue LED fades in (as well as red fading out).

The second part of this project is to have the same analog pot control a digital pot at the same time, but I need to get this LED part done first :slight_smile:

So, I took the fading between 3 LED code and modified it a bit to get it to fade between 2 LED’s. It is fading the blue LED all the way out, then at the middle of the pot, the blue LED is dimmed all the way, and the red begins to fade in. What I want ( ::)) it to do is to have each LED brightest at either end of the sweep, but have them fade in and out at the same time, not one then the other.

Here is the code I’m using

// INPUT: Potentiometer should be connected to 5V and GND
int potPin = 3; // Potentiometer output connected to analog pin 3
int potVal = 0; // Variable to store the input from the potentiometer

// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED’s cathodes should be connected to digital GND
int redPin = 9; // Red LED, connected to digital pin 9
int bluPin = 11; // Blue LED, connected to digital pin 11

// Program variables
int redVal = 0; // Variables to store the values to send to the pins
int bluVal = 0;

int DEBUG = 1; // Set to 1 to turn on debugging output

void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(bluPin, OUTPUT);

if (DEBUG) { // If we want to see the pin values for debugging…
Serial.begin(9600); // …set up the serial ouput in 0004 format
}
}

// Main program
void loop()
{
potVal = analogRead(potPin); // read the potentiometer value at the input pin

if (potVal < 511) // Lowest third of the potentiometer’s range (0-340)
{
potVal = (potVal * 2) / 4; // Normalize to 0-255

redVal = 256 - potVal; // Red from full to off
bluVal = 1; // Blue off
}
else if (potVal < 1023) // Middle third of potentiometer’s range (341-681)
{
potVal = ( (potVal-1023) * 2) / 4; // Normalize to 0-255
redVal = 1; // Red off
bluVal = potVal; // Blue from off to full
}

analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(bluPin, bluVal);

if (DEBUG) { // If we want to read the output
DEBUG += 1; // Increment the DEBUG counter
if (DEBUG > 100) // Print every hundred loops
{
DEBUG = 1; // Reset the counter
// Serial output using 0004-style functions
Serial.print(“R:”); // Indicate that output is red value
Serial.print(redVal); // Print red value
Serial.print("\t"); // Print a tab
Serial.print(“B:”);
Serial.println(bluVal); // println, to end with a carriage return
}
}
}

My brain is totally fried. If anyone has any input on how to modify this code, I would much appreciate it. Also, if there is an easy control code that I haven’t found, that would be a big help, if this way isn’t the best way to do it.

Thanks a lot :slight_smile:

First thing to point out is that some of your comments do not match the code. While this does not affect the code, two weeks from now, when you look at the code again, and assume the comments are right, you'll spend a lot of time looking elsewhere for problems.

Second thing is that all-the-way-off is 0, not 1. ! is just very,very dim.

Third is that the level for each LED is either 255 - potval/4 or potval/4. You're doing some weird stuff with potval.

If I read what you wanted correctly, you want one led off, and the other full brightness with the pot at one extreme, and the intensity levels to swap places when the pot is all the way at the other end.

void loop()
{
   potVal = digitalRead(potPin) / 4;
   redVal = 255 - potVal;
   bluVal = potVal;

   analogWrite(redPin, redVal);
   analogWrite(bluPin, bluVal);
}

With this code, potVal ranges from 0 to 255. When potVal is 0, redVal will be 255, and bluVal will be 0. As potVal increases to 10, redVal will be 245, abd bluVal will be 10. When potVal gets to 255, redVal will be 0 and bluVal will be 255.

Lesson #1 on the forums. Learn how to post code.

In the edit window you will see an wide range of options above the edit window. When posting code you will want to highlight the code then click on the button with the # symbol on it. This will give the code the proper tags so it sows up like this.

Your Code Here!

Part of the problem your having is not defining the ranges properly.

if (potVal > 0 && potVal < 511)  // Lowest third of the potentiometer's range (0-340)
 {                  
   //do something here
 }
 if (potVal > 511 && potVal < 1023) // Middle third of potentiometer's range (341-681)
 {
  // do something here
 }

You don’t need an else in there. Just a new if() statement. The way you had it the second condition was going to execute every time.

Or you could use the ‘map’ function.

Quick code (probably contains errors)

if(potVal <= 512){
potVal = map(potVal; 0,512, 255, 0)
analogWrite(redLED, potVal);
}
else if(potVal >512){
potVal = map(potVal; 513, 1023, 0, 255)
analogWrite(blueLED, potVal);
}

Something like that anyway. Look up the map function, it will help you a load.

Mowcius

Thanks a lot for all the help. Sorry about the code part :-[ I wasn't aware of the code posting.

As for the notes, I was copying and pasting from all over the place, without really paying attention to the notes, but I will keep better track now.

PaulS, that is what I was trying to do, but couldn't figure it out. It seem so easy now :-[

Unfortuantly, the cheapish pot that came with the kit has died a smokey death. I've written up a code, but I can't check it.

int potPin = 2;                      // select the input pin for the potentiometer
int bluLED = 9;                      // select the input pin for the blue LED
int redLED = 11;                     // select the input pin for the red LED
int potVal = 0;                      // variable to store the value coming from the sensor
int bluVal = 0;
int redVal = 0;

void setup() {
  pinMode(bluLED, OUTPUT);           // declare the bluLED as an OUTPUT
  pinMode(redLED, OUTPUT);           // declare the redLED as an OUTPUT
}

void loop() {
  potVal = analogRead(potPin);       // read the value from the sensor
  potVal = potVal/4;                 // convert from 0-1023 to 0-255
  bluVal = potVal;
  redVal = 255 - potVal;
  analogWrite(bluLED, bluVal);       // fades blue LED on/off
  analogWrite(redLED, redVal);       // fades red LED on/off
}

I am a bit unsure about the int blu and red val = 0 at the beginning, but it didn't seem to like when it wasn't defined.

Thanks again for helping me out, I really appreciate it.

Ok, I would also still use map (from my above example) but that code looks ok. A few alterations:

const int potPin = 2;                      // select the input pin for the potentiometer
const int bluLED = 9;                      // select the input pin for the blue LED
const int redLED = 11;                     // select the input pin for the red LED
int potVal = 0;                      // variable to store the value coming from the sensor
int bluVal = 0;
int redVal = 0;

void setup() {
  pinMode(bluLED, OUTPUT);           // declare the bluLED as an OUTPUT
  pinMode(redLED, OUTPUT);           // declare the redLED as an OUTPUT
  pinMode(potPin, INPUT);               // declare the potPin as an INPUT
}

void loop() {
  potVal = analogRead(potPin);       // read the value from the sensor
  potVal = potVal/4;                 // convert from 0-1023 to 0-255
  bluVal = potVal;
  redVal = 255 - potVal;
  analogWrite(bluLED, bluVal);       // fades blue LED on/off
  analogWrite(redLED, redVal);       // fades red LED on/off
}

Yeah shame about the pot, I suggest you go buy a few, they are always useful and if you buy a few they can be really cheap.

Mowcius

Unfortuantly, the cheapish pot that came with the kit has died a smokey death

That's...not easy to do. :o

Depends on what kind of pot it was, and where the kit came from...

@ mowcius - thanks for the amendment to the code. I didn't look too far into the maps command because what PaulS has posted before made a lot of sense. I have a page with the maps command book marked for reading though :slight_smile: Thanks for the help!

The pot was a pretty cheap 9mm through-hole that came with the kit from sparkfun (I think). It was a little wonky to begin with, as I could tell from the LED brightness from rotation, it skipped sometimes. Maybe there was a short somewhere, not sure. I do have a lot of 16mm and 24mm pots, but I left them at home when I went back for Christmas break :-/ If I can get anything to work, I'll let you guys know.

I was looking around the forum, and I must say, you guys sure know your way around with this stuff! Lots of knowledge floating around here.

Thanks again for everyones help.

I was looking around the forum, and I must say, you guys sure know your way around with this stuff! Lots of knowledge floating around here.

Well we pretend to at least!

Mowcius