Keep an analogRead for further calculation

Hello,

I am new to this board and also new to the Arduino. I have programmed before but I am not a professional. Also excuses for my english, it is not my mother-language.

Right now I am testing some stuff with the Arduino and the contents of a Starter-Kit.

I am using an RGBB-LED and want to make something like a mood light. I have one Potentiometer and one Push-Button. I wired everything on my breadboard and it works so far.

What I want to accomplish is that I cycle with the push-button through the different colors of the LED and adjust them with the potentiometer. Like, I start the Sketch on the Arduino and with the potentiometer I adjust the first color with the potentiometer, then I push the button to adjust the second color and so on. What I just described is working so far. What I want to do now is that while cycling through the colors not the actual reading of the potentiometer is instantly altering the intensity of the color to the actual reading of the potentiometer.

For example the analog output to the LED color red is 130 and the reading from the potentiometer is 220. Now I dont want the analog output to change to 220, I´d more rather like it to stay at 130 until I turn the potentiometer to 130 and start altering the color from that point on. I hope that was understandable.

My problem is that I can not keep the reading from the potentiometer locked in a variable for checking it against it´s actual reading.

This is how far I got with my code until now. The comments are in german and part of my variables as well.

// Mit diesem Programm sollen die 4 LEDs in der RGB LED gesteuert werden. Wenn man den Button drückt wird auf die nächste LED geschaltet.
// Die jeweilige LED soll dann mit dem Potentiometer rauf bzw. runter geregelt werden. Dadurch können die Farben gemixt werden.

// Pinbelegungen
// Pins für die LEDs
#define blue1 10
#define green 9
#define blue2 6
#define red 5

//Pin für den Button
#define buttonPin 3

//Pin für das Poti
#define potiPin A0

//Varibalen
byte buttonCounter = 0;
byte buttonState = 0;
byte lastButtonState = 0;

int potiWert = 0;

byte blue1Wert = 5;
byte greenWert = 5;
byte redWert = 5;
byte blue2Wert = 5;
byte blue1WertAlt = 5 ;
byte greenWertAlt = 5;
byte redWertAlt = 5;
byte blue2WertAlt = 5;
//byte ledAlt = 0;


void setup(){
  pinMode(blue1, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(blue2, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(potiPin, INPUT);
  Serial.begin(9600);
}

void loop(){
  
/* An dieser Stelle wird der Status des Buttons überprüft. Wird der Button gedrückt, wird die
Variable buttonCounter um 1 erhöht. Es wird von 0 bis 3 gezählt. Wenn buttonCounter größer als
3 wird, dann wird buttonCounter auf 0 zurück gesetzt. */

  // Setze buttonState auf den aktuellen Wert (Button gedrückt oder nicht gedrückt).
  buttonState = digitalRead(buttonPin);
  // Wenn buttonState ungleich dem alten Status wird abgefragt, ob der Button gedrückt ist.
  if (buttonState != lastButtonState){
    if (buttonState == HIGH){
      // Ist Button gedrückt buttonCounter erhöhen.
      buttonCounter++;
      // Wenn buttonCounter über 3 dann wird wieder auf 0 gestellt
      if (buttonCounter > 3){
        buttonCounter = 0;
      }
    }
  }
  // Variable zum überprüfen des Wechsels von gedrückt zu ungedrückt und umgekehrt.
  lastButtonState = buttonState;
  
  // Das Potentiometer wird ausglesen und der Wert durch 4 geteilt, damit er den PWM Werten der LED-Ausgänge entspricht.  
  potiWert = analogRead(potiPin)/4;
  
  // Die verschiedenen Farben der LED werden angeschaltet.
  analogWrite(blue1, blue1Wert);
  analogWrite(green, greenWert);
  analogWrite(red, redWert);
  analogWrite(blue2, blue2Wert);
  
  
   
  switch (buttonCounter){
      
    case 0:
    blue2WertAlt = blue2Wert;
    if (blue1WertAlt <= potiWert){
      blue1Wert = farbwechselRauf(blue1WertAlt);
    }
    else{
      blue1Wert = farbwechselRunter(blue1WertAlt);
    }
    break;
    
    case 1:
    blue1WertAlt = blue1Wert;
    if (greenWertAlt <= potiWert){
      greenWert = farbwechselRauf(greenWertAlt);
    }
    else{
      greenWert = farbwechselRunter(greenWertAlt);
    }
    break;
    
    case 2:
    greenWertAlt = greenWert;
    if (redWertAlt <= potiWert){
      redWert = farbwechselRauf(redWertAlt);
    }
    else{
      redWert = farbwechselRunter(redWertAlt);
    }
    break;
    
    case 3:
    redWertAlt = redWert;
    if (blue2WertAlt <= potiWert){
      blue2Wert = farbwechselRauf(blue2WertAlt);
    }
    else{
      blue2Wert = farbwechselRunter(blue2WertAlt);
    }
    break;
   }
}

byte farbwechselRunter(byte ledAlt){
  Serial.println("DOWNDOWNDOWN");
  while (potiWert >= ledAlt){
    return ledAlt;
  }
  return potiWert;
}

byte farbwechselRauf(byte ledAlt){
  Serial.println("UPUPUPUPUP");
  while (potiWert <= ledAlt){
    return ledAlt;
  }
  return potiWert;
}

If anybody could help me in understanding how I store an anlogRead and fix it in a variable (in this case blue1WertAlt, greenWertAlt, redWertAlt and blue2WertAlt), I would be really thankfull. :slight_smile:

Thanks in regard,
EinTyp

case 0:
    blue2WertAlt = blue2Wert;
if ( blue1WertAlt < (potiWert * 11/10)  && blue1WertAlt > (potiWert * 9/10)  // 20% intercept zone
{
    if (blue1WertAlt <= potiWert){
      blue1Wert = farbwechselRauf(blue1WertAlt);
    }
    else{
      blue1Wert = farbwechselRunter(blue1WertAlt);
    }
}
    break;

Probably, you could define NN% intercept zone, before allow adjustment

Thanks Magician,

the idea with the intercept zone is great. :slight_smile:

I think I have not explained my problem that good though.

My code morks more or less like this (example for one color):

potiWert = anologRead(potiPin)/4;
redWert = potiWert;
redWertAlt = redWert;

Because the analogRead modifies the variable permanently I can not keep redWertAlt for example at 120 to use it for calculating the difference to the actual value of the potentiometer. I think that explains my problem much better. :slight_smile:

Is there some trick to accomplish that?

-EinTyp

Now I dont want the analog output to change to 220, I´d more rather like it to stay at 130 until I turn the potentiometer to 130 and start altering the color from that point on

It was absolutely not clear, but it’s fine.
There is no problem to calculate the difference, what next? You would define a threshold?
If so , situation repeat itself:

 if ( potiWert < 11*(anologRead(potiPin)/4)/10) ..... &&  etc;
potiWert = anologRead(potiPin)/4;

The trick is to save the value you read into another variable at the end of your function so that you can compare it with the new value you read at the start of the function.

Thanks for your Input. :slight_smile:

Actually I had problems in saving a constant value of the potentiometer in a variable. I am trying some things out now.

This is only a temporary solution anyways, since I only have one poti at the moment. I guess I am going to buy two more to change every color by itself. Seems to be a much nicer way to adjust the colors.

-EinTyp