If I place this
noInterrupts() ;
if( counter != temp ){
Serial.println (counter);
temp = counter;
}
interrupts() ;
into the code it still behaves as before wildbill.
Like this...
/*
In this example, we're using optical encoder LPD3806-400BM.
Only OUT-B (white wire) is used and connected to pin 2 on Arduino.
This allows a count of up to 400 pulses only.
Connecting both outputs will give a count of up to 800 pulses!
*/
const int ledPin = LED_BUILTIN;
unsigned long startTime;
int lastCounterReading = 0;
int ledState;
unsigned long interval = 50;
int lastTempReading = 0;
volatile long temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder
void setup() {
Serial.begin (250000);
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
pinMode(ledPin, OUTPUT);
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);
}
void loop() {
// Send the value of counter
noInterrupts() ;
if ( counter != temp ) {
Serial.println (counter);
temp = counter;
}
interrupts();
if (counter == 100)
{
if (lastCounterReading != 100)
{
ledState = 1;
startTime = millis();
}
}
lastCounterReading = counter;
if (ledState)
{
if (millis() - startTime <= interval)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
ledState = 0;
}
}
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
if (digitalRead(2) == LOW) {
counter--;
if (counter == 0)
counter = 400;
}
else {
if (counter == 400)
counter = 0;
counter++;
}
}
However, if I do this...
/*
In this example, we're using optical encoder LPD3806-400BM.
Only OUT-B (white wire) is used and connected to pin 2 on Arduino.
This allows a count of up to 400 pulses only.
Connecting both outputs will give a count of up to 800 pulses!
*/
const int ledPin = LED_BUILTIN;
unsigned long startTime;
int lastCounterReading = 0;
int ledState;
unsigned long interval = 50;
int lastTempReading = 0;
volatile long temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder
void setup() {
Serial.begin (250000);
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
pinMode(ledPin, OUTPUT);
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);
}
void loop() {
// Send the value of counter
if( counter != temp ){
Serial.println (counter);
temp = counter;
}
if (temp == 100)
{
if (lastTempReading != 100)
{
ledState = 1;
startTime = millis();
}
}
lastCounterReading = counter;
if (ledState)
{
if (millis() - startTime <= interval)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
ledState = 0;
}
}
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
if(digitalRead(2)==LOW) {
counter--;
if (counter == 0)
counter = 400;
}
else {
if (counter == 400)
counter = 0;
counter++;
}
}
It works. What am I missing?