save value (in a simple Code)

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

That's a very good start.

Without basics you are lost, ...and not only while programming.

This Code woks:
The Analog-Signal from Poti Jumps and I integrated an average function who measures 100 values.

int ledPin = 9;      // LED connected to digital pin 9
int analogPin = 5;   // potentiometer connected to analog pin 1
float pwmOLD = 0;    //  variable to store the read value 
float pwmNEW = 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() 
{ 
  // calculate average
  // average from 100 measurements
    pwmNEW=0;
      for(int i=0;i<100;i++)
      {
        
        pwmNEW+=(analogRead(analogPin)/4);
      }
      pwmNEW=trunc(pwmNEW/100);
    
      if (pwmNEW != pwmOLD)              // verleicht die Werte valNEW und valOLD 
            {             
               analogWrite(ledPin, pwmNEW);  // PWM-Signal send to LED-Pin   
               Serial.print("                     PWM-Signal:"); // PWM-Signal on S-Monitor if available 
               Serial.println(pwmNEW);      
               pwmOLD = pwmNEW;  // Put pwm value as "valOLD"  AND save this val for the next pass
             }
  Serial.print("Analogsignal:"); // Analog-Signal on S-Monitor if available 
  Serial.println(analogRead(analogPin));
            
}

What exactly do you mean "jumps"? Varies enough to be seen as a brightness fluctuation of the LED? That isn't a known problem with this hardware. This kind of experiment is very common on Arduino.

What happens when you run the example sketch "AnalogInOutSerial" that comes with the IDE?