save potentiometer values in array

If you're going to overwrite it, it doesn't really matter if it is initialised.

No you can change the values at any time.

Val[0][3] = analogRead(0);

That sort of thing

ok mike that makes good sense. thanks.

rexhex:
ok... so i know i should probably stop and figure this out on my own. But i looked it up and feel this should work.
i get "invalid types 'int[3][int[3]' for array subscript"
but rgb is a array of 3 and i have it set up as int place[3][9];
im thinking that if this worked then i would just call to fade column.

void savecolor()
{
if (fadechoose == HIGH)
{
rpotval = rgb[0];
gpotval = rgb[1];
bpotval = rgb[2];

column = place[9][rgb];

}

Post all of your code so we can recreate the error message. A lot depends on how things are declared.

void savecolor()
{
  if (fadechoose == HIGH)
  {
    rpotval = rgb[0];
    gpotval = rgb[1];
    bpotval = rgb[2];

 int place[9][3] = {{rpotval, gpotval, bpotval},
 {rpotval, gpotval, bpotval},
 {rpotval, gpotval, bpotval},
 {rpotval, gpotval, bpotval},
 {rpotval, gpotval, bpotval},
 {rpotval, gpotval, bpotval},
 {rpotval, gpotval, bpotval},
 {rpotval, gpotval, bpotval},
 {rpotval, gpotval, bpotval}};

  }

}

I'm not sure of the point of making an array, where everything in it is the same, which is never used.

Hello everyone, I have been busy so have not had time to work on the code much. But today in my programing lab I decided it was more relevant to work on my new idea for this program rather than the lab assignment. This is what I got and I think it should work but its not. Its possible that Im setting up the push button wrong though because its my first time using one. I had my teacher look at it and she thinks it should work but says she does not understand how this language loops all the time. So here it is.

//led pins 
const byte red =11;
const byte green =9;
const byte blue =10;

//potentiometer pins
const int rpot =A3;
const int gpot =A4;
const int bpot =A5;

//potentiometer values
int rpotval = 0;
int gpotval = 0;
int bpotval = 0;

//buttons 
int fadeChooseCounter = 0;
int fadeChooseState =0;
int lastFadeChooseState =0;
int fadeon =2; 
int fadechoose =3;

//packages the values 
int rgb[3]={
  0};
int place[9][3]={
  {
    0  }
};
int column = 0;

int fadeval1;
int fadeval2;
int fadeval3;

// saved values
int black[3] = {
  0, 0, 0};
int redVal= black[0];
int grnVal= black[1];
int bluVal= black[2];
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
int rval1;
int gval1;
int bval1;

void setup()
{
  //declair pins functions 
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(fadechoose, INPUT);
  pinMode(fadeon, INPUT);

  Serial.begin(9600);
}
void loop()
{
  // reads potentiometers value and turns it from 1023 to 255 
  rpotval = map(analogRead(rpot), 0, 1024, 0, 255);
  gpotval = map(analogRead(gpot), 0, 1024, 0, 255);
  bpotval = map(analogRead(bpot), 0, 1024, 0, 255);

  // sends values to leds so you can pick full range of colors
  analogWrite(red, rpotval);
  analogWrite(green, gpotval);
  analogWrite(blue, bpotval);
  delay(3);
}
int calculateStep(int prevValue, int endValue)
{
  int step = endValue - prevValue;
  if (step) 
  {
    step = 255/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;
}


//stores the color in memory 
void savecolor()
{
  fadeChooseState = digitalRead(fadeon);

  if (fadeChooseState != lastFadeChooseState)
  {
    if (fadeChooseState == HIGH) 
    {
      fadeChooseCounter++;
    }
  }
  
  place[fadeChooseCounter][0] = analogRead(rpot);
  place[fadeChooseCounter][1] = analogRead(gpot);
  place[fadeChooseCounter][2] = analogRead(bpot);

  
  lastFadeChooseState = fadeChooseState;

}
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 <= 255; i++)
  {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);


    analogWrite(red, redVal);
    analogWrite(green, grnVal);
    analogWrite(blue, bluVal);


  }
}

// fades  stored values 
void displaycolors()
{


  if(fadeon == HIGH)
  {

crossFade (place[fadeChooseCounter]);

  }

  delay(3);
Serial.println(fadeChooseState);
  Serial.println("rval1");
  Serial.println (rval1);
  Serial.println("gval1");
  Serial.println(gval1);
  Serial.println("bval1");
  Serial.println(bval1);
  Serial.println(" red output ");
  Serial.println(rpotval);
  Serial.println("green output ");
  Serial.print(gpotval);
  Serial.println(" blue output ");
  Serial.print(bpotval);


  delay(200);
}

Can you describe how you have your button(s) connected. Have you got any resistors connected to them to make sure that the input is held at a known value (high or low) when the button is not pressed ?

One obvious thing about your code is that although you have a function called savecolor() it is not called in the program

You have 2 inputs defined, fadechoose and fadeon but you never read from fadechoose. Which button should save the values in the array ?

This is what I got and I think it should work but its not.

Code always works. It does exactly what you tell it to. If that is what you think you have told then it is fine.
So what do you think it should do that it does not?

You never look at the buttons in the loop so they can't do anything can they!

Where are you calling the function that is supposed to save data in the array? Where are you using those stored values? What evidence do you have that there is a problem?

I have the button connected on one side to a 10k resistor to ground, and also connected to the pin on the arduino. the other side is connected to the 5v source. Fadechoose is what should be saving the values into the place array, Fadeon is what should be displaying the saved values. The reason I say there is a problem is that when I upload the code to my arduino and plug it all in it does not store the values or display them in a fade sequence. I see that I forgot to see if the button was pressed though. This is what I think I need to do to fix that problem, but I wont be home for a while to test it.

void savecolor()
{
  fadeChooseState = digitalRead(fadeon);

  if (fadeChooseState != lastFadeChooseState)
  {
    if (fadeChooseState == HIGH) 
    {
      fadeChooseCounter++;
    }
  }
if (fadechoose = HIGH)
{
  
  place[fadeChooseCounter][0] = analogRead(rpot);
  place[fadeChooseCounter][1] = analogRead(gpot);
  place[fadeChooseCounter][2] = analogRead(bpot);
}
  
  lastFadeChooseState = fadeChooseState;

}
if (fadechoose = HIGH)

= != ==

And where is that function called?

I forgot to put == and did a = thanks for the catch. I thought that by making the function it was being called but I guess that does no really make sense. Would I write it like I would with a main and header file? So in the loop() call the function?

Call it just like you do with all the other functions used by your program such a analogRead(), pinMode() and crossfade()

Oh! because Im not writing in digitalRead? So this is what your talking about?

//led pins 
const byte red =11;
const byte green =9;
const byte blue =10;

//potentiometer pins
const int rpot =A3;
const int gpot =A4;
const int bpot =A5;

//potentiometer values
int rpotval = 0;
int gpotval = 0;
int bpotval = 0;

//buttons 
int fadeChooseCounter = 0;
int fadeChooseState =0;
int lastFadeChooseState =0;
int fadeon =2; 
int fadechoose =3;

//packages the values 
int rgb[3]={
  0};
int place[9][3]={
  {
    0  }
};
int column = 0;

int fadeval1;
int fadeval2;
int fadeval3;

// saved values
int black[3] = {
  0, 0, 0};
int redVal= black[0];
int grnVal= black[1];
int bluVal= black[2];
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
int rval1;
int gval1;
int bval1;

void setup()
{
  //declair pins functions 
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(fadechoose, INPUT);
  pinMode(fadeon, INPUT);

  Serial.begin(9600);
}
void loop()
{
  // reads potentiometers value and turns it from 1023 to 255 
  rpotval = map(analogRead(rpot), 0, 1024, 0, 255);
  gpotval = map(analogRead(gpot), 0, 1024, 0, 255);
  bpotval = map(analogRead(bpot), 0, 1024, 0, 255);

  // sends values to leds so you can pick full range of colors
  analogWrite(red, rpotval);
  analogWrite(green, gpotval);
  analogWrite(blue, bpotval);
  delay(3);
}
int calculateStep(int prevValue, int endValue)
{
  int step = endValue - prevValue;
  if (step) 
  {
    step = 255/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;
}


//stores the color in memory 
void savecolor()
{
if(digitalRead(fadechoose) == HIGH)
{
  fadeChooseState = digitalRead(fadeon);

  if (fadeChooseState != lastFadeChooseState)
  {
    if (fadeChooseState == HIGH) 
    {
      fadeChooseCounter++;
    }
  }
  
  place[fadeChooseCounter][0] = analogRead(rpot);
  place[fadeChooseCounter][1] = analogRead(gpot);
  place[fadeChooseCounter][2] = analogRead(bpot);
}
  
  lastFadeChooseState = fadeChooseState;

}
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 <= 255; i++)
  {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);


    analogWrite(red, redVal);
    analogWrite(green, grnVal);
    analogWrite(blue, bluVal);


  }
}

// fades  stored values 
void displaycolors()
{


  if(digitalRead(fadeon) == HIGH)
  {

crossFade (place[fadeChooseCounter]);

  }

  delay(3);
Serial.println(fadeChooseState);
  Serial.println("rval1");
  Serial.println (rval1);
  Serial.println("gval1");
  Serial.println(gval1);
  Serial.println("bval1");
  Serial.println(bval1);
  Serial.println(" red output ");
  Serial.println(rpotval);
  Serial.println("green output ");
  Serial.print(gpotval);
  Serial.println(" blue output ");
  Serial.print(bpotval);


  delay(200);
}

rpotval = map(analogRead(rpot), 0, 1024, 0, 255); or, less wastefully, rpotval = analogRead(rpot) / 4;

You are still not calling the savecolor() function.
If the fadechoose button is supposed to save the values then read it and if is pressed call the function. At the moment your code reads the button inside a function that is never called so the button will never be read anyway.

Ok, I think I know what your talking about and reading this has helped clarify. However I am still slightly lost. To call a function do I just need to put fadechoose(); inside of void loop() ? So the code runs through void loop constantly and all the new functions are never looked at unless you call them, but if you put the function name in the loop it will then call the function?

//led pins 
const byte red =11;
const byte green =9;
const byte blue =10;

//potentiometer pins
const int rpot =A3;
const int gpot =A4;
const int bpot =A5;

//potentiometer values
int rpotval = 0;
int gpotval = 0;
int bpotval = 0;

//buttons 
int fadeChooseCounter = 0;
int fadeChooseState =0;
int lastFadeChooseState =0;
int fadeon =2; 
int fadechoose =3;

//packages the values 
int rgb[3]={
  0};
int place[9][3]={
  {
    0  }
};
int column = 0;

int fadeval1;
int fadeval2;
int fadeval3;

// saved values
int black[3] = {
  0, 0, 0};
int redVal= black[0];
int grnVal= black[1];
int bluVal= black[2];
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
int rval1;
int gval1;
int bval1;

void setup()
{
  //declair pins functions 
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(fadechoose, INPUT);
  pinMode(fadeon, INPUT);

  Serial.begin(9600);
}
void loop()
{
{
  // reads potentiometers value and turns it from 1023 to 255 
  rpotval = analogRead(rpot)/4; 
  gpotval = analogRead(gpot)/4;
  bpotval = analogRead(bpot)/4;

  // sends values to leds so you can pick full range of colors
  analogWrite(red, rpotval);
  analogWrite(green, gpotval);
  analogWrite(blue, bpotval);
  delay(3);
}
int calculateStep(int prevValue, int endValue)
{
  int step = endValue - prevValue;
  if (step) 
  {
    step = 255/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;
}
savecolor();
displaycolors();
}

//stores the color in memory 
void savecolor()
{
if(digitalRead(fadechoose) == HIGH)
{
  fadeChooseState = digitalRead(fadeon);

  if (fadeChooseState != lastFadeChooseState)
  {
    if (fadeChooseState == HIGH) 
    {
      fadeChooseCounter++;
    }
  }
  
  place[fadeChooseCounter][0] = analogRead(rpot);
  place[fadeChooseCounter][1] = analogRead(gpot);
  place[fadeChooseCounter][2] = analogRead(bpot);
}
  
  lastFadeChooseState = fadeChooseState;

}
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 <= 255; i++)
  {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);


    analogWrite(red, redVal);
    analogWrite(green, grnVal);
    analogWrite(blue, bluVal);


  }
}

// fades  stored values 
void displaycolors()
{


  if(digitalRead(fadeon) == HIGH)
  {

crossFade (place[fadeChooseCounter]);

  }

  delay(3);
Serial.println(fadeChooseState);
  Serial.println("rval1");
  Serial.println (rval1);
  Serial.println("gval1");
  Serial.println(gval1);
  Serial.println("bval1");
  Serial.println(bval1);
  Serial.println(" red output ");
  Serial.println(rpotval);
  Serial.println("green output ");
  Serial.print(gpotval);
  Serial.println(" blue output ");
  Serial.print(bpotval);


  delay(200);
}

rexhex:
Ok, I think I know what your talking about and reading this has helped clarify. However I am still slightly lost. To call a function do I just need to put fadechoose(); inside of void loop() ? So the code runs through void loop constantly and all the new functions are never looked at unless you call them, but if you put the function name in the loop it will then call the function?

http://arduino.cc/en/Reference/FunctionDeclaration

Basically yes. Why did your teacher not tell you that? I am not impressed with her.
So what happens with the new code?

My teacher only helped me by verifying that I was using the array correctly and showing me a better way of using it. I did not ask her for any other help. I think that my code is very wrong now :~ . For the crossFade Im using code I found a long time ago, and never understood how it worked. I still dont fully understand it, but Im getting closer. The way that I understand code so far tells me what Im doing should work, however Im not getting the results I want. I decided to put the analogReads for the 3 pots into its own function to see if I could just call it too, it did not work. My LEDs are just white. nothings working. Im going to talk to my teacher about it tomorrow, hopefully she might be able to help me but I dont think she fully understands how the arduino language works, or maybe my code is confusing her. This is where im at with it.

//led pins 
const byte red =11;
const byte green =9;
const byte blue =10;

//potentiometer pins
const int rpot =A3;
const int gpot =A4;
const int bpot =A5;

//potentiometer values
int rpotval = 0;
int gpotval = 0;
int bpotval = 0;

//buttons 
int fadeChooseCounter = 0;
int fadeChooseState =0;
int lastFadeChooseState =0;
int fadeon =2; 
int fadechoose =3;

//packages the values 
int rgb[3]={
  0};
int place[9][3]={
  {
    0  }
};
int column = 0;

int fadeval1;
int fadeval2;
int fadeval3;

// saved values
int black[3] = {
  0, 0, 0};
int redVal= black[0];
int grnVal= black[1];
int bluVal= black[2];
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
int rval1;
int gval1;
int bval1;

void setup()
{
  //declair pins functions 
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(fadechoose, INPUT);
  pinMode(fadeon, INPUT);

  Serial.begin(9600);
}
void loop()
{
  //check buttons
savecolor;
displaycolors;
potfun;
}
int calculateStep(int prevValue, int endValue)
{
  int step = endValue - prevValue;
  if (step) 
  {
    step = 255/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;
}


//stores the color in memory 
int savecolor()
{
  fadeChooseState = digitalRead(fadeon);

  if (fadeChooseState != lastFadeChooseState)
  {
    if (fadeChooseState == HIGH) 
    {
      fadeChooseCounter++;
    }
  }
  
  return place[fadeChooseCounter][0] = analogRead(rpot);
  return place[fadeChooseCounter][1] = analogRead(gpot);
  return place[fadeChooseCounter][2] = analogRead(bpot);

  
  lastFadeChooseState = fadeChooseState;

}
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 <= 255; i++)
  {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);


    analogWrite(red, redVal);
    analogWrite(green, grnVal);
    analogWrite(blue, bluVal);

delay (3);
  }
}

// fades  stored values 
void displaycolors()
{


  if(fadeon == HIGH)
  {

return crossFade (place[fadeChooseCounter]);

  }
}
  // reads potentiometers value and turns it from 1023 to 255 
  int potfun()
  {
  rpotval = analogRead(rpot)/4;
  gpotval = analogRead(gpot)/4;
  bpotval = analogRead(bpot)/4;

  // sends values to leds so you can pick full range of colors
  analogWrite(red, rpotval);
  analogWrite(green, gpotval);
  analogWrite(blue, bpotval);
  delay(3);
}
  savecolor;
  displaycolors;
  potfun;

Do you see any other functions in your program called like this ?
No

pinMode(fadeon, INPUT);
digitalRead(fadeon);
analogWrite(red, rpotval);

Notice that all the function calls have brackets after the function names. This allows you to pass parameters to a function so that it can use them to make decisions such as which pin to set the mode for and what value the mode should be set to. The brackets are mandatory and if you don't want to pass any parameters to a function you must still use the brackets like this

savecolor();

Once you have managed to call savecolor() you will find that it does not work because of other things being wrong (see below) but at least get your code working to call the functions. You can prove that the functions are being called by putting Serial.print statements in then like this

int savecolor()
{
  Serial.println("In savecolor");
  fadeChooseState = digitalRead(fadeon);

  if (fadeChooseState != lastFadeChooseState)

If you don't see the messages then the function has not been called.

You have code in savecolor()

  return place[fadeChooseCounter][0] = analogRead(rpot);
  return place[fadeChooseCounter][1] = analogRead(gpot);
  return place[fadeChooseCounter][2] = analogRead(bpot);

The function will end and go back to where it was called from when it finds the first return command. In general you cannot return more than one value from a function but do you need to ? The place array is defined as a global variable and is accessible by all parts of your program.