I'm writing a access control system using a wiegand reader I'm using a ethermega from freetronics. I use Interrupts to reader from the reader and everything works great but as soon as I put a digitalwrite in the main loop on a different pin in a different port group the reader1 variable returns a weird number instead of the one it returns every other time
I've attached the code as I'm running it ... if it helps I will trim it down just let me know
// include the library code:
#include <LiquidCrystal.h>
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#include <IniFile.h>
#include <EthernetUDP.h>
#include <Time.h>
#define LCD_RS 8
#define LCD_E 9
#define LCD_B4 2
#define LCD_B5 5
#define LCD_B6 6
#define LCD_B7 7
#define LCD_BL 3
#define LCD_BU A0
#define SD_SEL 4
#define ET_SEL 10
#define DR1_RELAY 43
#define BUTTON_NONE 0
#define BUTTON_RIGHT 1
#define BUTTON_UP 2
#define BUTTON_DOWN 3
#define BUTTON_LEFT 4
#define BUTTON_SELECT 5
#define LCD_BACKLIGHT_OFF() digitalWrite( LCD_BL, LOW )
#define LCD_BACKLIGHT_ON() digitalWrite( LCD_BL, HIGH )
#define DOOR1_OPEN() digitalWrite( DR1_RELAY, LOW )
#define DOOR1_LOCK() digitalWrite( DR1_RELAY, HIGH )
#define BUTTONHYSTERESIS 10
#define RIGHT_10BIT_ADC 0 // right
#define UP_10BIT_ADC 145 // up
#define DOWN_10BIT_ADC 329 // down
#define LEFT_10BIT_ADC 505 // left
#define SELECT_10BIT_ADC 741 // right
File myINIfile;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte defaultip[] = {192,168,0,200};
IPAddress currentIP;
byte buttonJustPressed = false;
byte buttonJustReleased = false;
byte buttonCanPress = true;
byte buttonWas = BUTTON_NONE;
unsigned long buttonLastPress;
unsigned long LCD_TimeOut;
boolean LCD_BackLightAlwaysOn = false;
boolean isDHCP;
IPAddress loadIP;
IPAddress loadGW;
IPAddress loadSN;
IPAddress loadDN;
boolean t_isDHCP = true;
int int_currentLine = 0;
int int_currentCursorLoc = 0;
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_B4, LCD_B5, LCD_B6, LCD_B7);
unsigned int ntp_localPort = 8888;
IPAddress timeServer(192, 43, 244, 18);
const int NTP_PACKET_SIZE= 48;
byte ntp_packetBuffer[ NTP_PACKET_SIZE];
EthernetUDP Udp;
int timeZoneHour = -4;
int temp_timezone;
long timeZoneOffset = (timeZoneHour * -1) * 60 * 60 ;
int NTP_Update_Interval = 60 * 60 * 24;
volatile long reader1 = 0;
volatile int reader1Count = 0;
void reader1One(void) {
if(digitalRead(21) == LOW)
{
if (reader1Count < 26){
reader1Count++;
reader1 = reader1 << 1;
reader1 |= 1;
}
}
}
void reader1Zero(void) {
if(digitalRead(20) == LOW)
{
if (reader1Count < 26){
reader1Count++;
reader1 = reader1 << 1;
}
}
}
void setup() {
boolean SD_Works = false;
boolean SD_File_Works = false;
pinMode( LCD_BU, INPUT );
pinMode( LCD_BL, OUTPUT );
digitalWrite( LCD_BU, LOW );
LCD_BACKLIGHT_ON();
pinMode(SD_SEL, OUTPUT);
digitalWrite(SD_SEL, HIGH);
pinMode(ET_SEL, OUTPUT);
digitalWrite(ET_SEL, HIGH);
lcd.begin(16, 2);
lcd.print("Starting Up ...");
delay(300);
lcd.print("Starting Reader");
attachInterrupt(2, reader1One, FALLING);
attachInterrupt(3, reader1Zero, FALLING);
pinMode(DR1_RELAY, OUTPUT);
digitalWrite(DR1_RELAY, HIGH);
reader1 = 0;
reader1Count = 0;
if (!SD.begin(SD_SEL)) {
lcd.clear();
lcd.print("SD Card Failed");
SD_Works = false;
} else {
lcd.clear();
lcd.print("SD Card Works");
SD_Works = true;
myINIfile = SD.open("net.ini", FILE_READ);
if (myINIfile) {
SD_File_Works = true;
} else {
SD_File_Works = false;
}
myINIfile.close();
}
lcd.clear();
lcd.print("Starting Ethernet");
if (!SD_File_Works){
if (Ethernet.begin(mac) == 0) {
lcd.clear();
lcd.print("NO DHCP/CONFIG");
lcd.setCursor(0,1);
lcd.print("192.168.0.200");
Ethernet.begin(mac,defaultip);
currentIP = IPAddress(192,168,0,200);
delay(2000);
} else {
lcd.clear();
lcd.print("DHCP");
lcd.setCursor(0,1);
lcd.print(Ethernet.localIP());
currentIP = Ethernet.localIP();
delay(2000);
}
} else {
const char *filename = "net.ini";
IniFile ini(filename);
if (ini.open()) {
LCD_BackLightAlwaysOn = ini.getBooleanValue("backlight","alwayson");
timeZoneHour = ini.getValue("timezone", "offset");
isDHCP = !ini.getBooleanValue("network","static");
if (isDHCP){
if (Ethernet.begin(mac) == 0) {
Ethernet.begin(mac,defaultip);
currentIP = IPAddress(192,168,0,200);
delay(2000);
} else {
currentIP = Ethernet.localIP();
delay(2000);
}
} else {
boolean load_sucessful = true;
if (!ini.getIPAddress("network", "IP", loadIP)){
// Error Loading IP
load_sucessful = false;
}
if (!ini.getIPAddress("network", "GW", loadGW)){
// Error Loading GW
load_sucessful = false;
}
if (!ini.getIPAddress("network", "SN", loadSN)){
// Error Loading SubNet
load_sucessful = false;
}
if (!ini.getIPAddress("network", "DN", loadDN)){
// Error Loading DNS
load_sucessful = false;
}
if (load_sucessful == true) {
t_isDHCP = false;
Ethernet.begin(mac, loadIP, loadDN, loadGW, loadSN );
currentIP = Ethernet.localIP();
lcd.clear();
lcd.print("STATIC");
lcd.setCursor(0,1);
lcd.print(Ethernet.localIP());
} else {
if (Ethernet.begin(mac) == 0) {
//No Conf No DHCP
lcd.clear();
lcd.print("NO DHCP/CONFIG");
lcd.setCursor(0,1);
lcd.print("192.168.0.200");
Ethernet.begin(mac,defaultip);
currentIP = IPAddress(192,168,0,200);
} else {
lcd.clear();
lcd.print("Static Err-DHCP");
lcd.setCursor(0,1);
lcd.print(Ethernet.localIP());
currentIP = Ethernet.localIP();
}
}
delay(2000);
}
}
}
Udp.begin(ntp_localPort); // begin Time Lookup
lcd.clear();
lcd.print("Looking Up Time");
setSyncProvider(getNTPTime);
setSyncInterval(NTP_Update_Interval);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Ready");
if (SD_Works == false){
lcd.setCursor(0,1);
lcd.print(" *SD Card Failed");
}
LCD_TimeOut = millis()/1000;
Serial.begin(9600);
}
int ClearMillis;
boolean ClearIt = false;
void loop() {
byte button;
if (!LCD_BackLightAlwaysOn){
unsigned long currentTimeOut = ( (millis()/1000) - LCD_TimeOut);
if (currentTimeOut == 100){
LCD_BACKLIGHT_OFF();
int_currentLine = 0;
lcd.setCursor(0,1);
lcd.print(" ");
}
}
if (ClearIt == true){
int doorTimeOut = (millis() - ClearMillis);
if ( doorTimeOut > 3500){
lcd.setCursor(0,1);
lcd.print(" ");
ClearIt = false;
DOOR1_LOCK();
reader1 = 0;
reader1Count = 0;
}
}
DisplayTime();
int t_reader = reader1Count;
boolean FoundUser = false;
if(reader1Count >= 26){
ClearMillis = millis();
ClearIt = true;
lcd.setCursor(0,1);
Serial.println(reader1);
if (reader1 == 43912068){
lcd.print("Joni Albright");
FoundUser = true;
}
if (FoundUser != true){
lcd.print(reader1);
} else {
DOOR1_OPEN();
}
reader1 = 0;
reader1Count = 0;
}
}
void DisplayTime(){
lcd.setCursor(11,0);
time_t t = now();
if (hour(t) < 10){
lcd.setCursor(12,0);
}
lcd.print(hour(t)); // print the hour (86400 equals secs per day)
lcd.print(':');
if ( minute(t) < 10 ) {
lcd.print('0');
}
lcd.print(minute(t));
}
void DisplayCurrentLine(){
}
byte ReadButtons()
{
}
boolean AlreadySent = false;
unsigned long getNTPTime()
{
return 0;
}
unsigned long sendNTPpacket(IPAddress& address)
{
}