Code for LEDs + Sound Working with Potentiometer

Status
Not open for further replies.

YeloLem

Member
Hello,

I built something like a miniature space and I am trying to get a potentiometer to fade in and out of various lighting/sound situations in the space, but I am not really sure how to go about this for the code. So as the potentiometer is turned I want a certain sound file to correspond to a certain setup of leds (I have around 3-4 leds per pin). As of now I have the potentiometer set to pin A1 and the leds set to pin 3 on a teensy 3.2. Someone suggested to me that I try using something like this:

HTML:
int red = 0;
int redPin = 3;

void setup() {
pinMode(redPin, OUTPUT);
}

void loop() {
  int knob = analogRead(A1);
  if (knob < 50) {
  transitionOne();
    }

  void transitionOne() {
    for (int i = 0; i <255; i++);{
    if (red <50){
      red++;
    }
    else if (red = 50){                                          
    }
    else{
    red--;
    }
    }
analogWrite(redPin,red);
}

This would just be a start to the leds.

I am a beginner, so my knowledge of code is very limited. As I tested this code it says "transitionOne(); was not declared in this scope." I'm not sure how to fix this. Does anyone know how I can get this code working? Any other suggestions for going about this are greatly appreciated to.

Thank you.
 
Do a Ctrl+T to format the code in the IDE - that will show unbalanced braces - also makes forum reading better.

The function transitionOne() is actually inside loop() - the brace to end loop() is in the wrong place.

This line is doing an assignment not a compare: " else if (red = 50){ "
If should be : " else if (red == 50){ "

Once you get that cleaned up it will start to run, then you will see it isn't likely to do what you expect as red will count up to 50 - then it will stay at 50. That isn't what was intended I assume - if it did get to the final else and do red--, that would take it to 49 then next loop would take it back to 50, which wouldn't be very exciting. If you get stuck there repost your code.
 
Ooh thanks for the format tip. After making changes the leds light up and the potentiometer does nothing so I must have still missed something.

HTML:
int red = 0;
int redPin = 3;

void setup() {
  pinMode(redPin, OUTPUT);
}

void loop() {
  int knob = analogRead(A1);
  if (knob < 50) {
    transitionOne();
  }
}

void transitionOne() {
  for (int i = 0; i < 255; i++); {

    if (red < 50) {
      red++;
    }
    else if (red == 50) {
    }
    else {
      red--;
    }
  }
  analogWrite(redPin, red);
}
 
Still an issue if (red == 50) you don't change red so it will forever stay 50.

Maybe instead of using ++ and -- you might setup a variable, like maybe incr which you init to 1, and add that to red. If it then gets beyond some value, change incr to -1 and if the value gets too low change back to +1...
 
Thanks for the suggestion. Sorry this is stressing me out so my mind is processing really slowly - how might I setup this variable? Would it be something like red += 1? Would I change everything under void transitionOne()?
 
The value of knob is from the pot - but it really doesn't do much except when :: if (knob < 50)

You say "the potentiometer does nothing". If it is indeed working it will only have a very small active area (under 50) where the value range would be 0-4095. For debug and perhaps use you could make a "static int last_knob" and print its value when it changes to see it work and over what range.

I'm not clear on what effect you want to achieve with regard to the knob? You could have it affect the value of "red" or only call the transition when it changes? Or maybe you want to have 80 affects and each will respond during a 50 wide interval with this being the first one?

Perhaps this that will run up to 50 then down and repeat a few times very fast with a change of only 1 each iteration - again a small part of the full range:
Code:
void transitionOne() {
  static int change_red = 1;
  for (int i = 0; i < 255; i++); {
    if (red <= 0) {
      change_red = 1;
    }
    else if (red >= 50) {
      change_red = -1;
    }
  }
  red+= change_red;
  analogWrite(redPin, red);
}


BTW - to embed code it uses this with the number 0's shown replaced with letter O's :: [C0DE] // your code here [/C0DE]
 
My bad, I need to work on my explanation. So my idea is to have up to 4 different transitions of lights, physically translating into 4 different areas on the knob. The example I can think of is like a radio. So I want the starting position to be off, then transitionOne to fade into a specific set of lights at position 50, the second transition to fade into another set of lights at maybe position 200, etc. With every transition the previous set of lights should fade out. I hope that makes sense.

I tested the code and the leds start on then sometimes goes brighter/fades lower when I the knob position is turned to 50.
Oh, I was using the HTML tags heh heh woops.
 
Maybe the most simple way to word it is as the knob is turned the first set of leds crossfade into the next set and so on.
 
you can map the encoder, and then use a set of 'case' statements. an example for discreet colours could be:
where stepNum=.. is your colour function such as setPixel(colour);
Code:
int sensorReading = analogRead(A4);
int range = map(sensorReading, divMin, divMax, 0, 6);

switch (range) {
    case 0:    
stepNum =0;// or instead, replace with doColourFade();//function
      break;
    case 1:    
stepNum=6;
      break;
    case 2:    
stepNum =18;
      break;
    case 3:    
stepNum=42;
      break;
    case 4:    
stepNum =66;
   break;
    case 5:    
stepNum =90;
break;
}

where doColourFade is something like
void doColourFade() {
//do the sweep here
};

you can also use some logic statements, so that you use a fade colour function that get triggered if moving from one step to the other only in a given direction. With this approach you would use a variable to hold the current state to compare it to the new state, or simply to check if the reading from the pin was going up or down. This way you would have a set of functions that each would scroll through a given part of your colour fade.
 
Last edited:
Status
Not open for further replies.
Back
Top