Go Down

Topic: using the Mode Switch Manager library (Read 667 times) previous topic - next topic

urbanmiles

Hi guys , I've been using Mr. Nick Gammon's Mode Switch Manager and it works and helps well .
however , I think I need  a deeper understanding of this method .

below is my sketch and this is what i want to happen:

1- at button off
   * turn off all ledPins

2- if button has been OFF for >=3s and i then press it
  * turn on playPin

3- after 5s has passed upon pressing(button still being pressed)
  * turn on all ledPins


HOwever, processes 1 and 2 are the only ones happening . process 3 is not .
am i coding it right ?
my perception is that if the intervals are met , i can do whatever i want  ,
  tnx in advance guys

Code: [Select]


#include <SwitchManager.h>
SwitchManager modeSW;  // create the object

const byte ModeSW = 2;//input from the Mode push button switch   
const int playPin = 7;
const int ledPin1 = 8;
const int ledPin2 = 9;
const int ledPin3 = 10;
const int ledPin4 = 11;




void setup ()
{
  modeSW.begin (ModeSW, modeSwitchManager);
  pinMode(ledPin1, OUTPUT); 
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(playPin, OUTPUT);

}

void loop ()
  {
  //check to see what's happening with the Mode switch
  modeSW.check();

  //other suff
  }


//                M O D E   S W I T C H   M A N A G E R
//****************************************************************
// function looks after the Mode switch functions
void modeSwitchManager (const byte newState, const unsigned long interval)
{
 
 
 
if (newState == LOW)  //in this case ignor a switch release
{       
   
      digitalWrite(ledPin1, LOW);     
      digitalWrite(ledPin2, LOW);     
      digitalWrite(ledPin3, LOW);
      digitalWrite(ledPin4, LOW);     
      return;
      }
  //the newSatate must be LOW
  //-----------------------------------
     
if (interval >=1000)
{     
      digitalWrite(playPin, HIGH);
      return; 


  //-----------------------------------
      if (interval >=5000)
      {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, HIGH);
      digitalWrite(ledPin4, HIGH);   
      return;
      }
 
  //-----------------------------------

      if (interval <= 250) 
      {

      return;
      }

      return;

      }  //                  END of modeSwitchManager()

nickgammon

Code: [Select]

  if (interval >=1000)
  {     
    digitalWrite(playPin, HIGH);
    return; 
  } 

  //-----------------------------------
  if (interval >=5000)
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);   
    return;
  }



If the interval is >= 1000 then you return. Thus you never test for interval >= 5000. That's because 5000 is also >= 1000.

Maybe reverse the order of those tests.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

urbanmiles

thanks Mr Nick ! i also tried using delay within a single perimeter (e.g if interval >= 3000) and it also works the way i want it to be . i just want to omit using the delay ...

Pagework

Just a quick, but big thank you to Nick Gammon for SwitchManager.
Just got my motorcycle all wired up with Brake, Indicator and Hazard lights working flawlessly.
I did independent hazard switch. With a Nano I figure I have another 3 switch to play with.

Grip and seat heater switch
Jacket heater switch
Extra running lights switch

Thanks again for the great examples on your site


Go Up