how to store last pwm output to eeprom

iam a student and not an expert in c languange, maybe soon :slight_smile:

rotary encoder to set pwm output, but i want if button is pressed, last pwm output at pin 11 stored to eeprom

i know my edited code is mistake, n i want to ask u for a right code.

this code not realy created by me, i just copy from some web and i forgot the link source

thanks

 #include <EEPROM.h>
 #define PWM 11
 #define EN_PIN_A 12 
 #define EN_PIN_B 8 
 #define BTN_PIN 4
 
 unsigned char encoder_A;
 unsigned char encoder_B;
 unsigned char last_encoder_A;
 unsigned char last_encoder_B; 
 int power = 0; 
 int power_step = 1; 
 long loop_time; 
 long button_time;
 bool last_button_state;

 void setup() {

 pinMode(EN_PIN_A, INPUT_PULLUP);
 pinMode(EN_PIN_B, INPUT_PULLUP); 
 pinMode(BTN_PIN, INPUT_PULLUP);
 analogWrite(PWM, 0);
 } 
 void loop() { 
 int Val = EEPROM.read(100);
 int val = analogWrite;
 long current_time = millis(); .
 bool btn = digitalRead(BTN_PIN); 
 if( btn!=last_button_state && current_time - button_time > 20 ){ 
 if( btn==LOW && last_button_state ){
 EEPROM.write(100, val);
 
 } 
 last_button_state = btn;
 button_time = current_time;
 } if( current_time - loop_time >= 5 ){
 encoder_A = digitalRead(EN_PIN_A);
 encoder_B = digitalRead(EN_PIN_B);
 if( !encoder_A && last_encoder_A ){
 if( encoder_B ){
 power = power - power_step;
 }else{ 
 power = power + power_step;

 }
 if( power < 0 ) power=0;
 if( power >= 255 ) power=255;
 if( power >= 0 && power <=10 ){
 } 
 analogWrite(PWM, power);
 } 
 last_encoder_A = encoder_A; 
 last_encoder_B = encoder_B;
 loop_time = current_time;
 } 
 }

voidnoob:
iam a student and not expert in c languange, maybe soon :slight_smile:

rotary encoder to set pwm output, but i want if button is pressed, last pwm output at pin 11 stored to eeprom

i know my edited code is mistake, n i want to ask u for a right code.

this code not realy created by me, i just copy from some web and i forgot the link source

thanks

 #include <EEPROM.h>

#define PWM 11
#define EN_PIN_A 12
#define EN_PIN_B 8
#define BTN_PIN 4

unsigned char encoder_A;
unsigned char encoder_B;
unsigned char last_encoder_A;
unsigned char last_encoder_B;
int power = 0;
int power_step = 1;
long loop_time;
long button_time;
bool last_button_state;

void setup() {

pinMode(EN_PIN_A, INPUT_PULLUP);
pinMode(EN_PIN_B, INPUT_PULLUP);
pinMode(BTN_PIN, INPUT_PULLUP);
analogWrite(PWM, 0);
}
void loop() {
int Val = EEPROM.read(100);
int val = analogWrite;
long current_time = millis(); .
bool btn = digitalRead(BTN_PIN);
if( btn!=last_button_state && current_time - button_time > 20 ){
if( btn==LOW && last_button_state ){
EEPROM.write(100, val);

}
last_button_state = btn;
button_time = current_time;
} if( current_time - loop_time >= 5 ){
encoder_A = digitalRead(EN_PIN_A);
encoder_B = digitalRead(EN_PIN_B);
if( !encoder_A && last_encoder_A ){
if( encoder_B ){
power = power - power_step;
}else{
power = power + power_step;

}
if( power < 0 ) power=0;
if( power >= 255 ) power=255;
if( power >= 0 && power <=10 ){
}
analogWrite(PWM, power);
}
last_encoder_A = encoder_A;
last_encoder_B = encoder_B;
loop_time = current_time;
}
}

Welcome to the Arduino forum. And I hope you do well with your project.

But explain a bit more about your project. You ask about storing the last PWM output. Your last PWM output will be either 0 or 1. Because that pin is putting out a square wave which goes between 0 and whatever the VCC of your Arduino is being powered with.

What is it ACTUALLY you want to store?

Paul

 int Val = EEPROM.read(100);
 int val = analogWrite;

It is a bad idea to have two variables where the only difference is the case.

Assigning the address of the analogWrite() function to val seems pointless.

long current_time = millis(); .

Why is there a stray character on the end of that line?

Your code looks like crap. Put EVERY { on a line BY ITSELF. Put EVERY } on a line BY ITSELF. Use Tools + Auto Format to properly indent it.

Why is power an int, when you constrain the value in the variable to be in the range 0 to 255? Writing an int to EEPROM, and reading it back, is twice as hard as writing a byte. Which you never actually do.

analogWrite;

Something missing perhaps

What is it ACTUALLY you want to store?

if encoder turn clock wise, voltage at pin 11 is count up from 0 to 5volt, and vice verza
for example if my encoder stop run and pin 11 reading by volt meter at 3v, when sw encoder pressed, last voltage at pin 11 saved to eeprom
so if arduino powered off and on, voltage at pin 11 still at 3v

thanks for reply, yes mr. PaulS, i know i was wrong and need your guadiance.

iam 13 years old

As has been pointed out the voltage of a PWM output pin will always be 0 or 5V so you can't save the value of the voltage, nor can you measure it with a meter. What you could save is the value written to the PWM pin that provides the apparent output voltage.

UKHeliBob:
As has been pointed out the voltage of a PWM output pin will always be 0 or 5V so you can't save the value of the voltage, nor can you measure it with a meter. What you could save is the value written to the PWM pin that provides the apparent output voltage.

i want like this example, speed still on last rpm after turn off, but at my project i want last voltage

What should I do

voidnoob:
i want like this example, speed still on last rpm after turn off, but at my project i want last voltage

https://www.youtube.com/watch?v=r8TwXkTqELQ

What should I do

Which "last voltage" do you want? The last voltage on the Arduino pin or the last voltage on the mosfet driving the motor? In either case, you have already been told the last voltage on the Arduino pin is either 0 volts or 5 volts. Similarly, the last voltage on the mosfet is either 0 or the voltage powering the motor. Ant that voltage relates directly to the Arduino pin being set to 0 or to 1 and your code controls that.

Paul

Just to help OP...
PWM requires the passage of time.
Those 0/1 states change at high frequency to provide a ratio of on-time to off-time.
This not a voltage, but a ‘duty cycle’

Saving the output state at a given moment in time will either give you 0 or 1, or a ratio that was used to generate that duty-cycle. Not a voltage.

You can use an external hardware filter & buffer to convert this repeating 0/1 bit stream at 5/0V into a stable ‘analog voltage.

An analog voltage can also be generated by an external DAC component. (some micros have a simple DAC on chip, but it’s not part of the processor’s logic core.