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
}
}
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
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
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?
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 ?
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; "
}
}
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...?
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);~~ }
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.
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);
}
}
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));
}