save value (in a simple Code)

Hi Guys, I am trying to write a simple Code with a modification. I want to control a Led with a potentiometer. I wrote a Code that compares values. Normally the code/arduino measure permanently the analog value and write permanently the pwm-Signal. Now I want that the Code compare the analog value and if the code measure a new analog value than it should send a pwm-signal.

My Code doesn’t work. I don’t know how I can save the new value "valOLD" for the next pass.

(I visualized the code)

int ledPin = 9;      // LED connected to digital pin 9

int analogPin = 1;   // potentiometer connected to analog pin 1

int valNEW = 0;         // variable to store the read value
int valOLD = 0;         // variable to store the read value


void setup()

{
pinMode(ledPin, OUTPUT);   // sets the pin as output
Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
}

void loop()
{
  valNEW = analogRead(analogPin);   // read the current val from Poti
  if (valNEW != valOLD)              // compares the values
 {
 int pwm = (valNEW / 4);
 analogWrite(ledPin, pwm);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 Serial.println(pwm);        

 pwm = valOLD;  // Put pwm value as "valOLD"  AND save this val for the next pass
 }
}

Thanks for helping. :)

pwm = valOLD;  // Put pwm value as "valOLD"  AND save this val for the next pass

Assignment puts the value on the right into the thing on the left. This is setting pwm to equal whatever was in valOLD.

Perhaps you meant:

valOLD = pwm;

But really, since you are comparing with valNEW to look for a change, maybe it would make more sense to keep up with valNEW instead of pwm.

So maybe this:

valOLD = valNEW;

Use the code button so your code looks like this

int ledPin = 9;      // LED connected to digital pin 9

int analogPin = 1;   // potentiometer connected to analog pin 1

int valNEW = 0;         // variable to store the read value
int valOLD = 0;         // variable to store the read value


void setup()

{
pinMode(ledPin, OUTPUT);   // sets the pin as output
Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
}

void loop()
{
  valNEW = analogRead(analogPin);   // read the current val from Poti
  if (valNEW != valOLD)              // compares the values
 {
 int pwm = (valNEW / 4);
 analogWrite(ledPin, pwm);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
 Serial.println(pwm);       

 pwm = valOLD;  // Put pwm value as "valOLD"  AND save this val for the next pass
 }
}

I think you have this line back to front

pwm = valOLD;  // Put pwm value as "valOLD"  AND save this val for the next pass

try

valOLD = pwm;  // Put pwm value as "valOLD"  AND save this val for the next pass

...R

Hi,

  valNEW = analogRead(analogPin);   // read the current val from Poti
  if (valNEW != valOLD)              // compares the values

ValNew contains the current/latest value of the potentiometer value. If it's different from the old value, we are updating the PWM, which is fine.

If a new value of the potentiometer has been read/detected, then valOLD should be updated with the value of valNEW , not with the value of PWM (which has been divided by 4).

As a result, I think the following line :

valOLD = pwm;  // Put pwm value as "valOLD"  AND save this val for the next pass

should be :

valOLD = valNEW;  // Put pwm value as "valOLD"  AND save this val for the next pass
  • dan

Many thanks for the reply.

I have changed the code but I can see in the serial monitor that the Arduino still send permanently new PWM-Signal to the LED although I don’t change the Poti signal.

The Arduino reset the Value after it ends the loop.

How can I save the value for one time for following Loop pass?

Hi LLucas, thanks for the feedback.

I have changed the code but I can see in the serial monitor that the Arduino still send permanently new PWM-Signal to the LED

What changes have you done? You've received a couple of suggested changes. Would it be possible to post your updated code, just so that we do not make any frustrating assumptions ?

Many thanks in advance,

  • dan

Iam sorry...

Here is the Code:

int ledPin = 9;      // LED connected to digital pin 9

int analogPin = 1;   // potentiometer connected to analog pin 1

int valNEW = 0;         // variable to store the read value
int valOLD = 0;         // variable to store the read value


void setup()

{
pinMode(ledPin, OUTPUT);   // sets the pin as output
Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
}



void loop()

{
  valNEW = analogRead(analogPin);   // read the current val from Poti
  if (valNEW != valOLD)              // compares the values
    {
      int pwm = valNEW / 4;
      analogWrite(ledPin, pwm);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
      Serial.println(pwm);        

    // my first try with this row : "  valOLD = pwm;  " 
  
 // my second try with this row:  "  valOLD = valNEW;  "  

    }
}

LLucas: Many thanks for the reply.

I have changed the code but I can see in the serial monitor that the Arduino still send permanently new PWM-Signal to the LED although I don’t change the Poti signal.

The Arduino reset the Value after it ends the loop.

How can I save the value for one time for following Loop pass?

int ledPin = 9;      // LED connected to digital pin 9
int analogPin = 1;   // potentiometer connected to analog pin 1
int valOld = 0;         // variable to store the read value

void setup()

{
  pinMode(ledPin, OUTPUT);   // sets the pin as output
  Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
}

void loop()
{
  int val = analogRead(analogPin);
  if (val != valOld)
  {
    analogWrite(ledPin, map(val, 0, 1023, 0, 255));
    Serial.println(map(val, 0, 1023, 0, 255));
    valOld = val;
  }
}

not tested but I think this is what you want... so update the led only when the pot value changes...?

The analog value jumps around a little bit due to noise. So the value is never the same. You need to introduce some hysteresis.

Thank You for the reply

@BulldogLowell: The code doesn’t work, if I open the serial monitor, it doesn’t stop to write pwm-signal. @aarg: iam searching at the moment

LLucas: The code doesn’t work, if I open the serial monitor, it doesn’t stop to write pwm-signal.

Can you expand on what exactly you mean by that? What are you actually seeing? What do you want to see?

i open the serial monitor and see this:

240 //pwm signal 240 240 ...

till i rotate the poti

or

150 150 150

or

200 200

Constantly calling analogWrite(ledPin, pwm); even though the value of PWM does not change won't do any harm.

Constantly showing the value on the Serial Monitor could be annoying.

Just realized your code already does this - sorry for my blindness You could do something like this to check for a changed value int oldPwm = pwm; static int pwm = valNEW / 4; // ========NOTE STATIC if (pwm != oldPwm) { ~~ analogWrite(ledPin, pwm); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255~~ ~~ Serial.println(pwm);~~ }

...R

Robin2: Constantly calling analogWrite(ledPin, pwm); even though the value of PWM does not change won't do any harm.

Is that really the case? Does the function protect against a timing reset by remembering the last phase and frequency? If they were that smart, then my hat goes off to them.

aarg: Is that really the case? Does the function protect against a timing reset by remembering the last phase and frequency?

I strongly suspect it does not.

I did not / do not think that matters for the OP's purposes (but it would not be the first time I have been wrong).

...R

Here is the solution

Guys, I have a new concept to realize my project but I need your help. I create a Arrays in my code. There a two fields / two measurement: Val[0] = x / the first measurement from analog pin Val[1] = y / the second measurement from analog pin

If the two value unequal { int pwm = (y / 4); analogWrite(ledPin, pwm); }

The Idea is to compare two consecutive value instead of to save the value (if you see my first code concept) Here is the code but it doesn’t work, the arduino programm displays an error:

" sketch_sep22a:2: error: initializer fails to determine size of 'val' sketch_sep22a:2: error: array must be initialized with a brace-enclosed initializer initializer fails to determine size of 'val' "

int ledPin = 9;      // LED connected to digital pin 9
float val[] = 1;    // potentiometer connected to analog pin 1
void setup()
{
pinMode(ledPin, OUTPUT);   // sets the pin as output
Serial.begin(9600);             // opens serial port, sets data rate to 9600 bps
}
void loop()
{
if (val[0] != val[1])              // compares the values
    {
     int pwm = (val[1] / 4);   //create pwm-signal
analogWrite(ledPin, pwm);  
     Serial.println(pwm);        
    }
}

LLucas:
Here is the code but it doesn’t work,

You don’t know whether your code works because it does not compile.

Using floats without reason is not overly clever.
Analog does not mean floating point.

Analog values have to be read before they can be used.

This will at least compile:

const byte ledPin = 9;      // LED connected to digital pin 9
const byte anaPin = A1;  // potentiometer connected to analog pin 1
byte val[2];
    
void setup()
{
pinMode(ledPin, OUTPUT);   // sets the pin as output
Serial.begin(9600);        // opens serial port, sets data rate to 9600 bps
}

void loop()
{
  val[0] = analogRead(anaPin);
  val[1] = analogRead(anaPin);
  if (val[0] != val[1])
    {
     int pwm = (val[1]>>2);
     analogWrite(ledPin, pwm); 
     Serial.println(pwm);       
    }
}

Rethink your logic.

If the analogRead would not fluctuate (a litte) two direct consecutive reads
would show no difference (unless you turn the poti very fast).

Thank you at all that you try to help me but i give up now. i haven't any ideas any more.

i have the same problem all the time with any code modification:

best regards :)

Don’t give up.

Learn C (by reading books, watching tutorials, programming),
it’s really not that hard and there is no way around it.

i will start studying c with "C Primer Plus (Developer's Library)" because i will start my study in mrz 2016 in a university

C Primer Plus is a huge Literature :D