iam a student and not an expert in c languange, maybe soon
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;
}
}