Since I went from my test situation of a switch controlling an IF routine to an analogread, things aren't working as before.
Before, if the switch was pressed, then the servo would whip back and forth.
Now, the servo sweeps only in one direction and then stays there...'stuck'.
I guess the obvious answer is because the analogpin is still reading a HIGH, but my debounce routine should preclude this, shouldn it?
THANKS
#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;
boolean EthernetRunning = false; //ATTN! Not sure how this will affect things...
IPAddress server(207,7,108,203); // Joyent
////////////SERVO////////////
Servo servo1;
////////////PIN LOCATIONS////////////
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 DoorBellPinReadValue = 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
////////////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; // ms to wait to see if door is opened (1m)
int DoorMonitorWaitState = 0; // A state where the sketch is monitoring the reed switch
int DoorMonitorPinReadValue = 0; //Variable to store the magnetic reed switch read value
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();
DoorBellPinReadValue = analogRead(DoorBellPin);
unsigned long currentMillis = millis();
if (DoorBellPinReadValue < 100) {
// If the doorbell pin voltage is less than 100 units (i.e. triggered)
if (currentMillis-DoorBellValTriggeredMillis > debounce) // and if we're not currently debouncing same
{
DoorBellValTriggeredMillis = currentMillis;
// SPECIFIC TO FRONT DOOR CODE BELOW
//====================================
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.
//SPECIFIC TO FRONT DOOR CODE ABOVE!!
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;
}
}
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration)
{
digitalWrite(PiezoPin,HIGH);
delayMicroseconds(period / 2);
digitalWrite(PiezoPin, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
void PrintDateTime(DateTime t) {
char datestr[24];
sprintf(datestr, "%04d-%02d-%02d %02d:%02d:%02d ", t.year(), t.month(), t.day(), t.hour(), t.minute(), t.second());
Serial.print(datestr);
}