Restar for each pulse

i am on a project where i am using increment encoder for angle detection and switching on and off of my components with arduino as per encoder value .my conditions are

1.program should start accepting the encoder input after the signal from D4 to which i have connected IR sensor output when program is started (this is working properly)
2.the pin D13,12 must go high and low as per the values of encoder (this is also working properly)
3.the pin d4 will become high one time for each revolution of my driven component .so from second time of high of D4 pin my encoder value should become zero and star from ZERO (this is to be added)

that is intially the encoder input should be accepted by the program after getting a high pulse from D4 .untill it will not accept encoder input .And for the following pulses from D4 the value of encoder must be reseted to zero .

my code:

int encoderPin1 = 2;
int encoderPin2 = 3;
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
int led1 = 13;
int led2=12;
int ir_on = 4;
int on;
int a;
void setup() {
Serial.begin (9600);
pinMode(ir_on,INPUT);
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
//call updateEncoder() when any high/low changed seen
//on interrupt 0 (pin 2), or interrupt 1 (pin 3)

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop(){
//Do stuff here
on=digitalRead(ir_on);
if (on == LOW)
{
a=1;
encoderValue == 0;

  }
  if(a==1)
  {
    attachInterrupt(0, updateEncoder, CHANGE);

attachInterrupt(1, updateEncoder, CHANGE);

    }

Serial.println(encoderValue);
//delay(1000); //just here to slow down the output, and show it will work even during a delay
}
void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011)
encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000)
encoderValue --;
lastEncoded = encoded;

//store this value for next time

if((encoderValue >= 1 )&&(encoderValue<=685))
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}

if((encoderValue > 685)&&(encoderValue<= 2052))
{
  digitalWrite(led1, LOW);
   digitalWrite(led2, LOW);
}
     
if((encoderValue > 2052 )&&(encoderValue<= 4104))
{
  digitalWrite(led2, HIGH);
 
}

if(encoderValue > 4104 )
{

 digitalWrite(led2, LOW);

}

}

Oops

Please remember to use code tags when posting code

Hi, @chitrarasan
Welcome to the forum.

To add code please click this link;

What model Arduino are you using?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hey Guys,

a short word like "post in code-tags" does not explain how it works to post in code-tags

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

I'm think about expanding the original text with an explanation how to donwload and install autohotkey including the script for a hotstring that lets autohotkey type the epxlanation

If you want to find a post from TMFKAA just search for "Oops" ;-))

best regards Stefan

"post in code-tags" is a fairly long phrase; it is most certainly NOT a "short word".
(Usually, I remind people to "please remember to use code tags when posting code", which is an even longer phrase)

I'm happy to post longer responses (and maybe appear condescending), but I'd MUCH rather give people the option to pause and think.

I find most people are happier to arrive at their own solution, via a short hint, than to have a solution spoon-fed to them.

At least, that's how my endorphins appear to work.

I basically agree on that. Though to know how to post code efficiently has nothing to do with programming.

But programming is all about knowing rules, and how to read, and find-out about them.
And how to realise when people tell you to read and find-out about them.
Anything else is spoon-feeding.

(Is anyone picking-up on the pro-thought, anti-spoon-feeding vibe?)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.