problem with my sketch....

Noop here....

I am trying to write a sketch that can fade 2 LEDs when you press the button.
the fade rate can be changed by using scale & EEPROM.

I had 2 problems:

1- the LED connected to pin 10 doesn't seem to respond to the skech at all.

2- while the other LED seems to work fine at first, after pressing the button for 3 or 4 times, it doesn't fade down to 0 brightness, instead it just dim from max to 0 at instance

please help...

here is the sketch.

#include <EEPROM.h>

/*
برنامج بيخلى لمبتين يعملوا fade
بشرط الضغط على الزر
مع امكانيه تعديل معدل التردد و الخفاظ عليه عند فصل الكهرباء
*/

int light[2] = {10,5};
int button =2;

int fadeamout[2] = {5.5};
int brightness[2]={0,0};

int scale =1;

void setup() {
pinMode (light[0],OUTPUT);
pinMode (light[1],OUTPUT);
pinMode (button,INPUT);

Serial.begin(9600);
Serial.setTimeout(10);

int savedscale =EEPROM.read(0);
if (savedscale<20)
scale = savedscale;

}

void loop() {

int newscale =Serial.parseInt();
if (newscale > 0)
scale = newscale;
EEPROM.write(0,scale);

int buttonsatate = digitalRead(button);
for (int counter =0;counter<2;counter++){

if (buttonsatate == HIGH){

brightness[counter] = brightness[counter]+fadeamout[counter]*scale;
if(brightness[counter]==0 || brightness[counter]==255){
fadeamout[counter] = -fadeamout[counter];}

}
else analogWrite(light[counter], 0);
analogWrite(light[counter], brightness[counter]);
}

delay(30);
}

void loop() {
 
 int newscale =Serial.parseInt();

Every time through loop(), block waiting for serial data. Why? Calling parseInt() makes sense ONLY if there is data waiting to be read.

EEPROM.write(0,scale);

Writing to EEPROM on every pass through loop() is a good way to wear out a non-replaceable resource.

How IS your switch wired? How ARE the LEDs wired? Have you tried simply making pin 10 turn on, to verify that the LED is wired correctly?

thank you for your reply

I tried pin 10 with another sketch and it is working ....

Save some wear on your EEPROM. After check for new serial data to read (Serial.available()>0 ) and then reading it and working out what newscale is, then

if (newscale >0 ){
  if ( newscale == EEPROM.read(0) ){
  // do nothing
  }
  else { // write new data
  EEPROM.write(0,scale);
  }
}
int fadeamout[2] = {5.5};

Was that decimal supposed to be a comma?

I didn't know how to solve the EEPROM issue ..... thank you CrossRoads

Delta_G:

int fadeamout[2] = {5.5};

Was that decimal supposed to be a comma?

That fixed the pin 10 led problem, thanks man.\

anyone can help with problem No. 2 ?

Post code.

here is the sketch.

sketch_may29a.ino (1.27 KB)

Why is it so hard to respond to a simple request?
Post your code.

#include <EEPROM.h>


int light[2] = {10,5};
int button =2;

int fadeamout[2] = {5,5};
int brightness[2]={0,0};

int scale =1;


void setup() {
  pinMode (light[0],OUTPUT);
  pinMode (light[1],OUTPUT);
  pinMode (button,INPUT);
  
  Serial.begin(9600);
  Serial.setTimeout(10);
  
  int savedscale =EEPROM.read(0);
  if (savedscale<20)
  scale = savedscale;

}

void loop() {
 
 int newscale =Serial.parseInt();
if (newscale > 0)
scale = newscale;
EEPROM.write(0,scale);

int buttonsatate = digitalRead(button);
for (int counter =0;counter<2;counter++){
  
  if (buttonsatate == HIGH){
 analogWrite(light[counter], brightness[counter]);
    brightness[counter] = brightness[counter]+fadeamout[counter]*scale;
    if(brightness[counter]==0 || brightness[counter]==255){
      fadeamout[counter] = -fadeamout[counter];}
    
  
  }
    else analogWrite(light[counter], 0);
   
}

delay(30);
}

like this ?

I didn't know how to solve the EEPROM issue

Keep track of the last value you wrote (it's the same as the value you read in setup(), initially). Only write a new value if the value is not the same as the last value written.

void loop() {
 
 int newscale =Serial.parseInt();
if (newscale > 0)
scale = newscale;
EEPROM.write(0,scale);

No.
You're still parsing without checking if any data arrived.
Without { } after the "if (newscale >0)" to control the next couple of lines, they will always run.