Analogread causing servo to misbehave

retrolefty:
So yes we sometime may come across as overly picky about wanting details before trying to give beginners possible solutions, but it's based on hearing lots of new comers with damaged boards asking for help and advice after it's a little to late to help, as the damage has already happened. :wink:
Lefty

Thanks, Lefty. Totally understand your concern here. I have used a multimeter myself on the speaker wire, but other than that, relied on the expertise of those who designed and successfully implemented this on the web.

I've modified my code to this. I hope it will be more successful. Going to test it later on tonight.

Would be interested, though, in any thoughts on why my original doesn't work. The signal is received and can be processed as many times as the button is pushed (the debouncing works, too) it's just the servo that stopped returning to base when I implemented the analogread. Perhaps because my else brackets got dislodged. Who knows. I don't. :slight_smile:

#include <Bounce.h>
#include <Wire.h>
#include "RTClib.h"
#include <Servo.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = {0x00, 0x11, 0x22, 0x33, 0xFB, 0x11};
EthernetClient client;
////////////NTP UPDATE////////////
unsigned int localPort = 8888;			// NTP Port
byte timeServer[] = {193, 79, 237, 14};	// ntp1.nl.net NTP server
const int NTP_PACKET_SIZE= 48;			// NTP time stamp is in the first 48 bytes of the message
byte pb[NTP_PACKET_SIZE];				// Buffer to hold incoming and outgoing packets
#if ARDUINO >= 100
EthernetUDP Udp;
#endif
unsigned long currentMillis = 0;
////////////CONNECTIVITY////////////
boolean EthernetRunning = false;		//ATTN! Not sure how this will affect things...
////////////SERVER////////////
IPAddress server(207,7,108,203);		// Joyent
////////////SERVO////////////
Servo servo1;
//////////// I/O PINS////////////
const int DoorMonitorPin = 5;			// the number of the magnetic reed switch pin 5
const int DoorBellPin = 0;				// white doorbell wire to ANALOG pin 0
const int LEDPin1 = 8;      			// the number of the LED pin 8
const int PiezoPin = 7;					// the number of the piezo buzzer pin 7
////////////DOORBELL 1 INPUT////////////
long debounce = 10000;   				// how long to leave before polling analog read again
int DoorBellPinVal = 0;			// Variable to store wireless doorbell read value
long DoorBellValTriggeredMillis = 0;	// When was the analog read triggered
int Button1State = 0;					// Used to set the button state
int DoorBellTriggered = 0;				// Used to set the triggered state
////////////LED 1 OUTPUT////////////
int LED1State  = 0;						// Used to turn the LED 'on' & make it flash.
long LED1OnDuration  = 30000;			// Time to keep the LED on for (30s)
long LED1TimeStamp1 = 0;				// Will store last time LED was updated for state 1
long LED1TimeStamp2 = 0;       			// Will store last time LED was updated for state 2
long FlashRate = 400;					// How fast should the LED flash?
////////////REED SWITCH////////////
long DoorMonitorDoorbellPressed = 0;	// Stores the millis value for the last time doorbell was rung for the reed switch routine
long DoorMonitorWaitTime = 60000;		// How many ms to wait to see if door is opened (1m)
int DoorMonitorWaitState = 0;			// The sketch is monitoring the reed switch if this is '1'
int DoorMonitorPinReadValue = 0;		// Stores the magnetic reed switch read value
////////////RTC////////////
RTC_DS1307 RTC;
enum {
  OFF, ON, FLASH
};
void setup() {
  pinMode(LEDPin1,OUTPUT);
  pinMode(PiezoPin, OUTPUT);
  pinMode(DoorMonitorPin, INPUT);
  digitalWrite(DoorMonitorPin, HIGH);		// turn on reed input pin's pull-up resistor
  digitalWrite(14 + DoorBellPin, HIGH);		// set pullup on the analog pin
  digitalWrite(LEDPin1, OFF);
  servo1.attach(6);
  LED1State  = OFF;
  Serial.begin(57600);
  Wire.begin();
  RTC.begin();

  Serial.println("Annunciator Panel v0.9");
  Ethernet.begin(mac);
  if (Ethernet.begin(mac) == 0)
  {
    Serial.println("Error: No DHCP!");
    EthernetRunning = false;
  }
  if(EthernetRunning)
  {
    Udp.begin(localPort);
    Serial.println("DHCP is working so enabling UDP.");
  }
  Serial.println();
  if (! RTC.isrunning())
  {
    Serial.println("Error: The RTC is NOT available!");
  }
}
void loop() {
  DateTime now = RTC.now();
  currentMillis = millis();
  DoorBellPinVal = analogRead(DoorBellPin);
  
  if (DoorBellPinVal < 100) {
    if (currentMillis-DoorBellValTriggeredMillis > debounce)  	// And if we're not within debounce time
    {
      DoorBellTriggered = 1;
	  DoorBellValTriggeredMillis = currentMillis;
	}
  }
  if (DoorbellTriggered == 1) {
	  DoorBellTriggered = 0;
      DoorMonitorDoorbellPressed = currentMillis;				//Time snapshot of when doorbell pressed. Doesn't matter if overwritten by another press.
      DoorMonitorWaitState = 1;      							//Waiting  for the door to be opened.
      if (Button1State == OFF)
      {
        // button pressed
        //===============
        Button1State = ON;
        if (LED1State  == OFF)
        {
          // the button's pressed and LED is OFF
          // turn it ON
          //====================================
          Serial.println("Button One pressed.");
          digitalWrite(LEDPin1, ON);
          LED1State  = ON;
          servo1.write(180);
          Serial.println("Servo: Move!");
          LED1TimeStamp1 = currentMillis;
          Serial.println();
        }
        else if (LED1State  == ON)
        {
          // if the button's pressed and LED is ON
          // start flashing
          //======================================
          digitalWrite(LEDPin1, OFF);
          LED1State  = FLASH;
          servo1.write(180);
          Serial.println("Servo: Move!");
          Serial.print("Button One pressed again.");
          Serial.println();
          LED1TimeStamp1 = currentMillis;
        }
      }
    else
    {
      Button1State = OFF;
      servo1.write(0);
    } 
}	
  if (DoorMonitorWaitState == 1) {
    DoorMonitorPinReadValue = digitalRead(DoorMonitorPin);
    if (DoorMonitorDoorbellPressed + DoorMonitorWaitTime > currentMillis
      )
    {
      if (DoorMonitorPinReadValue == HIGH )
      {
        DoorMonitorWaitState = 0;
        //        Serial.println("Stand easy - the door got opened!");
      }
    }
    if (DoorMonitorDoorbellPressed + DoorMonitorWaitTime < currentMillis
      )
    {
      DoorMonitorWaitState = 0;
      //Serial.println("The door just went unanswered!");
    }
  }
  if (LED1TimeStamp1 + LED1OnDuration  < currentMillis) {
    // If the predefined interval has elapsed for a button press
    // turn led OFF!
    //==================================================================
    digitalWrite(LEDPin1, OFF);
    LED1State  = OFF;
  }
  if (LED1State == FLASH && LED1TimeStamp2 + FlashRate < currentMillis) {
    // If the predefined interval has elapsed for the second button    press
    // make the LED 'flash' by toggling it
    //===================================================================
    digitalWrite(LEDPin1, !digitalRead(LEDPin1));
    LED1TimeStamp2 = currentMillis;
  }
}
  // set all bytes in the buffer to 0
  memset(pb, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  pb[0] = 0b11100011;   // LI, Version, Mode
  pb[1] = 0;     // Stratum, or type of clock
  pb[2] = 6;     // Polling Interval
  pb[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  pb[12]  = 49;
  pb[13]  = 0x4E;
  pb[14]  = 49;
  pb[15]  = 52;

  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(pb,NTP_PACKET_SIZE);
  Udp.endPacket();
}