analog read/write help

I posted about this already but have not received any help for it. Maybe its because last post i really did not know what i was doing, but its all becoming more clear. could someone give me some advice/ show me something to read that will help me come up with the solution.

I want a pot to read its value and write the speed at which the LEDs fade together. Im positive this can be done.

the code below includes what im at with a very important part of this. also it include a direction i think it would need to go to get this working. im just going to keep plugging away over here so i would really appreciate some help. thanks fellow arduino people.

int calculateStep(int prevValue, int endValue)
    {
      int step = endValue - prevValue;
      if (step) 
      {
        step = 1020/step;
      }
      return step; 
    }
    
    int calculateVal(int step, int val, int i) 
{
if ((step) && i % step == 0)
{
  if (step > 0)
  { 
    val += 1;
  }
  else if (step < 0) 
  {
    val -= 1;
  }
}
//calculate step/val is the math for what controls the fade function 

if (val > 255)
{
  val = 255;
}
if (val < 0)
{
  val = 0;
}
return val;
}

void crossFade(int color[3])
// this says that 'crossFade' controls 3 intigers and gives them the names R,G,B
{ 
  int R = color[0];
  int G = color[1];
  int B = color[2];
  
  int stepR = calculateStep(prevR, R);
  int stepG = calculateStep(prevG, G);
  int stepB = calculateStep(prevB, B);
  
  //when the loop starts over it says to move from last color value to new through the math equation
  //which is said at the bottom of the code for the loop
  
  for (int i = 0; i <= 1020; i++)
  // im not too sure about this.. it says value = 0 and value is less than or equal too 1020 
  // and increase value 
  {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);
    
    // rbgVal is the calculated of fade from last value to new value, ???, and the value...
    
 analogWrite(redPin, redVal);
 analogWrite(grnPin, grnVal);
 analogWrite(bluPin, bluVal);
 
 // tell the pins to do this
 
 delay(fade);
 
 // slow this process down what ever number was given to 'fade'
 
//  ??delay(analogRead(sensorPin));?? maybe starting off with something like this? instead of delay(fade)
{
  
 
   if (DEBUG)
 {
   if (i == 0 or i % loopCount == 0)
   {
     Serial.print("loop/RGB: #");
     Serial.print(i);
    Serial.print(" | ");
   Serial.print(redVal);
  Serial.print(" / ");
 Serial.print(grnVal);
Serial.print(" / ");
Serial.print(bluVal);
   }
   DEBUG += 1; 
 }
  }
  prevR = redVal;
  prevG = grnVal;
  prevB = bluVal;
  delay(hold);
}

Moderator edit: code tags added.

Read this before posting a programming question

I want a pot to read its value and write the speed at which the LEDs fade together. Im positive this can be done.

What happens? What do you expect to happen?

what happens is you put a number = to fade. this number will determine the speed at which the colors fade together. what i want to have happen is assign pot to change this speed. maybe it needs to change the fade number? i have been working on having a pot control the delay time.

If you are going to post code, can you post code that compiles please?

sketch_oct19a.cpp: In function 'void crossFade(int*)':
sketch_oct19a:43: error: 'prevR' was not declared in this scope
sketch_oct19a:44: error: 'prevG' was not declared in this scope
sketch_oct19a:45: error: 'prevB' was not declared in this scope
sketch_oct19a:54: error: 'redVal' was not declared in this scope
sketch_oct19a:55: error: 'grnVal' was not declared in this scope
sketch_oct19a:56: error: 'bluVal' was not declared in this scope
sketch_oct19a:60: error: 'redPin' was not declared in this scope
sketch_oct19a:61: error: 'grnPin' was not declared in this scope
sketch_oct19a:62: error: 'bluPin' was not declared in this scope
sketch_oct19a:66: error: 'fade' was not declared in this scope
sketch_oct19a:74: error: 'DEBUG' was not declared in this scope
sketch_oct19a:76: error: 'loopCount' was not declared in this scope
sketch_oct19a:93: error: 'hold' was not declared in this scope
sketch_oct19a:94: error: expected `}' at end of input

We don't want to guess what your code is, we want to see it.

ok i see sorry about that i was thinking thats all that was needed.

int redPin = 11;
int grnPin = 5;
int bluPin = 6;
int potPin = A0;

int sensorValue = 0;
int outputValue = 0;

int black[3] = {255, 255, 255 };
int red[3] = { 0, 255, 255 };
int greenBlue[3] = { 255, 0, 0 };
int purplePink[3] = { 0, 255, 0 };
int tangerine[3] = { 0, 226, 255 };
int lemonLime[3] = { 0, 0, 255 };
int guesswork[3] = { 200,50,250};

int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];

int fade = 4;
int hold = 1;
int DEBUG = 1;
int loopCount = 60;
int repeat = 0;
int j = 0;

int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;

void setup()
{

pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);

if (DEBUG){
Serial.begin(9600);
}
}

void loop()
{

crossFade(greenBlue);
crossFade(purplePink);
crossFade(tangerine);
crossFade(lemonLime);
crossFade(red);
crossFade(guesswork);

}

int calculateStep(int prevValue, int endValue)
{
int step = endValue - prevValue;
if (step)
{
step = 1020/step;
}
return step;
}

int calculateVal(int step, int val, int i)
{
if ((step) && i % step == 0)
{
if (step > 0)
{
val += 1;
}
else if (step < 0)
{
val -= 1;
}
}

if (val > 255)
{
val = 255;
}
if (val < 0)
{
val = 0;
}
return val;
}

void crossFade(int color[3])
{
int R = color[0];
int G = color[1];
int B = color[2];

int stepR = calculateStep(prevR, R);
int stepG = calculateStep(prevG, G);
int stepB = calculateStep(prevB, B);

for (int i = 0; i <= 1020; i++)
{
redVal = calculateVal(stepR, redVal, i);
grnVal = calculateVal(stepG, grnVal, i);
bluVal = calculateVal(stepB, bluVal, i);

analogWrite(redPin, redVal);
analogWrite(grnPin, grnVal);
analogWrite(bluPin, bluVal);

// delay(fade); this is what works and what i put under
// before } is what im working with to control the pot

sensorValue = analogRead(potPin);
outputValue = map(sensorValue, 0, 1023, 0, 500);
delay = analogWrite( redVal, grnVal, bluVal);
}

{

if (DEBUG)
{
if (i == 0 or i % loopCount == 0)
{
Serial.print("loop/RGB: #");
Serial.print(i);
Serial.print(" | ");
Serial.print(redVal);
Serial.print(" / ");
Serial.print(grnVal);
Serial.print(" / ");
Serial.print(bluVal);
}
DEBUG += 1;
}
}
prevR = redVal;
prevG = grnVal;
prevB = bluVal;
delay(hold);
}

and thank you for you time :slight_smile:

Did you read reply #1?

Read this before posting a programming question

Please read about putting your code into code tags.

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

just clicked the button the to right of it. sorry about that..

This is a cross-post isn't it?

http://arduino.cc/forum/index.php/topic,127837.0.html

Thread locked.

Please don't cross-post. Stick with one thread.