What I've done is a) improved my naming and b) compared my code from before when I was using a push button and my code now that I've integrated my analog reading.
To recap, before, the servo swung to 180deg and then swung straight back. Ideal for ringing a bell on a button push. This is my desired result.
I've noticed two things:-
Previous code
if (nurseryvalue == HIGH)
{
if (FrontDoorRoutine == OFF)
{
// button pressed
//===============
FrontDoorRoutine = ON;
if (FrontDoorLED == OFF)
{
// the button's pressed and LED is OFF
// turn it ON as this is a "first press".
//====================================
digitalWrite(ledPin1, ON);
FrontDoorLED = ON;
RingBellServo.write(180);
FrontDoorLEDTimeStamp1 = currentMillis;
}
else if (FrontDoorLED == ON)
{
// the button's pressed and LED is ON
// start flashing as this is a "second press".
//======================================
digitalWrite(ledPin1, OFF);
FrontDoorLED = FLASH;
RingBellServo.write(180);
FrontDoorLEDTimeStamp1 = currentMillis;
}
}
}
else
{
FrontDoorRoutine = OFF;
RingBellServo.write(0);
}
Current code
if (DoorBellTriggered == 1)
{
if (FrontDoorRoutine == OFF)
{
// button pressed
//===============
FrontDoorRoutine = ON;
if (FrontDoorLED == OFF)
{
// the button's pressed and LED is OFF
// turn it ON as this is a "first press".
//====================================
digitalWrite(LEDPin1, ON);
FrontDoorLED = ON;
RingBellServo.write(180);
FrontDoorLEDTimeStamp1 = currentMillis;
DoorBellTriggered = 0;
}
else if (FrontDoorLED == ON)
{
// the button's pressed and LED is ON
// start flashing as this is a "second press".
//======================================
digitalWrite(LEDPin1, OFF);
FrontDoorLED = FLASH;
RingBellServo.write(180);
FrontDoorLEDTimeStamp1 = currentMillis;
DoorBellTriggered = 0;
}
}
else
{
FrontDoorRoutine = OFF;
RingBellServo.write(0);
}
}
- I seem to have lost a closing bracket in my newer code after the else if routine. Surely this is adversely affecting how/when/if the else routine is operating.
- I think I'm resetting DoorBellTriggered in the wrong place. Perhaps it would be better to reset it straight after the if routine that checks for DoorBellTriggered being true is found to be true.
How should a servo operate?
- Should it:
a) move to a position and stay there
or
b) should it move back when it's reached that position? - If it's told to move to position 180 and then in the very next line told to move to position 0, should it complete the first move before doing the next, or does having two instructions so close together effectively cancel the first move out?
THANKS