Toggle switch state

You are missing the point.

  • As mentioned by @DaveX, Larry is looking for useful names for those codes.
    i.e. what are the names of the remote buttons used ?.

  • Lets say this is the IR remote being used:

  • For example:

Instead of using this style:

    //============ go to 180° ?
    //Check for other IR codes (servo control)
    else if (destinationServoPosition == MINIMUM && rawData ==0xBC43FF00)
    {

For documentation we use this style:

#define OKbutton      0xB847FF00
#define leftArrow     0xBC43FF00
#define rightArrow    0xBB44FF00
. . . 

    //============ go to 180° ?
    //Check for other IR codes (servo control)
    else if (destinationServoPosition == MINIMUM && rawData == leftArrow )
    {
  • Do you see the benifit when it comes to documentation ?
1 Like

Yeah i see that now, it makes it easier to understand whether I'm the author or just looking for help. Thanks

One thing I learned from this thread is that those IR codes have two parts... the 0xB847FF00 splits into a command 0xB847 and an address 0xFF00 and each of those parts commonly splits into the number and its complement: ~0xB8 = 0x47 and ~0xFF == 0x00. Since it is transmitted LSB first, the address is 0x00 and the command is 0x47, each followed by their complement. (Uncommonly, if the manufacturer needed more than 256 codes, they could break the compliment and use some of the 16 bit command space.) If the remote uses the same address for all the keys, then you could shortcut the address and discard the checkdigit with something like command=rawData >>16 & 0xFF.

1 Like

@LarryD After taking a break due to circumstances, why is it that whenever i change the value of destinationServoPosition from MAXIMUM to MINIMUM it doesn't work? (by the way i changed the value of LDR from > to < 300)

  • That’s okay, use what you want.

  • Assume the LDR has been ENABLED by receiving the IR code when the servo is sitting at Minimum.

  • When LDR reaches the trigger point, the servo should go to Maximum.

  • If the above does not happen, show us good images of the LDR wiring, also, you can add a print state to check the trigger point has indeed been reached




  • Please confirm what is supposed to happen when at the servo is at Maximum and the LDR is then enabled and then triggered ?

  • Please confirm what is supposed to happen when at the servo is at Minimum and the LDR is then enabled and then triggered.

okay, everything in the circuit works as it's supposed to. the wiring is exactly as in post #80. Once the LDR is <300 I want it to go in the same direction as the forward key which is 0. But it doesn't move, however when i set the code to maximum which is 180 degree it does move. by the way when testing i ensure the servo is at the opposite angle and i also ensure the LDR is covered so that it is reading below 300. Everything else is the same conditions just changed 'MAXIMUM' to 'MINIMUM' in that if loop.

When the servo is at Maximum and the LDR gets a reading below 300, the servo should then go to 0 (MINIMUM). If its at MINIMUM already nothing should happen.

  • The sketch is well documented, you should have been able to have made the needed changes. :thinking:

  • This version does the following:
    When at MAXIMUM and LDR is ENABLED and LDR < 300, servo goes to MINIMUM

//================================================^================================================
//
//  https://forum.arduino.cc/t/toggle-switch-state/1303593
//
//
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ========================================================================
//  1.00       24/09/20    Running code
//  1.10       24/09/21    Minor changes, added comments, baud ate changed to 115200
//  1.20       24/09/21    Added IR code, added LDR code
//  1.30       24/09/22    Added IR keypad button controls for LarryD's remote, delete these as needed
//  1.40       24/09/22    A switch operation is now reversed
//  1.50       24/09/22    Modified so any change in the toggle switch state operates the servo
//  1.60       24/09/22    When at MAXIMUM and LDR is ENABLED and LDR < 300, servo goes to MINIMUM
//
//
//
//
//  Notes:
//


#include <IRremote.hpp>
#include <Servo.h>


//================================================
#define LEDon              HIGH   //PIN---[220R]---A[LED]K---GND
#define LEDoff             LOW

#define PRESSED            LOW    //+5V---[Internal 50k]---PIN---[Switch]---GND
#define RELEASED           HIGH

#define CLOSED             LOW    //+5V---[Internal 50k]---PIN---[Switch]---GND
#define OPENED             HIGH

#define ENABLED            true
#define DISABLED           false

#define MAXIMUM            180
#define MINIMUM            0

//                          millis() / micros()   B a s e d   T I M E R S
//================================================^================================================
//To keep the sketch tidy, you can put this structure in a different tab in the IDE
//
//These TIMER objects are non-blocking
struct makeTIMER
{
#define MILLIS             0
#define MICROS             1

#define ENABLED            true
#define DISABLED           false

#define YES                true
#define NO                 false

#define STILLtiming        0
#define EXPIRED            1
#define TIMERdisabled      2

  //these are the bare minimum "members" needed when defining a TIMER
  byte                     TimerType;      //what kind of TIMER is this? MILLIS/MICROS
  unsigned long            Time;           //when the TIMER started
  unsigned long            Interval;       //delay time which we are looking for
  bool                     TimerFlag;      //is the TIMER enabled ? ENABLED/DISABLED
  bool                     Restart;        //do we restart this TIMER   ? YES/NO

  //================================================
  //condition returned: STILLtiming (0), EXPIRED (1) or TIMERdisabled (2)
  //function to check the state of our TIMER  ex: if(myTimer.checkTIMER() == EXPIRED);
  byte checkTIMER()
  {
    //========================
    //is this TIMER enabled ?
    if (TimerFlag == ENABLED)
    {
      //========================
      //has this TIMER expired ?
      if (getTime() - Time >= Interval)
      {
        //========================
        //should this TIMER restart again?
        if (Restart == YES)
        {
          //restart this TIMER
          Time = getTime();
        }

        //this TIMER has expired
        return EXPIRED;
      }

      //========================
      else
      {
        //this TIMER has not expired
        return STILLtiming;
      }

    } //END of   if (TimerFlag == ENABLED)

    //========================
    else
    {
      //this TIMER is disabled
      return TIMERdisabled;
    }

  } //END of   checkTime()

  //================================================
  //function to enable and restart this TIMER  ex: myTimer.enableRestartTIMER();
  void enableRestartTIMER()
  {
    TimerFlag = ENABLED;

    //restart this TIMER
    Time = getTime();

  } //END of   enableRestartTIMER()

  //================================================
  //function to disable this TIMER  ex: myTimer.disableTIMER();
  void disableTIMER()
  {
    TimerFlag = DISABLED;

  } //END of    disableTIMER()

  //================================================
  //function to restart this TIMER  ex: myTimer.restartTIMER();
  void restartTIMER()
  {
    Time = getTime();

  } //END of    restartTIMER()

  //================================================
  //function to force this TIMER to expire ex: myTimer.expireTimer();
  void expireTimer()
  {
    //force this TIMER to expire
    Time = getTime() - Interval;

  } //END of   expireTimer()

  //================================================
  //function to set the Interval for this TIMER ex: myTimer.setInterval(100);
  void setInterval(unsigned long value)
  {
    //set the Interval
    Interval = value;

  } //END of   setInterval()

  //================================================
  //function to return the current time
  unsigned long getTime()
  {
    //return the time             i.e. millis() or micros()
    //========================
    if (TimerType == MILLIS)
    {
      return millis();
    }

    //========================
    else
    {
      return micros();
    }

  } //END of   getTime()

}; //END of   struct makeTIMER


//                            D e f i n e   a l l   a r e   T I M E R S
//================================================^================================================
/*example
  //========================
  makeTIMER toggleLED =
  {
     MILLIS/MICROS, 0, 500ul, ENABLED/DISABLED, YES/NO  //.TimerType, .Time, .Interval, .TimerFlag, .Restart
  };

  TIMER functions we can access:
  toggleLED.checkTIMER();
  toggleLED.enableRestartTIMER();
  toggleLED.disableTIMER();
  toggleLED.expireTimer();
  toggleLED.setInterval(100ul);
*/

//========================
makeTIMER heartbeatTIMER =
{
  MILLIS, 0, 250ul, ENABLED, YES       //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//========================
makeTIMER switchesTIMER =
{
  MILLIS, 0, 50ul, ENABLED, YES        //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//========================
makeTIMER CHECKirTIMER =
{
  MICROS, 0, 1000ul, ENABLED, YES      //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//========================
makeTIMER servoTIMER =
{
  MILLIS, 0, 10ul, DISABLED, YES      //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};


//                                            G P I O s
//================================================^================================================
//
const byte photoPin               = A0;

const byte IRpin                  = 2;
const byte toggleSwitchPin        = 5;

const byte servoPin               = 9;
const byte heartbeatLED           = 13;


//                                        V A R I A B L E S
//================================================^================================================
//
Servo servo;

bool photoSensor                  = DISABLED;  //LDR

byte lastButtonState              = RELEASED;

int currentServoPosition          = 0;
int destinationServoPosition      = MINIMUM;


//                                           s e t u p ( )
//================================================^================================================
void setup()
{
  Serial.begin(115200);

  IrReceiver.begin(IRpin);

  pinMode(toggleSwitchPin, INPUT_PULLUP);

  digitalWrite(heartbeatLED, LEDoff);
  pinMode(heartbeatLED, OUTPUT);

  //========================
  //connect the servo, it moves to 90°
  servo.attach(servoPin);
  delay(1000);

  currentServoPosition = 90;
  destinationServoPosition = MINIMUM;

  Serial.print("Servo position = ");
  Serial.println(currentServoPosition);

  //enabled the TIMER to allow the servo to step
  servoTIMER.enableRestartTIMER();

} //END of   setup()


//                                            l o o p ( )
//================================================^================================================
void loop()
{
  //========================================================================  T I M E R  heartbeatLED
  //condition returned: STILLtiming, EXPIRED or TIMERdisabled
  //is it time to toggle the heartbeat LED ?
  if (heartbeatTIMER.checkTIMER() == EXPIRED)
  {
    //toggle the heartbeat LED
    digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
  }

  //========================================================================  T I M E R  switches
  //condition returned: STILLtiming, EXPIRED or TIMERdisabled
  //is it time to check our switches ?
  if (switchesTIMER.checkTIMER() == EXPIRED)
  {
    checkSwitches();
  }

  //========================================================================  T I M E R  CHECKir
  //condition returned: STILLtiming, EXPIRED or TIMERdisabled
  //is it time to check for an IR code ?
  if (CHECKirTIMER.checkTIMER() == EXPIRED)
  {
    checkIRcode();
  }

  //========================================================================  T I M E R  servo
  //condition returned: STILLtiming, EXPIRED or TIMERdisabled
  //if this TIMER is enabled, is it time to move the servo 1 step ?
  if (servoTIMER.checkTIMER() == EXPIRED)
  {
    //go CCW to maximum position ?
    if (destinationServoPosition == MAXIMUM && currentServoPosition <= destinationServoPosition)
    {
      //move one step CCW
      servo.write(currentServoPosition);

      Serial.print("CCW, servo position = ");
      Serial.println(currentServoPosition);

      Serial.print("Destination = ");
      Serial.println(destinationServoPosition);

      //get ready for the next step
      currentServoPosition++;

      //don't go over 180
      if (currentServoPosition > destinationServoPosition)
      {
        //re-adjust
        currentServoPosition = 180;

        Serial.println("We are at maximum");

        //we have arrived at our destination, disable this TIMER
        servoTIMER.disableTIMER();

        //cancel an LDR pending control
        photoSensor = DISABLED;
      }
    }

    //go CW to minimum position ?
    else if (destinationServoPosition == MINIMUM && currentServoPosition >= destinationServoPosition)
    {
      //move one step CW
      servo.write(currentServoPosition);

      Serial.print("CW, servo position = ");
      Serial.println(currentServoPosition);

      Serial.print("Destination = ");
      Serial.println(destinationServoPosition);

      //get ready for the next step
      currentServoPosition--;

      //don't go under 0
      if (currentServoPosition < 0)
      {
        //re-adjust
        currentServoPosition = 0;

        Serial.println("We are at minimum");

        //we have arrived at our destination, disable this TIMER
        servoTIMER.disableTIMER();

        //cancel an LDR pending control
        photoSensor = DISABLED;
      }
    }
  }


  //================================================
  //other non blocking code goes here
  //================================================


} //END of   loop()


//                                     c h e c k I R c o d e ( )
//================================================^================================================
void checkIRcode()
{
  //========================
  if (IrReceiver.decode())
  {
    static unsigned long rawData;

    //is this a repeat code ?
    if (IrReceiver.decodedIRData.decodedRawData == 0)
    {
      IrReceiver.resume();

      return;
    }

    //get the new IR code
    rawData = IrReceiver.decodedIRData.decodedRawData;

    //Serial.print("\nIR Receive code = ");
    //Serial.println(rawData, HEX);

    //============ LDR ON
    //when we are at MAXIMUM, check for IR code to enable LDR
    if (destinationServoPosition == MAXIMUM && (rawData == 0xB847FF00 || rawData == 0xBA45FF00 || rawData == 0xBF40FF00))
    {
      photoSensor = ENABLED;

      Serial.println("LDR is ENABLED");
    }

    //============ go to 180° ?    
    else if (destinationServoPosition == MINIMUM && (rawData == 0xBC43FF00 || rawData == 0xF609FF00 || rawData == 0xB946FF00))
    {
      destinationServoPosition = MAXIMUM;

      //enabled the TIMER to allow the servo to step
      servoTIMER.enableRestartTIMER();
    }

    //============ go to 0° ?
    else if (destinationServoPosition == MAXIMUM && (rawData == 0xBB44FF00 || rawData == 0xF807FF00 || rawData == 0xEA15FF00))
    {
      destinationServoPosition = MINIMUM;

      //enabled the TIMER to allow the servo to step
      servoTIMER.enableRestartTIMER();
    }

    IrReceiver.resume();

  } //END of   if (receiver.decode())

} //END of   checkIRcode()


//                                   c h e c k S w i t c h e s ( )
//================================================^================================================
void checkSwitches()
{
  byte pinState;
  int LDRvalue;

  //========================================================================  photoSensor
  //are we allowed to read the LDR ?
  if (photoSensor == ENABLED)
  {
    LDRvalue = analogRead(photoPin);

    //have we reached the trigger point ?
    if (LDRvalue < 300)
    {
      //we have now reached the trigger point, disable the LDR
      photoSensor = DISABLED;

      destinationServoPosition = MINIMUM;

      //enabled the TIMER to allow the servo to step
      servoTIMER.enableRestartTIMER();
    }
  }

  //========================================================================  toggleSwitchPin
  pinState = digitalRead(toggleSwitchPin);

  //===================================
  //has this switch changed state ?
  if (lastButtonState != pinState)
  {
    //update to this new state
    lastButtonState = pinState;

    //========================
    //the switch has changed state
    //cancel any LDR pending control
    photoSensor = DISABLED;

    Serial.println("\nSwitch state has changed");

    //============
    //toggle the servo destination ?
    if (destinationServoPosition == MINIMUM)
    {
      destinationServoPosition = MAXIMUM;
    }

    //============
    //toggle the servo destination ?
    else if (destinationServoPosition == MAXIMUM)
    {
      destinationServoPosition = MINIMUM;
    }

    //allow the servo to step
    servoTIMER.enableRestartTIMER();

  } //END of  toggleSwitchPin

  //========================================================================  Next Switch

} //END of   checkSwitches()


//================================================^================================================
1 Like

Thanks a bunch for the help. If you don't mind, can you tell me what the problem was?

Am i able to use this


to control the power supply for my servo and for the arduino? if yes please point me to a guide.

1 Like
  • Let's run a test on this sketch version.

  • Confirm this version works as needed ?

//================================================^================================================
//
//  https://forum.arduino.cc/t/toggle-switch-state/1303593
//
//
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ========================================================================
//  1.00       24/09/20    Running code
//  1.10       24/09/21    Minor changes, added comments, baud ate changed to 115200
//  1.20       24/09/21    Added IR code, added LDR code
//  1.30       24/09/22    Added IR keypad button controls for LarryD's remote, delete these as needed
//  1.40       24/09/22    A switch operation is now reversed
//  1.50       24/09/22    Modified so any change in the toggle switch state operates the servo
//  1.60       24/09/22    When at MAXIMUM and LDR is ENABLED and LDR < 300, servo goes to MINIMUM
//
//
//
//
//  Notes:
//


#include <IRremote.hpp>
#include <Servo.h>


//================================================
#define LEDon              HIGH   //PIN---[220R]---A[LED]K---GND
#define LEDoff             LOW

#define PRESSED            LOW    //+5V---[Internal 50k]---PIN---[Switch]---GND
#define RELEASED           HIGH

#define CLOSED             LOW    //+5V---[Internal 50k]---PIN---[Switch]---GND
#define OPENED             HIGH

#define ENABLED            true
#define DISABLED           false

#define MAXIMUM            180
#define MINIMUM            0


//                          millis() / micros()   B a s e d   T I M E R S
//================================================^================================================
//To keep the sketch tidy, you can put this structure in a different tab in the IDE
//
//These TIMER objects are non-blocking
struct makeTIMER
{
#define MILLIS             0
#define MICROS             1

#define ENABLED            true
#define DISABLED           false

#define YES                true
#define NO                 false

#define STILLtiming        0
#define EXPIRED            1
#define TIMERdisabled      2

  //these are the bare minimum "members" needed when defining a TIMER
  byte                     TimerType;      //what kind of TIMER is this? MILLIS/MICROS
  unsigned long            Time;           //when the TIMER started
  unsigned long            Interval;       //delay time which we are looking for
  bool                     TimerFlag;      //is the TIMER enabled ? ENABLED/DISABLED
  bool                     Restart;        //do we restart this TIMER   ? YES/NO

  //================================================
  //condition returned: STILLtiming (0), EXPIRED (1) or TIMERdisabled (2)
  //function to check the state of our TIMER  ex: if(myTimer.checkTIMER() == EXPIRED);
  byte checkTIMER()
  {
    //========================
    //is this TIMER enabled ?
    if (TimerFlag == ENABLED)
    {
      //========================
      //has this TIMER expired ?
      if (getTime() - Time >= Interval)
      {
        //========================
        //should this TIMER restart again?
        if (Restart == YES)
        {
          //restart this TIMER
          Time = getTime();
        }

        //this TIMER has expired
        return EXPIRED;
      }

      //========================
      else
      {
        //this TIMER has not expired
        return STILLtiming;
      }

    } //END of   if (TimerFlag == ENABLED)

    //========================
    else
    {
      //this TIMER is disabled
      return TIMERdisabled;
    }

  } //END of   checkTime()

  //================================================
  //function to enable and restart this TIMER  ex: myTimer.enableRestartTIMER();
  void enableRestartTIMER()
  {
    TimerFlag = ENABLED;

    //restart this TIMER
    Time = getTime();

  } //END of   enableRestartTIMER()

  //================================================
  //function to disable this TIMER  ex: myTimer.disableTIMER();
  void disableTIMER()
  {
    TimerFlag = DISABLED;

  } //END of    disableTIMER()

  //================================================
  //function to restart this TIMER  ex: myTimer.restartTIMER();
  void restartTIMER()
  {
    Time = getTime();

  } //END of    restartTIMER()

  //================================================
  //function to force this TIMER to expire ex: myTimer.expireTimer();
  void expireTimer()
  {
    //force this TIMER to expire
    Time = getTime() - Interval;

  } //END of   expireTimer()

  //================================================
  //function to set the Interval for this TIMER ex: myTimer.setInterval(100);
  void setInterval(unsigned long value)
  {
    //set the Interval
    Interval = value;

  } //END of   setInterval()

  //================================================
  //function to return the current time
  unsigned long getTime()
  {
    //return the time             i.e. millis() or micros()
    //========================
    if (TimerType == MILLIS)
    {
      return millis();
    }

    //========================
    else
    {
      return micros();
    }

  } //END of   getTime()

}; //END of   struct makeTIMER


//                            D e f i n e   a l l   a r e   T I M E R S
//================================================^================================================
/*example
  //========================
  makeTIMER toggleLED =
  {
     MILLIS/MICROS, 0, 500ul, ENABLED/DISABLED, YES/NO  //.TimerType, .Time, .Interval, .TimerFlag, .Restart
  };

  TIMER functions we can access:
  toggleLED.checkTIMER();
  toggleLED.enableRestartTIMER();
  toggleLED.disableTIMER();
  toggleLED.expireTimer();
  toggleLED.setInterval(100ul);
*/

//========================
makeTIMER heartbeatTIMER =
{
  MILLIS, 0, 250ul, ENABLED, YES       //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//========================
makeTIMER switchesTIMER =
{
  MILLIS, 0, 50ul, ENABLED, YES        //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//========================
makeTIMER CHECKirTIMER =
{
  MICROS, 0, 1000ul, ENABLED, YES      //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//========================
makeTIMER servoTIMER =
{
  MILLIS, 0, 10ul, DISABLED, YES      //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};


//                                            G P I O s
//================================================^================================================
//
const byte photoPin               = A0;

const byte IRpin                  = 2;
const byte toggleSwitchPin        = 5;

const byte servoPin               = 9;
const byte heartbeatLED           = 13;


//                                        V A R I A B L E S
//================================================^================================================
//
Servo servo;

bool photoSensor                  = DISABLED;  //LDR

byte lastButtonState              = RELEASED;

int currentServoPosition          = 0;
int destinationServoPosition      = MINIMUM;


//                                           s e t u p ( )
//================================================^================================================
void setup()
{
  Serial.begin(115200);

  IrReceiver.begin(IRpin);

  pinMode(toggleSwitchPin, INPUT_PULLUP);

  digitalWrite(heartbeatLED, LEDoff);
  pinMode(heartbeatLED, OUTPUT);

  //========================
  //connect the servo, it moves to 90°
  servo.attach(servoPin);
  delay(1000);

  currentServoPosition = 90;
  destinationServoPosition = MINIMUM;

  Serial.print("Servo position = ");
  Serial.println(currentServoPosition);

  //enabled the TIMER to allow the servo to step
  servoTIMER.enableRestartTIMER();

} //END of   setup()


//                                            l o o p ( )
//================================================^================================================
void loop()
{
  //========================================================================  T I M E R  heartbeatLED
  //condition returned: STILLtiming, EXPIRED or TIMERdisabled
  //is it time to toggle the heartbeat LED ?
  if (heartbeatTIMER.checkTIMER() == EXPIRED)
  {
    //toggle the heartbeat LED
    digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
  }

  //========================================================================  T I M E R  switches
  //condition returned: STILLtiming, EXPIRED or TIMERdisabled
  //is it time to check our switches ?
  if (switchesTIMER.checkTIMER() == EXPIRED)
  {
    checkSwitches();
  }

  //========================================================================  T I M E R  CHECKir
  //condition returned: STILLtiming, EXPIRED or TIMERdisabled
  //is it time to check for an IR code ?
  if (CHECKirTIMER.checkTIMER() == EXPIRED)
  {
    checkIRcode();
  }

  //========================================================================  T I M E R  servo
  //condition returned: STILLtiming, EXPIRED or TIMERdisabled
  //if this TIMER is enabled, is it time to move the servo 1 step ?
  if (servoTIMER.checkTIMER() == EXPIRED)
  {
    //go CCW to maximum position ?
    if (destinationServoPosition == MAXIMUM && currentServoPosition <= destinationServoPosition)
    {
      //move one step CCW
      servo.write(currentServoPosition);

      Serial.print("CCW, servo position = ");
      Serial.println(currentServoPosition);

      Serial.print("Destination = ");
      Serial.println(destinationServoPosition);

      //get ready for the next step
      currentServoPosition++;

      //don't go over MAXIMUM
      if (currentServoPosition > destinationServoPosition)
      {
        //re-adjust
        currentServoPosition = MAXIMUM;

        Serial.println("We are at maximum");

        //we have arrived at our destination, disable this TIMER
        servoTIMER.disableTIMER();

        //cancel an LDR pending control
        photoSensor = DISABLED;
      }
    }

    //go CW to minimum position ?
    else if (destinationServoPosition == MINIMUM && currentServoPosition >= destinationServoPosition)
    {
      //move one step CW
      servo.write(currentServoPosition);

      Serial.print("CW, servo position = ");
      Serial.println(currentServoPosition);

      Serial.print("Destination = ");
      Serial.println(destinationServoPosition);

      //get ready for the next step
      currentServoPosition--;

      //don't go under MINIMUM
      if (currentServoPosition < MINIMUM)
      {
        //re-adjust
        currentServoPosition = MINIMUM;

        Serial.println("We are at minimum");

        //we have arrived at our destination, disable this TIMER
        servoTIMER.disableTIMER();

        //cancel an LDR pending control
        photoSensor = DISABLED;
      }
    }
  }


  //================================================
  //other non blocking code goes here
  //================================================


} //END of   loop()


//                                     c h e c k I R c o d e ( )
//================================================^================================================
void checkIRcode()
{
  //========================
  if (IrReceiver.decode())
  {
    static unsigned long rawData;

    //is this a repeat code ?
    if (IrReceiver.decodedIRData.decodedRawData == 0)
    {
      IrReceiver.resume();

      return;
    }

    //get the new IR code
    rawData = IrReceiver.decodedIRData.decodedRawData;

    //Serial.print("\nIR Receive code = ");
    //Serial.println(rawData, HEX);

    //============ LDR ON
    //when we are at MAXIMUM, check for IR code to enable LDR
    if (destinationServoPosition == MAXIMUM && (rawData == 0xB847FF00 || rawData == 0xBA45FF00 || rawData == 0xBF40FF00))
    {
      photoSensor = ENABLED;

      Serial.println("LDR is ENABLED");
    }

    //============ go to 180° ?    
    else if (destinationServoPosition == MINIMUM && (rawData == 0xBC43FF00 || rawData == 0xF609FF00 || rawData == 0xB946FF00))
    {
      destinationServoPosition = MAXIMUM;

      //enabled the TIMER to allow the servo to step
      servoTIMER.enableRestartTIMER();
    }

    //============ go to 0° ?
    else if (destinationServoPosition == MAXIMUM && (rawData == 0xBB44FF00 || rawData == 0xF807FF00 || rawData == 0xEA15FF00))
    {
      destinationServoPosition = MINIMUM;

      //enabled the TIMER to allow the servo to step
      servoTIMER.enableRestartTIMER();
    }

    IrReceiver.resume();

  } //END of   if (receiver.decode())

} //END of   checkIRcode()


//                                   c h e c k S w i t c h e s ( )
//================================================^================================================
void checkSwitches()
{
  byte pinState;
  int LDRvalue;

  //========================================================================  photoSensor
  //are we allowed to read the LDR ?
  if (photoSensor == ENABLED)
  {
    LDRvalue = analogRead(photoPin);

    //have we reached the trigger point ?
    if (LDRvalue < 300)
    {
      //we have now reached the trigger point, disable the LDR
      photoSensor = DISABLED;

      destinationServoPosition = MINIMUM;

      //enabled the TIMER to allow the servo to step
      servoTIMER.enableRestartTIMER();
    }
  }

  //========================================================================  toggleSwitchPin
  pinState = digitalRead(toggleSwitchPin);

  //===================================
  //has this switch changed state ?
  if (lastButtonState != pinState)
  {
    //update to this new state
    lastButtonState = pinState;

    //========================
    //the switch has changed state
    //cancel any LDR pending control
    photoSensor = DISABLED;

    Serial.println("\nSwitch state has changed");

    //============
    //toggle the servo destination ?
    if (destinationServoPosition == MINIMUM)
    {
      destinationServoPosition = MAXIMUM;
    }

    //============
    //toggle the servo destination ?
    else if (destinationServoPosition == MAXIMUM)
    {
      destinationServoPosition = MINIMUM;
    }

    //allow the servo to step
    servoTIMER.enableRestartTIMER();

  } //END of  toggleSwitchPin

  //========================================================================  Next Switch

} //END of   checkSwitches()


//================================================^================================================

it will take some time to get back to you, i have already fully disassembled the original and is moving it to an arduino nano for the final parts. Is that sketch the previous one you sent in post #87?

  • It is a new sketch.

okay, i will. is it bad that I am still struggling to understand what pins on the switch connect to which of each power supply?

  • Battery is 7 to 9V, however, there is a drain on the battery when the switch is ON




  • It would be best to use a AC to DC converter with 5V out.
  • S8 must be OFF when the USB cable is connected to a PC !




To prevent shorts, after soldering wires to the switch, put heat shrink over the connections or add silicone glue to all three terminals.

  • Those switches are usually 10 amps, probably okay for this project though.

so is it not possible to connect 2 different batteries to be controlled by the switch? Example...
Servo battery & Arduino battery is both connected to the switch (see post #96 for the type of switch). So that when the switch is flipped it allows the arduino and servo to be powered on/off at the same time. Reason for wanting this is that i dont want the servo battery to be constantly on even when i power off the entire system, yes i am aware i can just put another switch but that would just look weird with 3 switches.

also, post #92 works the same. What changes did you make?

  • Now try these changes to see the results.

image

  • Remember, if the switch is ON, the batteries will eventually die as there is always a small amount of residual current being drawn.

  • S2 must be OFF when the USB cable is connected to a PC !