larryd:
You can add ‘print statements’ throughout your code to prove your variables are what you think they should be.
These print statements can also be used to see if code execution is happening.
I tried that, but my 2 led's remain on.
I would like to switch both leds on, wait a second, switch the first led off, wait another second, and then switch the second led off....and they should remain off until further actions.
Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu. [code]Paste your sketch here[/code] .
And if you want time comparisons to actually work you must use subtraction:
if (millis() < currentTime1 + interval_LED)
needs to be
if (millis() - currentTime1 < interval_LED)
Subtraction makes the code work across wrap-around of time variables, because subtraction always produces the right result even if millis() has just wrapped-back to zero.
I read the tutorials linked in your post, however they refer to blinking leds after a certain interval.
I can't adjust that to my code. I don't want blinks. Normally leds should be off. They should switch on only based on an interval I choose.
I've used your advise to use subtraction, but I still can't make it work.
//My Rotary Speed Encoder poject
unsigned long startMillis;
unsigned long currentMillis1;
unsigned long currentMillis2;
const unsigned long interval_LED = 1000;
const unsigned long interval_Buzzer = 2000;
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
#define inputCLK 6
#define inputDT 7
int counter = 0;
int p = 1;
int currentStateCLK;
int previousStateCLK;
const int buttonPin = 2;
int buttonState;
int lastButtonState = 0;
void setup()
{
pinMode (inputCLK, INPUT);
pinMode (inputDT, INPUT);
pinMode (buttonPin, INPUT);
pinMode (8, OUTPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
Serial.begin (9600);
previousStateCLK = digitalRead(inputCLK);
startMillis = millis();
}
void loop()
{
currentMillis1 = millis();
currentMillis2 = millis();
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH) {
p++;
Serial.print("Sensitivity Level: ");
Serial.println(p);
}
}
lastButtonState = buttonState;
if (p == 1)
{
currentStateCLK = digitalRead(inputCLK);
if (currentStateCLK != previousStateCLK)
{
counter ++;
Serial.println(counter);
if (currentMillis1 - startMillis <= interval_LED)
{
digitalWrite(3, HIGH);
currentMillis1 = millis();
}
else
{
digitalWrite(3, LOW);
}
if (currentMillis2 - startMillis <= interval_Buzzer)
{
digitalWrite(4, HIGH);
currentMillis2 = millis();
}
else
{
digitalWrite(4, LOW);
}
}
previousStateCLK = currentStateCLK;
}
if (p == 2)
{
currentStateCLK = digitalRead(inputCLK);
if (currentStateCLK != previousStateCLK)
{
counter ++;
Serial.println(counter);
}
previousStateCLK = currentStateCLK;
}
if (p == 3)
{
currentStateCLK = digitalRead(inputCLK);
if (currentStateCLK != previousStateCLK)
{
counter ++;
Serial.println(counter);
if ((counter % 5) == 0)
{
}
}
previousStateCLK = currentStateCLK;
}
if (p == 4)
{
currentStateCLK = digitalRead(inputCLK);
if (currentStateCLK != previousStateCLK)
{
counter ++;
if ((counter % 7) == 0)
{
}
Serial.println(counter);
}
previousStateCLK = currentStateCLK;
}
if (p == 5)
{
currentStateCLK = digitalRead(inputCLK);
if (currentStateCLK != previousStateCLK)
{
counter ++;
if ((counter % 9) == 0)
{
}
Serial.println(counter);
}
}
previousStateCLK = currentStateCLK;
if (p == 6)
{
p = 0;
}
}
ionass:
it's not accurate. Every time I turn the knob, the encoder value is incremented by 2 instead of 1.
I'm sure it's totally accurate. The code looks for a change in pinA, without regard for it being a rising or falling change. Then it compares B to the new A, to get the direction; same = one way, different = the other.
So it's seeing 2 changes in A (up and down) when you seem to want only every second one, into the detent.
That code could be easily modified to ask if there's a change on A, AND A is now low (or high, whichever is the right one), then it's going to ignore half of the counts.
Robin2:
If it always increments by 2 then just subtract 1
That and your edited-away suggestion (which I think was to divide by 2) are both a bit kludgey though.
Subtracting 1 probably wouldn't work anyway. On it's way to 2, it goes through 1, which would then be 0 after subtracting 1, and then the second sense would be to 1 which would get subtracted back to 0. (I think, would need to test....)
I'd go for only looking for A to change in one direction up or down but not both, thus only seeing every second count and being inherently correct not fudged.