count that resets to 0 after if else condition is reset

Problem: The code below sets REZ = 0 every iteration and never gets larger. I want it to go until the condition of the “if” statement is no longer met and then reverts “REZ” back to zero for use in the next “elsif” statement.

Reason for doing this, is i want to increase the resolution of my encoder. Currently I can dim the lights based on the value of “string”, but its choppy and you can see the jumps in light intensity. i want it to be smooth.

Currently I’m using setColor1(something i found on the web) to control The PWM.

String: position of angular encoder part # EAWOJ-B24-AEO128L BOURNS Total of 128 positions

PowOn1: and are “int” pre determined earlier in the code and have a range of about 0-32

 if ((( String <= PowOn1 ) || ( String >= PowOff1))) {
    setColor1(RR,GG,BB,WW);// (r,g,b) varry between 0-255, 0 ON 255 OFF 
      int REZ = 0;
      REZ= REZ+1;
      RR = REZ*0.25;
      GG = REX +90;
      BB = 255;
      WW = 255;
 
  }
  else if ((( String <= ExhOn1 ) && ( String >= ExhOff1))) {
    setColor1(0,255,255,255);
   }
  else if ((( String <= IntOn1 ) && ( String >= IntOff1))) {
    setColor1(255,255,0,255);
  }
  else if ((( String <= ComOn1 ) && ( String >= ComOff1))) {
    setColor1(0,255,0,255);
  }
  else if ((( String <= SparkOn1 ) && ( String > SparkOff1))) {
    setColor1(255,255,255,0);
  }

tried a while loop but the duration of each condition changes each time so the while loop does not end before the end of the set number of iterations.

  if ((( String <= PowOn1 ) || ( String >= PowOff1))) {
    setColor1(RR,GG,BB,WW);// (r,g,b) varry between 0-255, 0 ON 255 OFF 
     int REZ = 0; 
     while (REZ<120){    // the duration of the encoder is constantly changing so using REZ< # no
      REZ= REZ+1;
      RR = 0+REZ*0.25;
      GG = 255;
      BB = 255;
      WW = 255;
      Serial.println(RR);
     }
  }
  else if ((( String <= ExhOn1 ) && ( String >= ExhOff1))) {
    setColor1(0,255,255,255);
   }
  else if ((( String <= IntOn1 ) && ( String >= IntOff1))) {
    setColor1(255,255,0,255);
  }
  else if ((( String <= ComOn1 ) && ( String >= ComOff1))) {
    setColor1(0,255,0,255);
  }
  else if ((( String <= SparkOn1 ) && ( String > SparkOff1))) {
    setColor1(255,255,255,0);

I do not see a question here. I can tell you that String is not a good variable name. Also, snippets of code can leave out important information.

Sorry somehow it posted in the middle of typing

Here is the full code reading the encoder is done terribly but can only tackle one problem at a time and it works for now so i only showed the first and last

const int Pin1 = 30; // Encoder Digital Inputs 1-8
const int Pin2 = 32;   
const int Pin3 = 34;
const int Pin4 = 36;
const int Pin5 = 38;
const int Pin6 = 40;
const int Pin7 = 42;
const int Pin8 = 44;


const int Red1 = 8;      // Red LED CYL #1
const int Green1 =9;   // Green LED  CYL #1
const int Blue1 = 10;   // Blue LED CYL #1
const int Spark1 =11;   // Fuel LED CYL #1
const int Fuel1 = 12 ;
//const int Red1 = 6;      // Red LED CYL #2
//const int Green1 = 7;    // Green LED CYL #2
//const int Blue1 = 8;   // Blue LED CYL #2
//const int Spark1 = 9;  // Fuel LED CYL #2
//const int TDCLED = 10;  // LED For Timing Engine at TDC
//                      ######        LED TIMING INFORMATION HERE    #######

//                      ######        CYL #1      ###########
float IntOn1 = 80;   // Intake LED cyl #1  on ( Deg )
float IntOff1 = 47;   // Intake LED cyl #1 off ( Deg )
float ComOn1 = 47;   // Compression LED cyl #1  on ( Deg )
float ComOff1 = 17; // Compression LED cyl #1 off ( Deg )
float SparkOn1 = 17;
float SparkOff1 = 11;
float PowOn1 = 11;
float PowOff1 = 110;
float ExhOn1 = 110;    // Exhaust LED cyl #1 on ( Deg )
float ExhOff1 = 80 ;    // Exhaust LED cyl #1 off ( Deg )
float FuelOn1 = 84 ;   // Fuel LED cyl #1  on ( Deg )
float FuelOff1 = 47;   // Fuel LED cyl #1 off ( Deg )

//                      ######        CYL #2      ###########
//float IntOn2 = 103;   // Intake LED cyl #2  on ( Deg )
//float IntOff2 = 203;   // Intake LED cyl #2 off ( Deg )
//float ComOn2 = 203;   // Compression LED cyl #2  on ( Deg )
//float ComOff2 = 287;   // Compression LED cyl #2 off ( Deg )
//float SparkOn2 = 287;
//float SparkOff2 = 293;
//float PowOn2 = 294;
//float PowOff2 = 11;
//float ExhOn2 = 11;    // Exhaust LED cyl #2 on ( Deg )
//float ExhOff2 = 103;    // Exhaust LED cyl #2 off ( Deg )
//float FuelOn2 = 345 ;   // Fuel LED cyl #2  on ( Deg )
//float FuelOff2 = 262;   // Fuel LED cyl #2 off ( Deg )

//float TDCon = 160;       // Indicate TDC


// Variables
int String = 0;  //Variable used to store position value, between 0 and 127

int State1 = 0;   // Variables representing high or low pin states
int State2 = 0;
int State3 = 0;
int State4 = 0;
int State5 = 0;
int State6 = 0;
int State7 = 0;
int State8 = 0;

void setup() {
  pinMode( Pin1, INPUT);
  pinMode( Pin2, INPUT);
  pinMode( Pin3, INPUT);
  pinMode( Pin4, INPUT);
  pinMode( Pin5, INPUT);
  pinMode( Pin6, INPUT);
  pinMode( Pin7, INPUT);
  pinMode( Pin8, INPUT);

  pinMode ( Red1, OUTPUT );
  pinMode ( Green1, OUTPUT );
  pinMode ( Blue1, OUTPUT );
  pinMode ( Spark1, OUTPUT );
  pinMode ( Fuel1, OUTPUT );

 // pinMode ( Red2, OUTPUT );
 // pinMode ( Green2, OUTPUT );
 // pinMode ( Blue2, OUTPUT );
 // pinMode ( Spark2 , OUTPUT );
 // pinMode ( TDCLED , OUTPUT );
  // Setting Serial Monitor Rate
  Serial.begin(9600);
}

void loop() {



  //  Reading Pin Values
  State1 = digitalRead(Pin1);
  State2 = digitalRead(Pin2);
  State3 = digitalRead(Pin3);
  State4 = digitalRead(Pin4);
  State5 = digitalRead(Pin5);
  State6 = digitalRead(Pin6);
  State7 = digitalRead(Pin7);
  State8 = digitalRead(Pin8);

  ///  Defining Each Position " Only Even Ones "
  // #0
  if ((State1 == HIGH) && (State2 == HIGH) && (State3 == HIGH) && (State4 == HIGH)
      && (State5 == HIGH) && (State6 == HIGH) && (State7 == HIGH) && (State8 == LOW)) {
    String = 0;
  }

  // #1
  else if ((State1 == HIGH) && (State2 == HIGH) && (State3 == HIGH) && (State4 == HIGH)
           && (State5 == HIGH) && (State6 == HIGH) && (State7 == LOW) && (State8 == LOW)) {
    String = 1;
  }

    // #127
  else if ((State1 == HIGH) && (State2 == HIGH) && (State3 == HIGH) && (State4 == HIGH)
           && (State5 == HIGH) && (State6 == LOW) && (State7 == HIGH) && (State8 == LOW)) {
    String = 127;
  }
  Serial.println(String);
  // Printing String Value to Serial Monitor



  //  //////////////LED #1 //////////
  if ((( String <= PowOn1 ) || ( String >= PowOff1))) {
    setColor1(RR,GG,BB,WW);// (r,g,b) varry between 0-255, 0 ON 255 OFF 
     int REZ = 0; 
     while (REZ<120){
      REZ= REZ+1;
      RR = 0+REZ*0.25;
      GG = 255;
      BB = 255;
      WW = 255;
      Serial.println(RR);
     }
  }
  else if ((( String <= ExhOn1 ) && ( String >= ExhOff1))) {
    setColor1(0,255,255,255);
   }
  else if ((( String <= IntOn1 ) && ( String >= IntOff1))) {
    setColor1(255,255,0,255);
  }
  else if ((( String <= ComOn1 ) && ( String >= ComOff1))) {
    setColor1(0,255,0,255);
  }
  else if ((( String <= SparkOn1 ) && ( String > SparkOff1))) {
    setColor1(255,255,255,0);
  }
  //  //////////////FUEL LED #1 //////////

  if (( String <= FuelOn1 ) && ( String >= FuelOff1)) {
    analogWrite( Fuel1, 0);
  }
  else {
    analogWrite( Fuel1, 255 );
  }


void setColor1(int R1, int G1, int Bl1, int W1)
{
#ifdef COMMON_ANODE1

  R1 = R1;
  G1 = G1;
  Bl1 =Bl1;
  W1 = W1 ;
#endif
  analogWrite(Red1, R1);
  analogWrite(Green1, G1);
  analogWrite(Blue1, Bl1);
  analogWrite(Spark1, W1);
}

Hi,

Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Have you got pullup resistors on the input line to the arduino, the same as in the datasheet?
What model arduino are you using and what version IDE?

Thanks.. Tom... :slight_smile:

And a small tip: Gammon Tip 1
And divide it into smaller (understandable) functions.
And pick useful variable names, these are not...

And of course REZ is zero every loop because you create it every loop (and destroy it after each loop)

     int REZ = 0;