Switching between Modes with a single Button

Hey, Im totally at a loss right now.
This code works okay, But theres a few things wrong with it..
1.I want to change between 3 states/ 3 modes of accuracy. So i set up a counter and set it up to switch accordingly. It doesnt for some reason.

  1. I included a last state function so the serial output doesnt repeat. Only the first one works correctly ( pulse cycle micro seconds) The other ones dont even show.
       //const byte SpeedKnob_AIPin = A3;
const byte GreenLED_DOPin = 5;
const byte  RedLED_DOPin = 6;
const byte UpButton_DIPin = 7;
const byte DownButton_DIPin = 8;
const byte changeAccuracy = 9;


const unsigned long PulseMicroseconds = 50; //the duration of the time the led stayes on


const unsigned long  MinMicrosecondsPerCycle = 1UL;  // 1000000 Pulse Per Second
const unsigned long  MaxMicrosecondsPerCycle = 50000UL; // 20 Pulse Per Second

const unsigned change_LOW= 1;
const unsigned change_MID= 10;  // Different levels of accuracy.
const unsigned change_HIGH = 100;

unsigned ChangePerButtonPress;
const unsigned ButtonUpdatesPerSecond = 20;
const unsigned ButtonUpdateIntervalMillis = 1000 / ButtonUpdatesPerSecond;


unsigned long PulseCycleMicroseconds = MaxMicrosecondsPerCycle;
unsigned long TimeOfCycleStart = 0;
unsigned Counter = 1;


unsigned long TimeOfLastSpeedUpdate = 0;
unsigned long lastPulse;
unsigned long lastAccuracy;
unsigned long RPM = 0;
unsigned long lastRPM;

void setup()
{
  pinMode(GreenLED_DOPin, OUTPUT);
  pinMode(RedLED_DOPin, OUTPUT);//as simple as that. or is it?
  pinMode(UpButton_DIPin, INPUT_PULLUP);
  pinMode(DownButton_DIPin, INPUT_PULLUP);
  pinMode(changeAccuracy, INPUT_PULLUP);
  
Serial.begin(2000000);
}



void loop()
{
  // GHet the current times at the start of loop()
  unsigned long currenmMillis = millis();
  unsigned long currentMicros = micros();


  static boolean greenIsOn = false;
  static boolean redPulseStarted = false;


  // Is it time to check the buttons?
  if (currenmMillis - TimeOfLastSpeedUpdate >= ButtonUpdateIntervalMillis)
  {
    TimeOfLastSpeedUpdate = currenmMillis;


    boolean upPressed = digitalRead(UpButton_DIPin) == LOW;
    boolean downPressed = digitalRead(DownButton_DIPin) == LOW;


         if(Counter == 1){ChangePerButtonPress = change_HIGH;}
         if(Counter == 2){ChangePerButtonPress = change_MID;}//switch according to counter..
         if(Counter == 3){ChangePerButtonPress = change_LOW;}
    if(changeAccuracy == LOW)
    {
      Counter + 1;
      if(Counter == 4)
      {
        Counter = 1;//so the counter doesnt go overboard.
      }
    }
         
         
    
    if (upPressed)
    {
      PulseCycleMicroseconds += ChangePerButtonPress;
    }
    if (downPressed)
    {
      PulseCycleMicroseconds -= ChangePerButtonPress;
    }


    // Keep the pulse length within limits
    PulseCycleMicroseconds = constrain(PulseCycleMicroseconds, MinMicrosecondsPerCycle, MaxMicrosecondsPerCycle);
  }


  unsigned long cycleElapsed = currentMicros - TimeOfCycleStart;


  // Now check to see if the cycle is over
  if (cycleElapsed >= PulseCycleMicroseconds)
  {
    // Old cycle is over.  Starting a new one.
    TimeOfCycleStart = currentMicros;
    // Starting the Green pulse
    digitalWrite(GreenLED_DOPin, HIGH);
    greenIsOn = true;
    redPulseStarted = false;
  }


  // Is the Green pulse over?
  if (greenIsOn && cycleElapsed >= PulseMicroseconds)
  {
    // Green pulse is over.
    digitalWrite(GreenLED_DOPin, LOW);
    greenIsOn = false;
  }


  // Is it time to start the Red pulse?
  if (!redPulseStarted && cycleElapsed >= PulseCycleMicroseconds / 2)
  {
    // Start the Red pulse.
    digitalWrite(RedLED_DOPin, HIGH);
    redPulseStarted = true;
  }
 
  // Is the Red pulse over?
  if (redPulseStarted && cycleElapsed >= (PulseCycleMicroseconds / 2) + PulseMicroseconds)
  {
    // Red pulse is over.
    digitalWrite(RedLED_DOPin, LOW);
  }
 if(lastPulse != PulseCycleMicroseconds)
 { 
  Serial.print("Pulse Time : ");
  Serial.println(PulseCycleMicroseconds);
 }
 if(lastAccuracy != ChangePerButtonPress)
 {
  Serial.println("Change per button press : ");
  Serial.print(ChangePerButtonPress);
 }
 
 RPM = (1/PulseCycleMicroseconds)*60;
 if(RPM != lastRPM)
 { 
 Serial.println("RPM : ");
 Serial.print(RPM);
 }
 lastPulse = PulseCycleMicroseconds;
 lastAccuracy = ChangePerButtonPress;
 lastRPM = RPM;
}

I see 2 issues with this conditional:

    if (changeAccuracy == LOW)
    {
      Counter + 1;
      if (Counter == 4)
      {
        Counter = 1;//so the counter doesnt go overboard.
      }
    }

I believe it should be:

    if (digitalRead(changeAccuracy) == LOW)
    {
      Counter = Counter + 1;
      if (Counter == 4)
      {
        Counter = 1;//so the counter doesnt go overboard.
      }
    }

there were some other issue

  • you compared changeAccuracy to LOW while also using it as a pin number in pinMode()
  • i had a hard time with your code to change the LEDs. I felt you needed to reset TimeOfCycleStart in each case like you do for the first. But I thought a switch statement made it easier
#if 0
//const byte SpeedKnob_AIPin = A3;
const byte GreenLED_DOPin = 5;
const byte  RedLED_DOPin = 6;
const byte UpButton_DIPin = 7;
const byte DownButton_DIPin = 8;
const byte changeAccuracy = 9;

#else
const byte GreenLED_DOPin   = 10;
const byte RedLED_DOPin     = 11;

const byte UpButton_DIPin   = A1;
const byte DownButton_DIPin = A2;
const byte changeAccuracy   = A3;
#endif

const unsigned long PulseMicroseconds = 50;
//the duration of the time the led stayes on
const unsigned long  MinMicrosecondsPerCycle = 1UL;
// 1000000 Pulse Per Second
const unsigned long  MaxMicrosecondsPerCycle = 50000UL;
// 20 Pulse Per Second
const unsigned change_LOW= 1;
const unsigned change_MID= 10;
// Different levels of accuracy.
const unsigned change_HIGH = 100;
unsigned ChangePerButtonPress;
const unsigned ButtonUpdatesPerSecond = 20;
const unsigned ButtonUpdateIntervalMillis = 1000 / ButtonUpdatesPerSecond;
unsigned long PulseCycleMicroseconds = MaxMicrosecondsPerCycle;
unsigned long TimeOfCycleStart = 0;
unsigned Counter = 1;
unsigned long TimeOfLastSpeedUpdate = 0;
unsigned long lastPulse;
unsigned long lastAccuracy;
unsigned long RPM = 0;
unsigned long lastRPM;

void setup()
{
    pinMode(GreenLED_DOPin, OUTPUT);
    pinMode(RedLED_DOPin, OUTPUT);
    //as simple as that. or is it?
    pinMode(UpButton_DIPin, INPUT_PULLUP);
    pinMode(DownButton_DIPin, INPUT_PULLUP);
    pinMode(changeAccuracy, INPUT_PULLUP);

    Serial.begin(9600);
}

void loop()
{
    // GHet the current times at the start of loop()
    unsigned long currenmMillis = millis();
    unsigned long currentMicros = micros();
    static boolean greenIsOn = false;
    static boolean redPulseStarted = false;
    // Is it time to check the buttons?
    if (currenmMillis - TimeOfLastSpeedUpdate >= ButtonUpdateIntervalMillis)
    {
        TimeOfLastSpeedUpdate = currenmMillis;

        boolean upPressed   = digitalRead(UpButton_DIPin) == LOW;
        boolean downPressed = digitalRead(DownButton_DIPin) == LOW;

        if(Counter == 1){
            ChangePerButtonPress = change_HIGH;
        }

        if(Counter == 2){
            ChangePerButtonPress = change_MID;
        }//switch according to counter..

        if(Counter == 3){
            ChangePerButtonPress = change_LOW;
        }

#if 0
        if(changeAccuracy == LOW)
#else
        if(digitalRead(changeAccuracy) == LOW)
#endif
        {
#if 0
            Counter + 1;
#else
            Counter += 1;
#endif
            if(Counter == 4)
            {
                Counter = 1;
                //so the counter doesnt go overboard.
            }
        }

        if (upPressed) {
            PulseCycleMicroseconds += ChangePerButtonPress;
        }

        if (downPressed) {
            PulseCycleMicroseconds -= ChangePerButtonPress;
        }

        // Keep the pulse length within limits
        PulseCycleMicroseconds = constrain(PulseCycleMicroseconds, MinMicrosecondsPerCycle, MaxMicrosecondsPerCycle);
    }

    // ---------------------------------------------------------
    unsigned long cycleElapsed = currentMicros - TimeOfCycleStart;
#if 0
    // Now check to see if the cycle is over
    if (cycleElapsed >= PulseCycleMicroseconds)
    {
        // Old cycle is over.  Starting a new one.
        TimeOfCycleStart = currentMicros;
        // Starting the Green pulse
        digitalWrite(GreenLED_DOPin, HIGH);
        greenIsOn = true;
        redPulseStarted = false;
        Serial.println ("green on");
    }

    // Is the Green pulse over?
    if (greenIsOn && cycleElapsed >= PulseMicroseconds)
    {
        // Green pulse is over.
        digitalWrite(GreenLED_DOPin, LOW);
        greenIsOn = false;
        Serial.println ("green off");
    }

    // Is it time to start the Red pulse?
    if (!redPulseStarted && cycleElapsed >= PulseCycleMicroseconds / 2)
    {
        // Start the Red pulse.
        digitalWrite(RedLED_DOPin, HIGH);
        redPulseStarted = true;
        Serial.println ("red on");
    }

    // Is the Red pulse over?
    if (redPulseStarted && cycleElapsed >= (PulseCycleMicroseconds / 2) + PulseMicroseconds)
    {
        // Red pulse is over.
        digitalWrite(RedLED_DOPin, LOW);
        Serial.println ("red off");
    }
#else
    static int state = 0;

    if (cycleElapsed >= PulseCycleMicroseconds)  {
        TimeOfCycleStart = currentMicros;
        switch (state++)  {
        case 0:
            digitalWrite (GreenLED_DOPin, LOW);
            digitalWrite (RedLED_DOPin,   HIGH);
            state++;
            break;

        case 1:
            digitalWrite (GreenLED_DOPin, HIGH);
            digitalWrite (RedLED_DOPin,   HIGH);
            state++;
            break;

        case 2:
            digitalWrite (GreenLED_DOPin, HIGH);
            digitalWrite (RedLED_DOPin,   LOW);
            state++;
            break;

        default:
            state = 0;
            digitalWrite (GreenLED_DOPin, HIGH);
            digitalWrite (RedLED_DOPin,   HIGH);
            break;
        }
    }
#endif

    // ---------------------------------------------------------
    if(lastPulse != PulseCycleMicroseconds)
    {
        Serial.print("Pulse Time : ");
        Serial.println(PulseCycleMicroseconds);
    }

    if(lastAccuracy != ChangePerButtonPress)
    {
        Serial.println("Change per button press : ");
        Serial.print(ChangePerButtonPress);
    }

    RPM = (1/PulseCycleMicroseconds)*60;
    if(RPM != lastRPM)
    {
        Serial.println("RPM : ");
        Serial.print(RPM);
    }

    lastPulse = PulseCycleMicroseconds;
    lastAccuracy = ChangePerButtonPress;
    lastRPM = RPM;
}

gcjr:
there were some other issue

  • you compared changeAccuracy to LOW while also using it as a pin number in pinMode()
  • i had a hard time with your code to change the LEDs. I felt you needed to reset TimeOfCycleStart in each case like you do for the first. But I thought a switch statement made it easier
#if 0

//const byte SpeedKnob_AIPin = A3;
const byte GreenLED_DOPin = 5;
const byte  RedLED_DOPin = 6;
const byte UpButton_DIPin = 7;
const byte DownButton_DIPin = 8;
const byte changeAccuracy = 9;

#else
const byte GreenLED_DOPin  = 10;
const byte RedLED_DOPin    = 11;

const byte UpButton_DIPin  = A1;
const byte DownButton_DIPin = A2;
const byte changeAccuracy  = A3;
#endif

const unsigned long PulseMicroseconds = 50;
//the duration of the time the led stayes on
const unsigned long  MinMicrosecondsPerCycle = 1UL;
// 1000000 Pulse Per Second
const unsigned long  MaxMicrosecondsPerCycle = 50000UL;
// 20 Pulse Per Second
const unsigned change_LOW= 1;
const unsigned change_MID= 10;
// Different levels of accuracy.
const unsigned change_HIGH = 100;
unsigned ChangePerButtonPress;
const unsigned ButtonUpdatesPerSecond = 20;
const unsigned ButtonUpdateIntervalMillis = 1000 / ButtonUpdatesPerSecond;
unsigned long PulseCycleMicroseconds = MaxMicrosecondsPerCycle;
unsigned long TimeOfCycleStart = 0;
unsigned Counter = 1;
unsigned long TimeOfLastSpeedUpdate = 0;
unsigned long lastPulse;
unsigned long lastAccuracy;
unsigned long RPM = 0;
unsigned long lastRPM;

void setup()
{
    pinMode(GreenLED_DOPin, OUTPUT);
    pinMode(RedLED_DOPin, OUTPUT);
    //as simple as that. or is it?
    pinMode(UpButton_DIPin, INPUT_PULLUP);
    pinMode(DownButton_DIPin, INPUT_PULLUP);
    pinMode(changeAccuracy, INPUT_PULLUP);

Serial.begin(9600);
}

void loop()
{
    // GHet the current times at the start of loop()
    unsigned long currenmMillis = millis();
    unsigned long currentMicros = micros();
    static boolean greenIsOn = false;
    static boolean redPulseStarted = false;
    // Is it time to check the buttons?
    if (currenmMillis - TimeOfLastSpeedUpdate >= ButtonUpdateIntervalMillis)
    {
        TimeOfLastSpeedUpdate = currenmMillis;

boolean upPressed  = digitalRead(UpButton_DIPin) == LOW;
        boolean downPressed = digitalRead(DownButton_DIPin) == LOW;

if(Counter == 1){
            ChangePerButtonPress = change_HIGH;
        }

if(Counter == 2){
            ChangePerButtonPress = change_MID;
        }//switch according to counter..

if(Counter == 3){
            ChangePerButtonPress = change_LOW;
        }

#if 0
        if(changeAccuracy == LOW)
#else
        if(digitalRead(changeAccuracy) == LOW)
#endif
        {
#if 0
            Counter + 1;
#else
            Counter += 1;
#endif
            if(Counter == 4)
            {
                Counter = 1;
                //so the counter doesnt go overboard.
            }
        }

if (upPressed) {
            PulseCycleMicroseconds += ChangePerButtonPress;
        }

if (downPressed) {
            PulseCycleMicroseconds -= ChangePerButtonPress;
        }

// Keep the pulse length within limits
        PulseCycleMicroseconds = constrain(PulseCycleMicroseconds, MinMicrosecondsPerCycle, MaxMicrosecondsPerCycle);
    }

// ---------------------------------------------------------
    unsigned long cycleElapsed = currentMicros - TimeOfCycleStart;
#if 0
    // Now check to see if the cycle is over
    if (cycleElapsed >= PulseCycleMicroseconds)
    {
        // Old cycle is over.  Starting a new one.
        TimeOfCycleStart = currentMicros;
        // Starting the Green pulse
        digitalWrite(GreenLED_DOPin, HIGH);
        greenIsOn = true;
        redPulseStarted = false;
        Serial.println ("green on");
    }

// Is the Green pulse over?
    if (greenIsOn && cycleElapsed >= PulseMicroseconds)
    {
        // Green pulse is over.
        digitalWrite(GreenLED_DOPin, LOW);
        greenIsOn = false;
        Serial.println ("green off");
    }

// Is it time to start the Red pulse?
    if (!redPulseStarted && cycleElapsed >= PulseCycleMicroseconds / 2)
    {
        // Start the Red pulse.
        digitalWrite(RedLED_DOPin, HIGH);
        redPulseStarted = true;
        Serial.println ("red on");
    }

// Is the Red pulse over?
    if (redPulseStarted && cycleElapsed >= (PulseCycleMicroseconds / 2) + PulseMicroseconds)
    {
        // Red pulse is over.
        digitalWrite(RedLED_DOPin, LOW);
        Serial.println ("red off");
    }
#else
    static int state = 0;

if (cycleElapsed >= PulseCycleMicroseconds)  {
        TimeOfCycleStart = currentMicros;
        switch (state++)  {
        case 0:
            digitalWrite (GreenLED_DOPin, LOW);
            digitalWrite (RedLED_DOPin,  HIGH);
            state++;
            break;

case 1:
            digitalWrite (GreenLED_DOPin, HIGH);
            digitalWrite (RedLED_DOPin,  HIGH);
            state++;
            break;

case 2:
            digitalWrite (GreenLED_DOPin, HIGH);
            digitalWrite (RedLED_DOPin,  LOW);
            state++;
            break;

default:
            state = 0;
            digitalWrite (GreenLED_DOPin, HIGH);
            digitalWrite (RedLED_DOPin,  HIGH);
            break;
        }
    }
#endif

// ---------------------------------------------------------
    if(lastPulse != PulseCycleMicroseconds)
    {
        Serial.print("Pulse Time : ");
        Serial.println(PulseCycleMicroseconds);
    }

if(lastAccuracy != ChangePerButtonPress)
    {
        Serial.println("Change per button press : ");
        Serial.print(ChangePerButtonPress);
    }

RPM = (1/PulseCycleMicroseconds)*60;
    if(RPM != lastRPM)
    {
        Serial.println("RPM : ");
        Serial.print(RPM);
    }

lastPulse = PulseCycleMicroseconds;
    lastAccuracy = ChangePerButtonPress;
    lastRPM = RPM;
}

Ill try out your code today, but the thing is, you need a very bright led with this code, and even with that the light intensity doesnt 'seem' to change very much unless your at the very edge of the limit( hence the serial print) . I believe its because of the weber-fechner law, which states that human sensory perception is logarithmic, not linear.

(1/PulseCycleMicroseconds)*60aka zero

TheMemberFormerlyKnownAsAWOL:
(1/PulseCycleMicroseconds)*60aka zero

I just realized... i need to convert micros to seconds.

Anyways, after the suggested change above, Theres still some problems.

When i press the change accuracy button, nothing seems to happen, But if i press the button WHILE pushing down some other button, the accuracy switches between erratically and it basically wont work consistently.

gcjr:
there were some other issue

  • you compared changeAccuracy to LOW while also using it as a pin number in pinMode()
  • i had a hard time with your code to change the LEDs. I felt you needed to reset TimeOfCycleStart in each case like you do for the first. But I thought a switch statement made it easier
#if 0

//const byte SpeedKnob_AIPin = A3;
const byte GreenLED_DOPin = 5;
const byte  RedLED_DOPin = 6;
const byte UpButton_DIPin = 7;
const byte DownButton_DIPin = 8;
const byte changeAccuracy = 9;

#else
const byte GreenLED_DOPin  = 10;
const byte RedLED_DOPin    = 11;

const byte UpButton_DIPin  = A1;
const byte DownButton_DIPin = A2;
const byte changeAccuracy  = A3;
#endif

const unsigned long PulseMicroseconds = 50;
//the duration of the time the led stayes on
const unsigned long  MinMicrosecondsPerCycle = 1UL;
// 1000000 Pulse Per Second
const unsigned long  MaxMicrosecondsPerCycle = 50000UL;
// 20 Pulse Per Second
const unsigned change_LOW= 1;
const unsigned change_MID= 10;
// Different levels of accuracy.
const unsigned change_HIGH = 100;
unsigned ChangePerButtonPress;
const unsigned ButtonUpdatesPerSecond = 20;
const unsigned ButtonUpdateIntervalMillis = 1000 / ButtonUpdatesPerSecond;
unsigned long PulseCycleMicroseconds = MaxMicrosecondsPerCycle;
unsigned long TimeOfCycleStart = 0;
unsigned Counter = 1;
unsigned long TimeOfLastSpeedUpdate = 0;
unsigned long lastPulse;
unsigned long lastAccuracy;
unsigned long RPM = 0;
unsigned long lastRPM;

void setup()
{
    pinMode(GreenLED_DOPin, OUTPUT);
    pinMode(RedLED_DOPin, OUTPUT);
    //as simple as that. or is it?
    pinMode(UpButton_DIPin, INPUT_PULLUP);
    pinMode(DownButton_DIPin, INPUT_PULLUP);
    pinMode(changeAccuracy, INPUT_PULLUP);

Serial.begin(9600);
}

void loop()
{
    // GHet the current times at the start of loop()
    unsigned long currenmMillis = millis();
    unsigned long currentMicros = micros();
    static boolean greenIsOn = false;
    static boolean redPulseStarted = false;
    // Is it time to check the buttons?
    if (currenmMillis - TimeOfLastSpeedUpdate >= ButtonUpdateIntervalMillis)
    {
        TimeOfLastSpeedUpdate = currenmMillis;

boolean upPressed  = digitalRead(UpButton_DIPin) == LOW;
        boolean downPressed = digitalRead(DownButton_DIPin) == LOW;

if(Counter == 1){
            ChangePerButtonPress = change_HIGH;
        }

if(Counter == 2){
            ChangePerButtonPress = change_MID;
        }//switch according to counter..

if(Counter == 3){
            ChangePerButtonPress = change_LOW;
        }

#if 0
        if(changeAccuracy == LOW)
#else
        if(digitalRead(changeAccuracy) == LOW)
#endif
        {
#if 0
            Counter + 1;
#else
            Counter += 1;
#endif
            if(Counter == 4)
            {
                Counter = 1;
                //so the counter doesnt go overboard.
            }
        }

if (upPressed) {
            PulseCycleMicroseconds += ChangePerButtonPress;
        }

if (downPressed) {
            PulseCycleMicroseconds -= ChangePerButtonPress;
        }

// Keep the pulse length within limits
        PulseCycleMicroseconds = constrain(PulseCycleMicroseconds, MinMicrosecondsPerCycle, MaxMicrosecondsPerCycle);
    }

// ---------------------------------------------------------
    unsigned long cycleElapsed = currentMicros - TimeOfCycleStart;
#if 0
    // Now check to see if the cycle is over
    if (cycleElapsed >= PulseCycleMicroseconds)
    {
        // Old cycle is over.  Starting a new one.
        TimeOfCycleStart = currentMicros;
        // Starting the Green pulse
        digitalWrite(GreenLED_DOPin, HIGH);
        greenIsOn = true;
        redPulseStarted = false;
        Serial.println ("green on");
    }

// Is the Green pulse over?
    if (greenIsOn && cycleElapsed >= PulseMicroseconds)
    {
        // Green pulse is over.
        digitalWrite(GreenLED_DOPin, LOW);
        greenIsOn = false;
        Serial.println ("green off");
    }

// Is it time to start the Red pulse?
    if (!redPulseStarted && cycleElapsed >= PulseCycleMicroseconds / 2)
    {
        // Start the Red pulse.
        digitalWrite(RedLED_DOPin, HIGH);
        redPulseStarted = true;
        Serial.println ("red on");
    }

// Is the Red pulse over?
    if (redPulseStarted && cycleElapsed >= (PulseCycleMicroseconds / 2) + PulseMicroseconds)
    {
        // Red pulse is over.
        digitalWrite(RedLED_DOPin, LOW);
        Serial.println ("red off");
    }
#else
    static int state = 0;

if (cycleElapsed >= PulseCycleMicroseconds)  {
        TimeOfCycleStart = currentMicros;
        switch (state++)  {
        case 0:
            digitalWrite (GreenLED_DOPin, LOW);
            digitalWrite (RedLED_DOPin,  HIGH);
            state++;
            break;

case 1:
            digitalWrite (GreenLED_DOPin, HIGH);
            digitalWrite (RedLED_DOPin,  HIGH);
            state++;
            break;

case 2:
            digitalWrite (GreenLED_DOPin, HIGH);
            digitalWrite (RedLED_DOPin,  LOW);
            state++;
            break;

default:
            state = 0;
            digitalWrite (GreenLED_DOPin, HIGH);
            digitalWrite (RedLED_DOPin,  HIGH);
            break;
        }
    }
#endif

// ---------------------------------------------------------
    if(lastPulse != PulseCycleMicroseconds)
    {
        Serial.print("Pulse Time : ");
        Serial.println(PulseCycleMicroseconds);
    }

if(lastAccuracy != ChangePerButtonPress)
    {
        Serial.println("Change per button press : ");
        Serial.print(ChangePerButtonPress);
    }

RPM = (1/PulseCycleMicroseconds)*60;
    if(RPM != lastRPM)
    {
        Serial.println("RPM : ");
        Serial.print(RPM);
    }

lastPulse = PulseCycleMicroseconds;
    lastAccuracy = ChangePerButtonPress;
    lastRPM = RPM;
}

The code doesnt show any change with any button presses in the serial monitor. I thought it couldve been a speed issue, so i tried different values, but it still didnt work..

kaseftamjid:
I just realized... i need to convert micros to seconds.

No. You need to understand that division in C with integer variables yields an integer result.
So 1/pulseCycleMicroseconds almost always gives 0, regardless of what value pulseCycleMicroseconds has, except for 1 and -1 (in which case the result is 1 and -1), and when the divisor is 0 (in which case the result may be undefined).

Try 60/pulseCycleMicroseconds which might stand a chance of giving you something other than zero. Although I still expect not the value you are looking for. But as you are assigning to an integer result (RPM) there's not much point in floating point arithmetic here.

pcbbc:
No. You need to understand that division in C with integer variables yields an integer result.
So 1/pulseCycleMicroseconds almost always gives 0, regardless of what value pulseCycleMicroseconds has, except for 1 and -1 (in which case the result is 1 and -1), and when the divisor is 0 (in which case the result may be undefined).

Try 60/pulseCycleMicroseconds which might stand a chance of giving you something other than zero. Although I still expect not the value you are looking for. But as you are assigning to an integer result (RPM) there's not much point in floating point arithmetic here.

Im not sure how to work around this. i prefer integer values.. 60/pCMS wont get much useful values

kaseftamjid:
The code doesnt show any change with any button presses in the serial monitor. I thought it couldve been a speed issue, so i tried different values, but it still didnt work..

did you change the #if 0 to #if 1 to use your pin values?

kaseftamjid:
I'm not sure how to work around this. I prefer integer values.. 60/pCMS wont get much useful values

It's will give you a darn sight more useful results than your original calculation!

Yes, sticking with integer maths is usually better on an 8-bit CPU.

If you want fractional RPM, either...
a) Use floating point types
b) Or stick to integers and use an implied decimal point. e.g. 2 decimals...
6000/pCMS
Now just display your result with a decimal point between the hundreds and tens digits.

Technically, 0 isnt a number, so you are right!!

Anyways, I figured a way to get rpm values aand get the button working, Butttt

The arduino returns wrong division values.

const byte GreenLED_DOPin = 5;
const byte  RedLED_DOPin = 6;
const byte UpButton_DIPin = 7;
const byte DownButton_DIPin = 8;
const byte changeAccuracy = 9;


const unsigned long PulseMicroseconds = 50; //the duration of the time the led stayes on


const unsigned long  MinMicrosecondsPerCycle = 1UL;  // 1000 Pulse Per Second
const unsigned long  MaxMicrosecondsPerCycle = 50000UL; // 20 Pulse Per Second

const unsigned change_LOW= 1;
const unsigned change_MID= 10;  // Different levels of accuracy.
const unsigned change_HIGH = 100;

unsigned ChangePerButtonPress = change_HIGH;
const unsigned ButtonUpdatesPerSecond = 20;
const unsigned ButtonUpdateIntervalMillis = 1000 / ButtonUpdatesPerSecond;


unsigned long PulseCycleMicroseconds = MaxMicrosecondsPerCycle;
unsigned long TimeOfCycleStart = 0;
unsigned Counter = 1;


unsigned long TimeOfLastSpeedUpdate = 0;
unsigned long lastPulse;
unsigned long lastAccuracy;
unsigned long RPM = 0;
unsigned long lastRPM;

void setup()
{
  pinMode(GreenLED_DOPin, OUTPUT);
  pinMode(RedLED_DOPin, OUTPUT);//as simple as that. or is it?
  pinMode(UpButton_DIPin, INPUT_PULLUP);
  pinMode(DownButton_DIPin, INPUT_PULLUP);
  pinMode(changeAccuracy, INPUT_PULLUP);
  
Serial.begin(2000000);
}



void loop()
{
  // GHet the current times at the start of loop()
  unsigned long currenmMillis = millis();
  unsigned long currentMicros = micros();


  static boolean greenIsOn = false;
  static boolean redPulseStarted = false;


  // Is it time to check the buttons?
  if (currenmMillis - TimeOfLastSpeedUpdate >= ButtonUpdateIntervalMillis)
  {
    TimeOfLastSpeedUpdate = currenmMillis;


    boolean upPressed = digitalRead(UpButton_DIPin) == LOW;
    boolean downPressed = digitalRead(DownButton_DIPin) == LOW;


      //   if(Counter == 1)
         //{
        //  ChangePerButtonPress = change_HIGH;
        // delay(10);
         //}
         //if(Counter == 2)
         //{
         // ChangePerButtonPress = change_MID;
         //delay(10);
         //}
         //if(Counter == 3)
         //{
          //ChangePerButtonPress = change_LOW;
         //delay(10);
         //}
    if(digitalRead(changeAccuracy) == LOW)
    {
      Counter = Counter++;
      if(Counter == 4)
      {
        Counter = 1;
      }
    }
     switch (Counter) 
     {
      case 1:
       ChangePerButtonPress = change_HIGH;
       delay(10);
       break;

      case 2:
       ChangePerButtonPress = change_MID;
       delay(10);
       break;
       
      case 3:
       ChangePerButtonPress = change_LOW;
       delay(10);   
       break;
    }
     
         
    
    if (upPressed)
    {
      PulseCycleMicroseconds += ChangePerButtonPress;
    }
    if (downPressed)
    {
      PulseCycleMicroseconds -= ChangePerButtonPress;
    }


    // Keep the pulse length within limits
    PulseCycleMicroseconds = constrain(PulseCycleMicroseconds, MinMicrosecondsPerCycle, MaxMicrosecondsPerCycle);
  }


  unsigned long cycleElapsed = currentMicros - TimeOfCycleStart;


  // Now check to see if the cycle is over
  if (cycleElapsed >= PulseCycleMicroseconds)
  {
    // Old cycle is over.  Starting a new one.
    TimeOfCycleStart = currentMicros;
    // Starting the Green pulse
    digitalWrite(GreenLED_DOPin, HIGH);
    greenIsOn = true;
    redPulseStarted = false;
  }


  // Is the Green pulse over?
  if (greenIsOn && cycleElapsed >= PulseMicroseconds)
  {
    // Green pulse is over.
    digitalWrite(GreenLED_DOPin, LOW);
    greenIsOn = false;
  }


  // Is it time to start the Red pulse?
  if (!redPulseStarted && cycleElapsed >= PulseCycleMicroseconds / 2)
  {
    // Start the Red pulse.
    digitalWrite(RedLED_DOPin, HIGH);
    redPulseStarted = true;
  }
 
  // Is the Red pulse over?
  if (redPulseStarted && cycleElapsed >= (PulseCycleMicroseconds / 2) + PulseMicroseconds)
  {
    // Red pulse is over.
    digitalWrite(RedLED_DOPin, LOW);
  }
 if(lastPulse != PulseCycleMicroseconds)
 { 
  Serial.print("Pulse Time : ");
  Serial.println(PulseCycleMicroseconds);
 }
 if(lastAccuracy != ChangePerButtonPress)
 {
  Serial.println("Change per button press : ");
  Serial.println(ChangePerButtonPress);
 }
 
 RPM = (60*1000*1000)/PulseCycleMicroseconds;
 if(RPM != lastRPM)
 { 
 Serial.println("RPM : ");
 Serial.print(RPM);
 }
 lastPulse = PulseCycleMicroseconds;
 lastAccuracy = ChangePerButtonPress;
 lastRPM = RPM;
}

even if i add extra () like , ((6010001000)/PulseCycleMicroseconds); , even then, it gives out wrong output. Say at the start of the code, the PulseCycleMicroseconds value is 50000, so it should give out a value of 1200, but it says 8298 or something like this. I have NO need of floating points.

kaseftamjid:
Technically, 0 isnt a number

Wow, all these years, I've been getting it wrong.

TheMemberFormerlyKnownAsAWOL:
Wow, all these years, I've been getting it wrong.

New math because 0 is offensive to some.

You need to meet my imaginary friend, j.

Some of my other friends are quite irrational

TheMemberFormerlyKnownAsAWOL:
You need to meet my imaginary friend, j.

Some of my other friends are quite irrational

Nice one, it doubles the sum of jokes i know..

Umm.. thanks, i figured it out, my calculations overflowed. I forced unsigned long..